Sideway
output.to from Sideway
Draft for Information Only

Content

Data-Binding Expression Syntax
 Syntax
 Parameters
 Remarks
 Data-Binding Examples
   Examples of Visual C# ASP.NET Data-Binding
   Examples of Visual Basic ASP.NET Data-Binding
 See Also
 Reference
 Concepts
 Sources and References

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.

Syntax

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

Parameters

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.

Remarks

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.

Data-Binding Examples

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.

Examples of Visual C# ASP.NET Data-Binding

ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head id="Head1" runat="server">
        <title>Sample Page</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <%@ Page Language="C#"%>
        <script 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 &lt;%# %> expressions within the page.
                Label1.DataBind();
            }
        </script>
    </head>
    <body>
        <h3><font face="Verdana">Binding to a property of another server control</font></h3>
        <form id="Form1" 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 ID="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
            <p>
            Selected State: <asp:label ID="Label1" text='<%# StateList.SelectedItem.Text %>' runat="server"/>
        </form>
    </body>
</html>
HTTP Response Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head id="Head1"><title>
	Sample Page
</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>
    <body>
        <h3><font face="Verdana">Binding to a property of another server control</font></h3>
        <form method="post" action="./webforms_servercontrols_databindingexpressionsyntax_01a_01.aspx" id="Form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEyNDI2ODczNTZkZFV4h7pnNrPfg0bPMy1HKBzIzcnmjEBSOiVW0sOnoIHT" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CFF89F61" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAqFsd/IzXgaVGW648/ILFNIHWIJqmDo1dm/deKji2bRMIKBvxO+5mrJcypBSbYDf2vKvUmv9HdBvzs+VubDzX12YpUHcQIHWcnWtq3OXK/++wQ86Fn/RYj6fy9Ju0uwVRXg0JFwxNfpA/ZAzWXo/o+xqWdYe++/ZivmODInD+eNptXnR8LN6Haxrrg3GYDLeHjN+DvxnwFeFeJ9MIBWR693gpqh/Kfz1unrz9MitE7N98WKSg0NdxRPvc02SCa8SoY=" />
</div>
            <select name="StateList" id="StateList">
	<option value="CA">CA</option>
	<option value="IN">IN</option>
	<option value="KS">KS</option>
	<option value="MD">MD</option>
	<option value="MI">MI</option>
	<option value="OR">OR</option>
	<option value="TN">TN</option>
	<option value="UT">UT</option>

</select>
            <input type="submit" name="Button1" value="Submit" id="Button1" />
            <p>
            Selected State: <span id="Label1"></span>
        </form>
    </body>
</html>
ASPX Web Page Embedded Output:

Examples of Visual Basic ASP.NET Data-Binding

ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head id="Head1" runat="server">
        <title>Sample Page</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <%@ Page Language="vb"%>
        <script 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 &lt;%# %> 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 id="Form1" 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 ID="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
            <p>
            Selected State: <asp:label ID="Label1" text='<%# StateList.SelectedItem.Text %>' runat="server"/>
        </form>
    </body>
</html>
HTTP Response Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head id="Head1"><title>
	Sample Page
</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head>
    <body>
        <h3><font face="Verdana">Binding to a property of another server control</font></h3>
        <form method="post" action="./webforms_servercontrols_databindingexpressionsyntax_01a_02.aspx" id="Form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEyNDI2ODczNTZkZAJS3OjsVb8NMXDFcvE4KvPWb8P+nHBavyfZrZEXoye5" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="5A411EEB" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAoMkn7NVcPq7cfSq2Yp9KqOHWIJqmDo1dm/deKji2bRMIKBvxO+5mrJcypBSbYDf2vKvUmv9HdBvzs+VubDzX12YpUHcQIHWcnWtq3OXK/++wQ86Fn/RYj6fy9Ju0uwVRXg0JFwxNfpA/ZAzWXo/o+xqWdYe++/ZivmODInD+eNptXnR8LN6Haxrrg3GYDLeHjN+DvxnwFeFeJ9MIBWR693JF7wZ9qbHHfbZ704iaCGZ8PjLAQF+cyYvOQLkEUphZA=" />
</div>
            <select name="StateList" id="StateList">
	<option value="CA">CA</option>
	<option value="IN">IN</option>
	<option value="KS">KS</option>
	<option value="MD">MD</option>
	<option value="MI">MI</option>
	<option value="OR">OR</option>
	<option value="TN">TN</option>
	<option value="UT">UT</option>

</select>
            <input type="submit" name="Button1" value="Submit" id="Button1" />
            <p>
            Selected State: <span id="Label1"></span>
        </form>
    </body>
</html>
ASPX Web Page Embedded Output:

See Also

Reference

XPathBinder

Concepts

Sources and References

  • https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/bda9bbfx(v=vs.100)?redirectedfrom=MSDN

©sideway

ID: 211000020 Last Updated: 10/20/2021 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019