次の方法で共有


HtmlSelect.DataValueField プロパティ

HtmlSelect コントロールの各項目の ListItem.Value プロパティにバインドするデータ ソースのフィールドを取得または設定します。

名前空間: System.Web.UI.HtmlControls
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Overridable Property DataValueField As String
'使用
Dim instance As HtmlSelect
Dim value As String

value = instance.DataValueField

instance.DataValueField = value
public virtual string DataValueField { get; set; }
public:
virtual property String^ DataValueField {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_DataValueField ()

/** @property */
public void set_DataValueField (String value)
public function get DataValueField () : String

public function set DataValueField (value : String)
適用できません。

プロパティ値

HtmlSelect コントロールの各項目の ListItem.Value プロパティにバインドするデータ ソースのフィールド。既定値は空の文字列 ("") です。このプロパティが設定されていないことを示します。

解説

DataValueField プロパティを使用して、コントロールの各項目の ListItem.Value プロパティにデータ ソースのどのフィールドをバインドするかを指定します。このプロパティは通常、ListItem.Text プロパティの値とは異なる ListItem.Value プロパティの値を提供するために使用されます。

HtmlSelect クラスには、バインドするデータ ソースを指定するための 2 つのプロパティが用意されています。DataSource プロパティでは、IEnumerable インターフェイスまたは IListSource インターフェイスを実装するオブジェクト (DataViewArrayListHashtable など) に HtmlSelect コントロールをバインドできます。DataSource プロパティを使用してデータ ソースを指定した場合、DataBind メソッドを明示的に呼び出して、コントロールとそのコントロールの DataValueField プロパティをデータ ソースにバインドする必要があります。

DataSourceID プロパティでは、データ ソースを表すデータ ソース コントロールに HtmlSelect コントロールをバインドできます。DataSourceID プロパティを使用してデータ ソースを指定した場合、HtmlSelect コントロールとそのコントロールの DataValueField プロパティがデータ ソース コントロールに自動的にバインドされます。したがって、DataBind メソッドを明示的に呼び出す必要はありません。

使用例

DataSource プロパティと DataValueField プロパティを使用して、HtmlSelect コントロールの各項目の ListItem.Value プロパティにバインドするデータ ソースのフィールドを指定する方法を次のコード例に示します。

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title> HtmlSelect Example </title>
<script runat="server">

      Sub Page_Load (sender As Object, e As EventArgs)
  
        ' Bind the HtmlSelect control to a data source when the page is initially loaded.
        If Not IsPostBack Then
        
           ' Open a connection to the database and run the query.
           ' Note that the connection string may vary depending on your
           ' database server settings.
           Dim ConnectString As String = "server=localhost;database=pubs;integrated security=SSPI"
           Dim QueryString As String = "select * from authors"

           Dim myConnection As SqlConnection = New SqlConnection(ConnectString)
           Dim myCommand As SqlDataAdapter = New SqlDataAdapter(QueryString, myConnection)

           ' Create a dataset to store the query results.
           Dim ds As DataSet = New DataSet()
           myCommand.Fill(ds, "Authors")

           ' Bind the HtmlSelect control to the data source.
           Select1.DataSource = ds
           Select1.DataTextField = "au_fname"
           Select1.DataValueField = "au_fname"
           Select1.DataBind()
        
        End If

      End Sub

      Sub Button_Click (sender As Object, e As EventArgs)
        
         Dim i As Integer

         Label1.Text = "You selected:"

         For i = 0 To Select1.Items.Count - 1
         
            If Select1.Items(i).Selected Then
               Label1.Text = Label1.Text & "<br /> &nbsp;&nbsp; - " & Select1.Items(i).Text
            End If

         Next i

      End Sub

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list. <br />
      Use the Control or Shift key to select multiple items. <br /><br />

      <select id="Select1"
              multiple="true" 
              runat="server"/>

      <br /><br />

      <button id="Button1"
              onserverclick="Button_Click"
              runat="server">

         Submit

      </button>

      <br /><br />

      <asp:Label id="Label1"
           runat="server"/>

   </form>

</body>

</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head>
    <title> HtmlSelect Example </title>
<script runat="server">

      void Page_Load (Object sender, EventArgs e)
      {

        // Bind the HtmlSelect control to a data source when the page is initially loaded.
        if (!IsPostBack)
        {
      
           // Open a connection to the database and run the query.
           // Note that the connection string may vary depending on your
           // database server settings. 
           string ConnectString = "server=localhost;database=pubs;integrated security=SSPI";
           string QueryString = "select * from authors";

           SqlConnection myConnection = new SqlConnection(ConnectString);
           SqlDataAdapter myCommand = new SqlDataAdapter(QueryString, myConnection);

           // Create a dataset to store the query results.
           DataSet ds = new DataSet();
           myCommand.Fill(ds, "Authors");

           // Bind the HtmlSelect control to the data source.
           Select1.DataSource = ds;
           Select1.DataTextField = "au_fname";
           Select1.DataValueField = "au_fname";
           Select1.DataBind();
        }

      }

      void Button_Click (Object sender, EventArgs e)
      {
       
         // Display the selected items. 
         Label1.Text = "You selected:";

         for (int i=0; i<=Select1.Items.Count - 1; i++)
         {
            if (Select1.Items[i].Selected)
               Label1.Text += "<br /> &nbsp;&nbsp; - " + Select1.Items[i].Text;
         }

      }

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list. <br />
      Use the Control or Shift key to select multiple items. <br /><br />

      <select id="Select1"
              multiple="true" 
              runat="server"/>

      <br /><br />

      <button id="Button1"
              onserverclick="Button_Click"
              runat="server">

         Submit

      </button>

      <br /><br />

      <asp:Label id="Label1"
           runat="server"/>

   </form>

</body>

</html>

DataSourceID プロパティと DataValueField プロパティを使用して、HtmlSelect コントロールの各項目の ListItem.Value プロパティにバインドするデータ ソースのフィールドを指定する方法を次のコード例に示します。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server" language="vb">
  
  Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim i As Integer
    ' Iterate through the list items in the 
    ' HtmlSelect control to find the selected item.
    For i = 0 To Select1.Items.Count - 1
      If Select1.Items(i).Selected Then
        ' Display the Value property of the selected item.
        ' This property is populated by the DataValueField property.
        Label1.Text = "The Product ID is " & Select1.Items(i).Value
      End If
    Next i
  
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HtmlSelect.DataValueField</title>
</head>
<body>
  <form id="Form1" runat="server">  

    <h3> HtmlSelect.DataValueField Example 2</h3>

    <p>Select an item from the list:</p>

    <select id="Select1"
      name="Select1"             
      datasourceid="SqlDataSource1"
      datatextfield="ProductName"
      datavaluefield="ProductID"
      runat="server">
    </select>
       
    <asp:sqldatasource id="SqlDataSource1" 
          connectionstring="workstation id=localhost;integrated security=SSPI;initial catalog=Northwind"
      selectcommand="SELECT * FROM [Products] Where ProductID <= 5"
      runat="server">
    </asp:sqldatasource>
    
    <asp:button id="SubmitButton"
      text="Submit"
      onclick="SubmitButton_Click"
      runat="Server">
    </asp:button>
    
    <br /><br />
    
    <asp:label id="Label1"
      runat="Server">
    </asp:label>

   </form>

</body>

</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server" language="C#">
  
  void SubmitButton_Click (object sender, System.EventArgs e)
  {
    // Iterate through the list items in the 
    // HtmlSelect control to find the selected item.
    for (int i = 0; i <= Select1.Items.Count - 1; i++)
    {
      if (Select1.Items[i].Selected)
        // Display the Value property of the selected item.
        // This property is populated by the DataValueField property.
        Label1.Text = "The Product ID is " + Select1.Items[i].Value;
    }
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HtmlSelect.DataValueField</title>
</head>
<body>
  <form id="Form1" runat="server">  

    <h3> HtmlSelect.DataValueField Example 2</h3>

    <p>Select an item from the list:</p>

    <select id="Select1"
      name="Select1"             
      datasourceid="SqlDataSource1"
      datatextfield="ProductName"
      datavaluefield="ProductID"
      runat="server">
    </select>
       
    <asp:sqldatasource id="SqlDataSource1"          
          connectionstring="workstation id=localhost;integrated security=SSPI;initial catalog=Northwind"
      selectcommand="SELECT * FROM [Products] Where ProductID <= 5"
      runat="server">
    </asp:sqldatasource>
    
    <asp:button id="SubmitButton"
      text="Submit"
      onclick="SubmitButton_Click"
      runat="Server">
    </asp:button>
    
    <br /><br />
    
    <asp:label id="Label1"
      runat="Server">
    </asp:label>

   </form>

</body>

</html>

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

HtmlSelect クラス
HtmlSelect メンバ
System.Web.UI.HtmlControls 名前空間
HtmlSelect.DataSource プロパティ
ListItem.Value
HtmlSelect.DataTextField プロパティ

その他の技術情報

HTML サーバー コントロール