HtmlSelect.DataTextField 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定資料來源中要繫結至 Text 控制項之各個項目 HtmlSelect 屬性的欄位。
public:
virtual property System::String ^ DataTextField { System::String ^ get(); void set(System::String ^ value); };
public virtual string DataTextField { get; set; }
member this.DataTextField : string with get, set
Public Overridable Property DataTextField As String
屬性值
資料來源中要繫結至 Text 控制項之各個項目 HtmlSelect 屬性的欄位。 預設值是空字串 (""),表示尚未設定這個屬性。
範例
下列程式碼範例示範如何使用 DataSource 和 DataTextField 屬性來指定資料來源中的欄位,以系結至 ListItem.Text 控制項中 HtmlSelect 每個專案的 屬性。
<%@ 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 /> - " + 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>
<%@ 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 /> - " & 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>
下列程式碼範例示範如何使用 DataSourceID 和 DataTextField 屬性來指定資料來源中的欄位,以系結至 ListItem.Value 控制項中 HtmlSelect 每個專案的 屬性。
<!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>
<!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>
備註
DataTextField使用 屬性可指定資料來源中的哪一個欄位,以系結至 ListItem.Text 控制項中每個專案的 屬性。 這個屬性通常用來提供屬性的值 ListItem.Text ,與 屬性的值 ListItem.Value 不同。
類別 HtmlSelect 提供兩個屬性,指定要系結的資料來源。 屬性 DataSource 可讓您將控制項系結 HtmlSelect 至實作 或 System.ComponentModel.IListSource 介面的任何集合 System.Collections.IEnumerable , (例如 System.Data.DataView 、 System.Collections.ArrayList 或 System.Collections.Generic.List<T>) 。 當您使用 DataSource 屬性來指定資料來源時,您必須明確呼叫 DataBind 方法,將控制項及其 DataTextField 屬性系結至資料來源。
屬性 DataSourceID 可讓您將控制項系結 HtmlSelect 至代表資料來源的資料來源控制項。 當您使用 DataSourceID 屬性來指定資料來源時, HtmlSelect 控制項及其 DataTextField 屬性會自動系結至指定的資料來源控制項。 因此,您不需要明確呼叫 DataBind 方法。