Partager via


ProfileBase Constructeur

Définition

Crée une instance de la classe ProfileBase.

public:
 ProfileBase();
public ProfileBase();
Public Sub New ()

Exceptions

L’attribut enabled de la section profil du fichier Web.config est false.

Impossible de créer un type de propriété spécifié dans la section profil du fichier Web.config.

- ou -

L’attribut allowAnonymous d’une propriété dans la section profil du fichier Web.config est défini true sur et l’attribut enabled de l’élément <anonymousIdentification> est défini sur false.

- ou -

L’attribut serializeAs d’une propriété dans la section profil du fichier Web.config est défini Binary sur et la IsSerializable propriété de la propriété spécifiée type retourne false.

- ou -

Le nom d’un fournisseur spécifié à l’aide de l’attribut provider d’une propriété de profil est introuvable dans la Providers collection.

- ou -

Impossible de trouver la type propriété de profil spécifiée.

- ou -

Une propriété de profil a été spécifiée avec un nom qui correspond à un nom de propriété sur la classe de base spécifiée dans l’attribut inherits de la section profil .

Exemples

Le fichier Web.config suivant spécifie un profil utilisateur qui contient une ZipCode propriété de type string et une RecentSearchList propriété de type StringCollection. La propriété générée Profile du courant HttpContext aura des accesseurs fortement typés pour chacune des propriétés spécifiées.

<configuration>
   <connectionStrings>
       <add name="SqlServices" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
   </connectionStrings>

  <system.web>
   <anonymousIdentification enabled="true" />

   <profile defaultProvider="SqlProvider" >
     <providers>
       <add
         name="SqlProvider"
         connectionStringName="SqlServices"
         applicationName="ProfileBaseApplication"
         type="System.Web.Profile.SqlProfileProvider" />
     </providers>

     <properties>
       <add name="ZipCode" allowAnonymous="true" />
       <add name="RecentSearchList"
            type="System.Collections.Specialized.StringCollection"
            serializeAs="Xml"
            allowAnonymous="true" />
      </properties>
    </profile>
   </system.web>
</configuration>

La page ASP.NET suivante lit et définit la ZipCode propriété spécifiée pour le profil utilisateur.

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, ASP.NET pages web valident que l’entrée utilisateur n’inclut pas de script ou d’éléments HTML. Pour plus d’informations, consultez Vue d’ensemble des exploits 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 sur laquelle le profil utilisateur est activé est activée, ASP.NET crée une classe de type ProfileCommon, 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 respectivement les méthodes et SetPropertyValue les GetPropertyValue méthodes de la ProfileBase classe de base pour récupérer et définir des valeurs de propriété de profil. Une instance de la ProfileCommon classe est définie comme valeur de la Profile propriété pour l’application ASP.NET.

Note

La classe de base utilisée pour générer la classe stockée dans la Profile propriété peut être substituée à l’aide de l’attribut inherits de la section profil du fichier de configuration. Dans ce cas, vous spécifiez une classe personnalisée qui hérite de la ProfileBase classe de base.

Ce constructeur n’est pas destiné à être utilisé à partir du code d’application. Pour créer une instance d’un profil utilisateur, utilisez la Create méthode.

S’applique à

Voir aussi