ProfileBase.Item[String] 属性
定义
获取或设置按属性名进行索引的配置文件属性值。Gets or sets a profile property value indexed by the property name.
public:
virtual property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ propertyName); void set(System::String ^ propertyName, System::Object ^ value); };
public override object this[string propertyName] { get; set; }
member this.Item(string) : obj with get, set
Default Public Overrides Property Item(propertyName As String) As Object
参数
- propertyName
- String
配置文件属性的名称。The name of the profile property.
属性值
指定的配置文件属性的值,类型化为 object。The value of the specified profile property, typed as object.
例外
尝试为匿名配置文件中 allowAnonymous 特性为 false 的属性设置一个属性值。An attempt was made to set a property value on an anonymous profile where the property's allowAnonymous attribute is false.
没有为当前配置文件定义的属性。There are no properties defined for the current profile.
- 或 --or-
当前配置文件中不存在指定的配置文件属性名。The specified profile property name does not exist in the current profile.
- 或 --or-
指定的配置文件属性的提供程序不能识别所指定的属性。The provider for the specified profile property did not recognize the specified property.
尝试设置标记为只读的属性值。An attempt was made to set a property value that was marked as read-only.
尝试使用不兼容的类型给属性赋值。An attempt was made to assign a value to a property using an incompatible type.
示例
下面的代码示例通过 Name 将属性从静态 Properties 对象集合绑定 SettingsProperty 到控件来列出用户配置文件中的属性的名称 GridView 。The following code example lists the names of the properties in the user profile by binding the Name property from the static Properties collection of SettingsProperty objects to a GridView control. 选定的属性值使用集合按名称检索 Item[] 。The selected property value is retrieved by name using the Item[] collection. 有关指定用户配置文件属性的 Web.config 文件的示例,请参阅为类提供的示例 ProfileBase 。For an example of a Web.config file that specifies properties for the user profile, see the example provided for the ProfileBase class.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Profile" %>
<!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_Load()
{
if (!IsPostBack)
{
PropertiesListBox.DataSource = ProfileBase.Properties;
PropertiesListBox.DataBind();
}
if (PropertiesListBox.SelectedItem != null)
{
object propValue = Profile[PropertiesListBox.SelectedItem.Text];
Type propType = propValue.GetType();
// If the property is a value type, return ToString().
if (propType == typeof(string) || propType.IsValueType)
{
ValueLabel.Visible = true;
ValueGridView.Visible = false;
ValueLabel.Text = propValue.ToString();
return;
}
// Bind the property to a GridView.
try
{
ValueGridView.DataSource = propValue;
ValueGridView.DataBind();
ValueGridView.Visible = true;
ValueLabel.Visible = false;
}
catch
{
// If the property is not bindable, return ToString().
ValueLabel.Visible = true;
ValueGridView.Visible = false;
ValueLabel.Text = propValue.ToString();
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>
<h3>View Profile properties:</h3>
<form id="form1" runat="server">
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td>Property</td>
<td>Value</td>
</tr>
<tr>
<td valign="top">
<asp:ListBox runat="server" id="PropertiesListBox" Rows="10" AutoPostBack="True" DataTextField="Name" />
</td>
<td valign="top">
<asp:GridView runat="Server" id="ValueGridView" Visible="False" />
<asp:Label runat="Server" id="ValueLabel" Visible="False" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Profile" %>
<!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_Load()
If Not IsPostBack Then
PropertiesListBox.DataSource = ProfileBase.Properties
PropertiesListBox.DataBind()
End If
If Not PropertiesListBox.SelectedItem Is Nothing Then
Dim propValue As Object = Profile(PropertiesListBox.SelectedItem.Text)
Dim propType As Type = propValue.GetType()
' If the property is a value type, return ToString().
If propType Is GetType(String) Or propType.IsValueType Then
ValueLabel.Visible = True
ValueGridView.Visible = False
ValueLabel.Text = propValue.ToString()
Return
End If
' Bind the property to a GridView.
Try
ValueGridView.DataSource = propValue
ValueGridView.DataBind()
ValueGridView.Visible = True
ValueLabel.Visible = False
Catch
' If the property is not bindable, return ToString().
ValueLabel.Visible = True
ValueGridView.Visible = False
ValueLabel.Text = propValue.ToString()
End Try
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>
<h3>View Profile properties:</h3>
<form id="form1" runat="server">
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td>Property</td>
<td>Value</td>
</tr>
<tr>
<td valign="top">
<asp:ListBox runat="server" id="PropertiesListBox" Rows="10" AutoPostBack="True" DataTextField="Name" />
</td>
<td valign="top">
<asp:GridView runat="Server" id="ValueGridView" Visible="False" />
<asp:Label runat="Server" id="ValueLabel" Visible="False" />
</td>
</tr>
</table>
</form>
</body>
</html>
注解
可以使用此属性按名称检索或设置应用程序的用户配置文件的属性值。You can use this property to retrieve or set the property values of the user profile for your application by name. 返回的值的类型为 object ,并且在检索时必须强制转换为特定的对象类型。Returned values are typed as object and must be cast as the specific object type when retrieved. 对于对配置文件属性值的强类型访问,你可以按名称作为 Profile 每个页面上可用属性的成员访问属性,例如 Profile.CustomerAddress 。For strongly typed access to profile property values, you can access the property by name as a member of the Profile property available on each page, for example, Profile.CustomerAddress.