ProfileBase.GetPropertyValue(String) 方法

定义

获取配置文件属性的值。

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

参数

propertyName
String

配置文件属性的名称。

返回

Object

指定的配置文件属性的值,类型化为 object

例外

尝试为匿名配置文件中 allowAnonymous 特性为 false 的属性设置一个属性值。

没有为当前配置文件定义的属性。

  • 或 -

当前配置文件中不存在指定的配置文件属性名。

  • 或 -

指定的配置文件属性的提供程序不能识别所指定的属性。

示例

下面的代码示例演示读取和设置ZipCode为用户配置文件指定的属性的 ASP.NET 页。 有关指定用户配置文件属性的Web.config文件的示例,请参阅为 ProfileBase 类提供的示例。

重要

此示例包含接受用户输入的文本框,这是潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 强类型访问器调用 GetPropertyValue 该方法从生成的访问器中检索非类型化值 ProfileProvider ,然后将转换为指定类型并返回为属性值。

可以使用该方法 GetPropertyValue 按名称检索应用程序的用户配置文件的属性值。 返回的值是非类型化的,在检索时必须强制转换为特定对象类型。 对于对配置文件属性值的强类型访问,可以按名称作为每个页面上可用的 Profile 属性的成员来访问该属性,例如。 Profile.CustomerAddress

适用于

另请参阅