ProfileBase.SetPropertyValue(String, Object) Méthode

Définition

Définit la valeur d'une propriété de profil.

public:
 void SetPropertyValue(System::String ^ propertyName, System::Object ^ propertyValue);
public void SetPropertyValue (string propertyName, object propertyValue);
member this.SetPropertyValue : string * obj -> unit
Public Sub SetPropertyValue (propertyName As String, propertyValue As Object)

Paramètres

propertyName
String

Nom de la propriété à définir.

propertyValue
Object

Valeur à assigner à la propriété.

Exceptions

Une tentative de définition d'une valeur de propriété pour un profil anonyme a été effectuée pour une propriété dont l'attribut allowAnonymous est false.

Aucune propriété n'est définie pour le profil actuel.

  • ou -

Le nom de propriété de profil spécifié n'existe pas dans le profil actuel.

  • ou -

Le fournisseur de la propriété de profil spécifiée n'a pas reconnu la propriété spécifiée.

Une tentative a été faite pour définir une valeur sur une propriété marquée en lecture seule.

Une tentative d'assignation d'une valeur à une propriété à l'aide d'un type incompatible a été exécutée.

Exemples

L’exemple de code suivant montre une page ASP.NET qui lit et définit la ZipCode propriété spécifiée pour le profil utilisateur. Pour obtenir un exemple de fichier Web.config qui spécifie les propriétés du profil utilisateur, consultez l’exemple fourni pour la ProfileBase classe.

Important

Cet exemple contient une zone de texte qui accepte l’entrée utilisateur, qui est une menace de sécurité potentielle. Par défaut, les pages web ASP.NET vérifient que l’entrée d’utilisateur n’inclut pas de script ou d’éléments HTML. Pour plus d’informations, consultez Vue d’ensemble des attaques de 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>

Remarques

ASP.NET utilise la ProfileBase classe pour créer la classe utilisée pour le profil utilisateur. Lorsqu’une application avec le profil utilisateur activé est démarrée, ASP.NET crée une nouvelle classe de typeProfileCommon, qui hérite de la ProfileBase classe. Les accesseurs fortement typés sont ajoutés à la ProfileCommon classe pour chaque propriété définie dans la section configuration du profil . Les accesseurs fortement typés de la ProfileCommon classe appellent la SetPropertyValue méthode pour transmettre des valeurs de propriété à la ProfileProvider source de données.

Vous pouvez utiliser la SetPropertyValue méthode pour affecter des valeurs de propriété dans le profil utilisateur de votre application par nom. Les valeurs ne sont pas typées et la vérification des types est effectuée au moment de l’exécution, et non pas au moment de la compilation. Pour un accès fortement typé aux valeurs de propriété de profil, vous pouvez accéder à la propriété par nom en tant que membre de la Profile propriété disponible sur chaque page, par exemple Profile.CustomerAddress.

S’applique à

Voir aussi