Bagikan melalui


ProfileBase Konstruktor

Definisi

Membuat instans ProfileBase kelas .

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

Pengecualian

Atribut enabled bagian profil dari file Web.config adalah false.

Tipe properti yang ditentukan di bagian profil file Web.config tidak dapat dibuat.

-atau-

Atribut allowAnonymous untuk properti di bagian profil file Web.config diatur ke true dan enabled atribut <elemen anonymousIdentification> diatur ke false.

-atau-

Atribut serializeAs untuk properti di bagian profil file Web.config diatur ke Binary dan IsSerializable properti dari pengembalian falseyang ditentukan type .

-atau-

Nama penyedia yang ditentukan menggunakan provider atribut properti profil tidak dapat ditemukan dalam Providers koleksi.

-atau-

Properti type profil yang ditentukan tidak dapat ditemukan.

-atau-

Properti profil ditentukan dengan nama yang cocok dengan nama properti pada kelas dasar yang ditentukan di inherits atribut bagian profil .

Contoh

File Web.config berikut menentukan profil pengguna yang berisi ZipCode properti jenis string dan RecentSearchList properti jenis StringCollection. Properti yang dihasilkan Profile dari saat ini HttpContext akan memiliki aksesor yang sangat ditik untuk setiap properti yang ditentukan.

<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>

Halaman ASP.NET berikut membaca dan mengatur ZipCode properti yang ditentukan untuk profil pengguna.

Penting

Contoh ini berisi kotak teks yang menerima input pengguna, yang merupakan potensi ancaman keamanan. Secara default, ASP.NET halaman Web memvalidasi bahwa input pengguna tidak menyertakan elemen skrip atau HTML. Untuk informasi selengkapnya, lihat Gambaran Umum Eksploitasi Skrip.

<%@ 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>

Keterangan

ASP.NET menggunakan ProfileBase kelas untuk membuat kelas yang digunakan untuk profil pengguna. Ketika aplikasi yang mengaktifkan profil pengguna dimulai, ASP.NET membuat kelas jenis ProfileCommonbaru , yang mewarisi dari ProfileBase kelas . Aksesor yang sangat ditik ditambahkan ke ProfileCommon kelas untuk setiap properti yang ditentukan di bagian konfigurasi profil . Pengakses ProfileCommon kelas yang sangat ditik memanggil GetPropertyValue metode dan SetPropertyValue dari ProfileBase kelas dasar untuk mengambil dan mengatur nilai properti profil. Instans ProfileCommon kelas ditetapkan sebagai nilai Profile properti untuk aplikasi ASP.NET.

Catatan

Kelas dasar yang digunakan untuk menghasilkan kelas yang disimpan di Profile properti dapat ditimpa menggunakan inherits atribut bagian profil dari file konfigurasi. Dalam hal ini Anda akan menentukan kelas kustom yang mewarisi dari ProfileBase kelas dasar.

Konstruktor ini tidak dimaksudkan untuk digunakan dari kode aplikasi. Untuk membuat instans profil pengguna, gunakan Create metode .

Berlaku untuk

Lihat juga