共用方式為


ProfileBase.SetPropertyValue(String, Object) 方法

定義

設定設定檔屬性的值。

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)

參數

propertyName
String

所要設定的屬性名稱。

propertyValue
Object

要指派給屬性的值。

例外狀況

嘗試在匿名的設定檔上設定屬性 (Property) 值,其中屬性的 allowAnonymous 屬性 (Attribute) 是 false

目前的設定檔沒有定義任何屬性。

-或-

目前的設定檔中沒有指定的設定檔屬性名稱。

-或-

指定設定檔屬性的提供者無法辨認指定的屬性。

嘗試設定標記為唯讀的屬性值。

嘗試使用不相容的型別將值指派給屬性。

範例

下列程式碼範例顯示 ASP.NET 網頁,可讀取及設定 ZipCode 為使用者設定檔指定的屬性。 如需指定使用者設定檔屬性的Web.config檔案範例,請參閱為 ProfileBase 類別提供的範例。

重要

此範例包含接受使用者輸入的文字方塊,這是潛在的安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

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

備註

ASP.NET 會 ProfileBase 使用 類別來建立用於使用者設定檔的類別。 啟動已啟用使用者設定檔的應用程式時,ASP.NET 建立類型 ProfileCommon 的新類別,此類別繼承自 ProfileBase 類別。 強型別存取子會針對設定檔組態區段中定義的每個屬性新增至 ProfileCommon 類別。 類別的 ProfileCommon 強型別存取子會呼叫 方法, SetPropertyValue 將屬性值傳遞至 ProfileProvider 要儲存在資料來源的 。

您可以使用 SetPropertyValue 方法,依名稱指派應用程式使用者設定檔中的屬性值。 值不具類型,且類型檢查會在執行時間完成,而不是編譯時間。 對於配置檔案屬性值的強型別存取,您可以依名稱來存取屬性,作為每個頁面上可用屬性的成員 Profile ,例如 Profile.CustomerAddress

適用於

另請參閱