給予翻譯建議
 
其他人的建议:

progress indicator
無其他建議。
按一下以給予評分及指教
MSDN
MSDN Library
.NET 開發
.NET Framework 4
ASP.NET 參考
 資料繫結運算式語法
全部折疊/全部展開 全部折疊
檢視內容: 並排檢視檢視內容: 並排檢視
.NET Framework 4
Data-Binding Expression Syntax

Data-binding expressions create bindings between a server control property and a data source when the DataBind method is called on the page. You can include data-binding expressions on the value side of an attribute/value pair in the opening tag of a server control, or anywhere in the page.

<tagprefix:tagname property="<%# data-binding expression %>"
   runat="server" />
- or -
literal text <%# data-binding expression %>
property

The control property for which this data binding is declared.

data-binding expression

Any expression that conforms to the requirements outlined in the Remarks section.

All data-binding expressions must be contained between <%# and %> characters.

ASP.NET supports a hierarchical data-binding model that creates bindings between server control properties and data sources. Almost any server control property can be bound against any public field or property on the containing page or on the server control's immediate naming container.

Data-binding expressions use the Eval and Bind methods to bind data to controls and submit changes back to the database. The Eval method is a static (read-only) method that takes the value of a data field and returns it as a string. The Bind method supports read/write functionality with the ability to retrieve the values of data-bound controls and submit any changes made back to the database.

You can bind to XML data from an XmlDataSource control using the XPath and XPathSelect methods, as well as the XPathBinder class. For more information, see XmlDataSource Web Server Control Overview.

The following code example demonstrates how you can bind data against properties in an ASP.NET server control. When a user selects a state from the DropDownList Web server control, the Label Web server control is bound against the selected item in the list and displays the selected state.

C#
<html>
<head>
    <script language="C#" runat="server">
        void SubmitBtn_Click(Object sender, EventArgs e) {
          // Rather than explictly pulling out the variable from the StateList control
          // and then manipulating a Label control, just call Page.DataBind.
          // This will evaluate any <%# %> expressions within the page.   
          Page.DataBind();
        }
    </script>
</head>
<body>

    <h3><font face="Verdana">Binding to a property of another server control</font></h3>
    <form runat="server">
        <asp:DropDownList id="StateList" runat="server">
          <asp:ListItem>CA</asp:ListItem>
          <asp:ListItem>IN</asp:ListItem>
          <asp:ListItem>KS</asp:ListItem>
          <asp:ListItem>MD</asp:ListItem>
          <asp:ListItem>MI</asp:ListItem>
          <asp:ListItem>OR</asp:ListItem>
          <asp:ListItem>TN</asp:ListItem>
          <asp:ListItem>UT</asp:ListItem>
        </asp:DropDownList>       
        <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>        
        <p>     
        Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>     
    </form>
</body>
</html>
Visual Basic
<html>
<head>
    <script language="VB" runat="server">
         Sub SubmitBtn_Click(sender As Object, e As EventArgs)
            ' Rather than explictly pulling out the variable from the StateList control
            ' and then manipulating a Label control, just call Page.DataBind.
            ' This will evaluate any <%# %> expressions within the page.   
            Page.DataBind()
         End Sub
    </script>
</head>
<body>

    <h3><font face="Verdana"> Binding to a property of another server control</font></h3>
    <form runat="server">
        <asp:DropDownList id="StateList" runat="server">
          <asp:ListItem>CA</asp:ListItem>
          <asp:ListItem>IN</asp:ListItem>
          <asp:ListItem>KS</asp:ListItem>
          <asp:ListItem>MD</asp:ListItem>
          <asp:ListItem>MI</asp:ListItem>
          <asp:ListItem>OR</asp:ListItem>
          <asp:ListItem>TN</asp:ListItem>
          <asp:ListItem>UT</asp:ListItem>
        </asp:DropDownList>       
        <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>        
        <p>     
        Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>     
    </form>
</body>
</html>
.NET Framework 4
資料繫結運算式語法

在網頁上呼叫 DataBind 方法時,資料繫結運算式便會在伺服控制項屬性和資料來源之間建立繫結。 您可以將資料繫結運算式包含在伺服器控制項開頭標記中屬性/值配組中值的那一端,或是網頁上的任何位置。

<tagprefix:tagname property="<%# data-binding expression %>"
   runat="server" />
- or -
literal text <%# data-binding expression %>
property

對它宣告這個資料繫結的控制項屬性。

data-binding expression

符合<備註>章節中所列出之需求的任何運算式。

所有的資料繫結運算式都必須包含在 <%# 和 %> 字元之間。

ASP.NET 支援階層式資料繫結模型,這種模型會建立伺服器控制項屬性與資料來源之間的繫結。 幾乎任何伺服器控制項屬性都可以與容納它的網頁,或是伺服器控制項之直接命名容器 (Container) 上的任何公用欄位或屬性產生繫結。

資料繫結運算式使用 EvalBind 方法將資料繫結至控制項,並將變更送回資料庫。 Eval 方法是一種靜態 (唯讀) 方法,會取得資料欄位的值,並且將其以字串的形式送回。 Bind 方法支援讀/寫功能,並具有擷取資料繫結控制項之值,以及將所有變更都送回資料庫的能力。

使用 XPathXPathSelect 方法,以及 XPathBinder 類別,即可從 XmlDataSource 控制項繫結至 XML 資料。 如需詳細資訊,請參閱 XmlDataSource Web 伺服器控制項概觀

下列程式碼範例示範如何將資料繫結至 ASP.NET 伺服器控制項中的屬性。 當使用者從 DropDownList Web 伺服器選取某個狀態時,Label Web 伺服器控制項便會繫結至清單中的選取項目,並顯示選取的狀態。

C#
<html>
<head>
    <script language="C#" runat="server">
        void SubmitBtn_Click(Object sender, EventArgs e) {
          // Rather than explictly pulling out the variable from the StateList control
          // and then manipulating a Label control, just call Page.DataBind.
          // This will evaluate any <%# %> expressions within the page.   
          Page.DataBind();
        }
    </script>
</head>
<body>

    <h3><font face="Verdana">Binding to a property of another server control</font></h3>
    <form runat="server">
        <asp:DropDownList id="StateList" runat="server">
          <asp:ListItem>CA</asp:ListItem>
          <asp:ListItem>IN</asp:ListItem>
          <asp:ListItem>KS</asp:ListItem>
          <asp:ListItem>MD</asp:ListItem>
          <asp:ListItem>MI</asp:ListItem>
          <asp:ListItem>OR</asp:ListItem>
          <asp:ListItem>TN</asp:ListItem>
          <asp:ListItem>UT</asp:ListItem>
        </asp:DropDownList>       
        <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>        
        <p>     
        Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>     
    </form>
</body>
</html>
Visual Basic
<html>
<head>
    <script language="VB" runat="server">
         Sub SubmitBtn_Click(sender As Object, e As EventArgs)
            ' Rather than explictly pulling out the variable from the StateList control
            ' and then manipulating a Label control, just call Page.DataBind.
            ' This will evaluate any <%# %> expressions within the page.   
            Page.DataBind()
         End Sub
    </script>
</head>
<body>

    <h3><font face="Verdana"> Binding to a property of another server control</font></h3>
    <form runat="server">
        <asp:DropDownList id="StateList" runat="server">
          <asp:ListItem>CA</asp:ListItem>
          <asp:ListItem>IN</asp:ListItem>
          <asp:ListItem>KS</asp:ListItem>
          <asp:ListItem>MD</asp:ListItem>
          <asp:ListItem>MI</asp:ListItem>
          <asp:ListItem>OR</asp:ListItem>
          <asp:ListItem>TN</asp:ListItem>
          <asp:ListItem>UT</asp:ListItem>
        </asp:DropDownList>       
        <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>        
        <p>     
        Selected State: <asp:label text='<%# StateList.SelectedItem.Text %>' runat="server"/>     
    </form>
</body>
</html>
社群內容   什麼是社群內容?
新增內容 RSS  註解
Processing
© 2012 Microsoft. 著作權所有,並保留一切權利。 使用規定 | 商標 | 隱私權聲明
Page view tracker