HtmlSelect.DataMember-Eigenschaft
Ruft die Datengruppe ab, die aus einer DataSource-Eigenschaft mit mehreren Datengruppen an das HtmlSelect-Steuerelement gebunden werden soll, oder legt diese fest.
Namespace: System.Web.UI.HtmlControls
Assembly: System.Web (in system.web.dll)
Syntax
'Declaration
Public Overridable Property DataMember As String
'Usage
Dim instance As HtmlSelect
Dim value As String
value = instance.DataMember
instance.DataMember = value
public virtual string DataMember { get; set; }
public:
virtual property String^ DataMember {
String^ get ();
void set (String^ value);
}
/** @property */
public String get_DataMember ()
/** @property */
public void set_DataMember (String value)
public function get DataMember () : String
public function set DataMember (value : String)
Eigenschaftenwert
Die Datengruppe, die aus einer DataSource mit mehreren Datengruppen an das HtmlSelect-Steuerelement gebunden werden soll. Der Standardwert ist eine leere Zeichenfolge (""), die angibt, dass diese Eigenschaft nicht festgelegt wurde.
Hinweise
Wenn die DataSource-Eigenschaft mehrere Datengruppen enthält, können Sie mit der DataMember-Eigenschaft angeben, welche Datengruppe an das Steuerelement gebunden werden soll. Beispielsweise können Sie bei einem System.Data.DataSet-Objekt mit mehreren Tabellen mit dieser Eigenschaft angeben, welche Tabelle an das Steuerelement gebunden werden soll.
Nachdem Sie die Datenquelle angegeben haben, die an das Steuerelement gebunden werden soll, können Sie angeben, welche Felder der Datenquelle an die ListItem.Text-Eigenschaft und die ListItem.Value-Eigenschaft der einzelnen Elemente im Steuerelement gebunden werden sollen, indem Sie die DataTextField-Eigenschaft bzw. die DataValueField-Eigenschaft festlegen.
Beispiel
Im folgenden Codebeispiel wird veranschaulicht, wie mit der DataMember-Eigenschaft eine Datengruppe aus einer DataSource-Eigenschaft mit mehreren Datengruppen angegeben wird, die an das HtmlSelect-Steuerelement gebunden werden soll.
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<head>
<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
Dim i As Integer
' Create a data set to store the tables.
Dim ds As DataSet = New DataSet("TitleDataSet")
Dim dr As DataRow
' Create the authors table with a single column.
Dim authors As DataTable = New DataTable("Authors")
authors.Columns.Add(New DataColumn("LName", GetType(String)))
' Add the authors table to the data set.
ds.Tables.Add(authors)
' Create sample data in the authors table.
For i = 0 To 9
' Create a new row for the authors table.
dr = authors.NewRow()
' Add data to the LName column (column 0 in the row).
dr(0) = "Author " & i.ToString()
' Insert the row into the authors table.
authors.Rows.Add(dr)
Next i
' Create the titles table with a single column.
Dim titles As DataTable = New DataTable("Titles")
titles.Columns.Add(New DataColumn("BookTitle", GetType(String)))
' Add the titles table to the data set.
ds.Tables.Add(titles)
' Create sample data in the titles table.
For i = 0 To 9
' Create a new row for the titles table.
dr = titles.NewRow()
' Add data to the BookTitle column (column 0 in the row).
dr(0) = "Title " & i.ToString()
' Insert the row into the titles table.
titles.Rows.Add(dr)
Next i
' Bind the HtmlSelect control to the data source.
Select1.DataSource = ds
Select1.DataMember = "Titles"
Select1.DataTextField = "BookTitle"
Select1.DataBind()
End If
End Sub
Sub Button_Click (sender As Object, e As EventArgs)
Dim i As Integer
' Display the selected items.
Label1.Text = "You selected:"
For i=0 To Select1.Items.Count - 1
If Select1.Items(i).Selected Then
Label1.Text = Label1.Text & "<br> - " & Select1.Items(i).Text
End If
Next i
End Sub
</script>
</head>
<body>
<form 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" %>
<html>
<head>
<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)
{
// Create a data set to store the tables.
DataSet ds = new DataSet("TitleDataSet");
DataRow dr;
// Create the authors table with a single column.
DataTable authors = new DataTable("Authors");
authors.Columns.Add(new DataColumn("LName", typeof(string)));
// Add the authors table to the data set.
ds.Tables.Add(authors);
// Create sample data in the authors table.
for (int i = 0; i < 9; i++)
{
// Create a new row for the authors table.
dr = authors.NewRow();
// Add data to the LName column (column 0 in the row).
dr[0] = "Author " + i.ToString();
// Insert the row into the authors table.
authors.Rows.Add(dr);
}
// Create the titles table with a single column.
DataTable titles = new DataTable("Titles");
titles.Columns.Add(new DataColumn("BookTitle", typeof(string)));
// Add the titles table to the data set.
ds.Tables.Add(titles);
// Create sample data in the titles table.
for (int i = 0; i < 9; i++)
{
// Create a new row for the titles table.
dr = titles.NewRow();
// Add data to the BookTitle column (column 0 in the row).
dr[0] = "Title " + i.ToString();
// Insert the row into the titles table.
titles.Rows.Add(dr);
}
// Bind the HtmlSelect control to the data source.
Select1.DataSource = ds;
Select1.DataMember = "Titles";
Select1.DataTextField = "BookTitle";
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> - " + Select1.Items[i].Text;
}
}
</script>
</head>
<body>
<form 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="JScript" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<script runat="server">
function Page_Load (sender : Object, e : EventArgs)
{
// Bind the HtmlSelect control to a data source when the page is initially loaded.
if (!IsPostBack)
{
// Create a data set to store the tables.
var ds : DataSet = new DataSet("TitleDataSet");
var dr : DataRow;
// Create the authors table with a single column.
var authors : DataTable = new DataTable("Authors");
authors.Columns.Add(new DataColumn("LName", GetType("String")));
// Add the authors table to the data set.
ds.Tables.Add(authors);
// Create sample data in the authors table.
for (var i : int = 0; i < 9; i++)
{
// Create a new row for the authors table.
dr = authors.NewRow();
// Add data to the LName column (column 0 in the row).
dr[0] = "Author " + i.ToString();
// Insert the row into the authors table.
authors.Rows.Add(dr);
}
// Create the titles table with a single column.
var titles : DataTable = new DataTable("Titles");
titles.Columns.Add(new DataColumn("BookTitle", GetType("String")));
// Add the titles table to the data set.
ds.Tables.Add(titles);
// Create sample data in the titles table.
for (var j : int = 0; j < 9; j++)
{
// Create a new row for the titles table.
dr = titles.NewRow();
// Add data to the BookTitle column (column 0 in the row).
dr[0] = "Title " + j.ToString();
// Insert the row into the titles table.
titles.Rows.Add(dr);
}
// Bind the HtmlSelect control to the data source.
Select1.DataSource = ds;
Select1.DataMember = "Titles";
Select1.DataTextField = "BookTitle";
Select1.DataBind();
}
}
function Button_Click (sender : Object, e : EventArgs)
{
// Display the selected items.
Label1.Text = "You selected:";
for (var i : int = 0; i <= Select1.Items.Count - 1; i++)
{
if (Select1.Items[i].Selected)
Label1.Text += "<br> - " + Select1.Items[i].Text;
}
}
</script>
</head>
<body>
<form 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>
Plattformen
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
HtmlSelect-Klasse
HtmlSelect-Member
System.Web.UI.HtmlControls-Namespace
DataSource
DataTextField
DataValueField
ListItem.Text
ListItem.Value