ProfileBase.Item[String] プロパティ

定義

プロパティ名のインデックスによって指定されたプロパティ値を取得または設定します。

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

プロファイル プロパティの名前。

プロパティ値

指定されたプロファイル プロパティの値。object 型として返されます。

例外

プロパティの allowAnonymous 属性が false である匿名プロファイルに対してプロパティ値を設定しようとしました。

現在のプロファイルに対して定義されたプロパティはありません。

- または -

指定されたプロファイル プロパティ名が、現在のプロファイルに存在しません。

- または -

指定されたプロファイル プロパティのプロバイダーは、指定されたプロパティを認識しませんでした。

読み取り専用にマークされたプロパティ値を設定しようとしました。

互換性のない型を使用してプロパティに値を代入しようとしました。

次のコード例では、オブジェクトの静的Propertiesコレクションからコントロールに プロパティをNameバインドすることで、ユーザー プロファイル内のプロパティのSettingsProperty名前をGridView一覧表示します。 選択したプロパティ値は、 コレクションを使用して Item[] 名前で取得されます。 ユーザー プロファイルのプロパティを指定するWeb.config ファイルの例については、 クラスに提供されている例を ProfileBase 参照してください。

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

注釈

このプロパティを使用して、アプリケーションのユーザー プロファイルのプロパティ値を名前で取得または設定できます。 戻り値は として object 型指定され、取得時に特定のオブジェクト型としてキャストする必要があります。 プロファイル プロパティの値に厳密に型指定されたアクセス権を持つ場合は、各ページで使用できるプロパティの Profile メンバーとして、名前によって プロパティにアクセスできます (例: Profile.CustomerAddress)。

適用対象

こちらもご覧ください