ProfileBase.GetPropertyValue(String) Metodo

Definizione

Ottiene il valore di una proprietà del profilo.

public:
 System::Object ^ GetPropertyValue(System::String ^ propertyName);
public object GetPropertyValue (string propertyName);
member this.GetPropertyValue : string -> obj
Public Function GetPropertyValue (propertyName As String) As Object

Parametri

propertyName
String

Nome della proprietà del profilo.

Restituisce

Il valore della proprietà del profilo specificata, tipizzata come object.

Eccezioni

È stato effettuato un tentativo di impostare il valore di una proprietà di un profilo anonimo in cui l'attributo allowAnonymous della proprietà è false.

Non è stata definita alcuna proprietà per il profilo corrente.

-oppure-

Il nome della proprietà del profilo specificato non esiste nel profilo corrente.

-oppure-

Il provider per la proprietà del profilo specificato non riconosce la proprietà specificata.

Esempio

Nell'esempio di codice seguente viene illustrata una pagina ASP.NET che legge e imposta la ZipCode proprietà specificata per il profilo utente. Per un esempio di file di Web.config che specifica le proprietà per il profilo utente, vedere l'esempio fornito per la ProfileBase classe.

Importante

Questo esempio contiene una casella di testo che accetta l'input utente, ovvero una potenziale minaccia di sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite script.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void Page_PreRender()
{
  if (Profile.ZipCode == null)
  {
    PersonalizePanel.Visible = false;
    GetZipCodePanel.Visible = true;
  }
  else
  {
    ZipCodeLabel.Text = Profile.ZipCode;

    // Get personalized information for zip code here.

    PersonalizePanel.Visible = true;
    GetZipCodePanel.Visible = false;
  }
}

public void ChangeZipCode_OnClick(object sender, EventArgs args)
{
  ZipCodeTextBox.Text = Profile.ZipCode;
  Profile.ZipCode = null;

  PersonalizePanel.Visible = false;
  GetZipCodePanel.Visible = true;
}

public void EnterZipCode_OnClick(object sender, EventArgs args)
{
  Profile.ZipCode = ZipCodeTextBox.Text;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>

<form id="form1" runat="server">
  <table border="1" cellpadding="2" cellspacing="2">
    <tr>
      <td>
        <asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
          Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
          <!-- Information for Zip Code here. -->
          <br />
          <asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
                          OnClick="ChangeZipCode_OnClick" />
        </asp:Panel>
        <asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
          You can personalize this page by entering your Zip Code: 
          <asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
          <asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
                          OnClick="EnterZipCode_OnClick" />
        </asp:Panel>
      </td>
    </tr>
  </table>
</form>

</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub Page_PreRender()

  If Profile.ZipCode = Nothing Then
    PersonalizePanel.Visible = False
    GetZipCodePanel.Visible = True
  Else
    ZipCodeLabel.Text = Profile.ZipCode

    ' Get personalized information for zip code here.

    PersonalizePanel.Visible = True
    GetZipCodePanel.Visible = False
  End If

End Sub

Public Sub ChangeZipCode_OnClick(sender As Object, args As EventArgs)
  ZipCodeTextBox.Text = Profile.ZipCode
  Profile.ZipCode = Nothing

  PersonalizePanel.Visible = False
  GetZipCodePanel.Visible = True
End Sub

Public Sub EnterZipCode_OnClick(sender As Object, args As EventArgs)
  Profile.ZipCode = ZipCodeTextBox.Text
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>

<form id="form1" runat="server">
  <table border="1" cellpadding="2" cellspacing="2">
    <tr>
      <td>
        <asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
          Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
          <!-- Information for Zip Code here. -->
          <br />
          <asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
                          OnClick="ChangeZipCode_OnClick" />
        </asp:Panel>
        <asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
          You can personalize this page by entering your Zip Code: 
          <asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
          <asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
                          OnClick="EnterZipCode_OnClick" />
        </asp:Panel>
      </td>
    </tr>
  </table>
</form>

</body>
</html>

Commenti

ASP.NET usa la classe per creare la ProfileBase classe usata per il profilo utente. Quando viene avviata un'applicazione con il profilo utente abilitato, ASP.NET crea una nuova classe di tipo ProfileCommon, che eredita dalla ProfileBase classe . Le funzioni di accesso fortemente tipizzata vengono aggiunte alla ProfileCommon classe per ogni proprietà definita nella sezione configurazione del profilo . Le funzioni di accesso fortemente tipizzata della ProfileCommon classe chiamano il GetPropertyValue metodo per recuperare i valori non tipizzati dall'oggetto ProfileProvider che la funzione di accesso generata esegue quindi il cast come tipo specificato e restituisce come valore della proprietà.

È possibile usare il GetPropertyValue metodo per recuperare i valori delle proprietà del profilo utente per l'applicazione in base al nome. I valori restituiti non sono tipizzati e devono essere distribuiti come tipo di oggetto specifico quando viene recuperato. Per l'accesso fortemente tipizzato ai valori delle proprietà del profilo, è possibile accedere alla proprietà in base al nome come membro della proprietà Profile disponibile in ogni pagina, ad esempio Profile.CustomerAddress.

Si applica a

Vedi anche