Data-Binding Expressions Overview
Data-binding syntax allows you to bind control property values to data and specify values for retrieving, updating, deleting, and inserting data. z
Data-Binding Syntax
Data-binding expressions are contained within <%# and %> delimiters and use the Eval and Bind functions. The Eval function is used to define one-way (read-only) binding. The Bind function is used for two-way (updatable) binding. In addition to calling Eval and Bind methods to perform data binding in a data-binding expression, you can call any publicly scoped code within the <%# and %> delimiters to execute that code and return a value during page processing.
Data-binding expressions are resolved when the DataBind method of a control or of the
Page class is called. For controls such as the
GridView,
DetailsView, and
FormView controls, data-binding expressions are resolved automatically during the control's PreRender event and you are not required to call the DataBind method explicitly.
A Visual Studio project with source code is available to accompany this topic:
Download.
Examples
The following code example shows the use of data-binding expressions with a
FormView control in an
ItemTemplate.
Examples of ASP.NET C# page
Examples of ASP.NET C# page
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">
private class Record
{
public string ProductID { get; set; }
public string ProductName { get; set; }
public string CategoryID { get; set; }
public string QuantityPerUnit { get; set; }
public string UnitPrice { get; set; }
}
void Page_Load()
{
var list = new List<Record>();
list.Add(new Record()
{
ProductID = "001",
ProductName = "Adjustable Race",
CategoryID = "AR-5381",
QuantityPerUnit = "4",
UnitPrice = "5" });
list.Add(new Record()
{
ProductID = "002",
ProductName = "Bearing Ball",
CategoryID = "BA-8327",
QuantityPerUnit = "102",
UnitPrice = "15" });
list.Add(new Record()
{
ProductID = "003",
ProductName = "BB Ball Bearing",
CategoryID = "BE-2349",
QuantityPerUnit = "1",
UnitPrice = "25" });
list.Add(new Record()
{
ProductID = "001",
ProductName = "Headset Ball Bearings",
CategoryID = "BE-2908",
QuantityPerUnit = "7",
UnitPrice = "51" });
FormView1.DataSource = list;
FormView1.DataBind();
}
</script>
</head>
<body>
<%Response.Write("<p>Results on "+ Request.ServerVariables["SERVER_SOFTWARE"] + " .net: " + System.Environment.Version + "</p>");%>
<form id="form1" runat="server">
<h3>Data-Binding Expression Example</h3>
<asp:FormView ID="FormView1"
DataKeyNames="ProductID"
RunAt="server">
<ItemTemplate>
<table>
<tr>
<td align="right"><b>Product ID:</b></td>
<td><%# Eval("ProductID") %></td>
</tr>
<tr>
<td align="right"><b>Product Name:</b></td>
<td><%# Eval("ProductName") %></td>
</tr>
<tr>
<td align="right"><b>Category ID:</b></td>
<td><%# Eval("CategoryID") %></td>
</tr>
<tr>
<td align="right"><b>Quantity Per Unit:</b></td>
<td><%# Eval("QuantityPerUnit") %></td>
</tr>
<tr>
<td align="right"><b>Unit Price:</b></td>
<td><%# Eval("UnitPrice") %></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
</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>
<p>Results on Microsoft-IIS/8.5 .net: 4.0.30319.42000</p><form method="post" action="./aspnet_comp_webforms_servercontrols_databindingexpressionoverview_01a_01.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="eBY4UiVMtUbX6OTGyqDa1FCjKQWUs3qj/PKf23KUEzWehskTvjstXgSHtvRj/CMOYUsNcO94plWEr0U65TodImynE7CvRrYJK5J0ZtCll7x6Xd03fEh+WSn8cjJpy52kdhHim717J4ohO1nleNj9Aj8qvQ7tVi0fo5VH+0aSsHK1+A/Wlm9+5X/cVQ4BT+RVeV1DXORTVEqH7b2DCtROC5rhpLxKuMfh4/c9O+wrSqzR4rFPgKgEDnRZ3WJi3BA3v31QfHM+/fiwomEGnhlGGkBfqj7iMQQ0uh1xyA50HHD6XRzHQ4wbIhXjtlQYO3CvH9li2PJGxQDvBo7OxwPw5ouXURw3481ZDZzUEiJv8js=" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="B4BD2EC4" />
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
</div>
<h3>Data-Binding Expression Example</h3>
<table cellspacing="0" id="FormView1" style="border-collapse:collapse;">
<tr>
<td colspan="2">
<table>
<tr>
<td align="right"><b>Product ID:</b></td>
<td>001</td>
</tr>
<tr>
<td align="right"><b>Product Name:</b></td>
<td>Adjustable Race</td>
</tr>
<tr>
<td align="right"><b>Category ID:</b></td>
<td>AR-5381</td>
</tr>
<tr>
<td align="right"><b>Quantity Per Unit:</b></td>
<td>4</td>
</tr>
<tr>
<td align="right"><b>Unit Price:</b></td>
<td>5</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
HTML Web Page Embedded Output:
Examples of ASP.NET VB page
Examples of ASP.NET VB page
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" >
Private Class Record
Public Property ProductID
Public Property ProductName
Public Property CategoryID
Public Property QuantityPerUnit
Public Property UnitPrice
End Class
Sub Page_Load()
Dim list As new List(Of Record)()
list.Add(new Record() With { _
.ProductID = "001", _
.ProductName = "Adjustable Race", _
.CategoryID = "AR-5381", _
.QuantityPerUnit = "4", _
.UnitPrice = "5" })
list.Add(new Record() With { _
.ProductID = "002", _
.ProductName = "Bearing Ball", _
.CategoryID = "BA-8327", _
.QuantityPerUnit = "102", _
.UnitPrice = "15"})
list.Add(new Record() With { _
.ProductID = "003", _
.ProductName = "BB Ball Bearing", _
.CategoryID = "BE-2349", _
.QuantityPerUnit = "1", _
.UnitPrice = "25"})
FormView1.DataSource = list
FormView1.DataBind()
End Sub
</script>
</head>
<body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
<form id="form1" runat="server">
<h3>Data-Binding Expression Example</h3>
<asp:FormView ID="FormView1"
DataKeyNames="ProductID"
RunAt="server">
<ItemTemplate>
<table>
<tr>
<td align="right"><b>Product ID:</b></td>
<td><%# Eval("ProductID") %></td>
</tr>
<tr>
<td align="right"><b>Product Name:</b></td>
<td><%# Eval("ProductName") %></td>
</tr>
<tr>
<td align="right"><b>Category ID:</b></td>
<td><%# Eval("CategoryID") %></td>
</tr>
<tr>
<td align="right"><b>Quantity Per Unit:</b></td>
<td><%# Eval("QuantityPerUnit") %></td>
</tr>
<tr>
<td align="right"><b>Unit Price:</b></td>
<td><%# Eval("UnitPrice") %></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
</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>
<p>Results on Microsoft-IIS/8.5 .net: 4.0.30319.42000 VB Version 14.0</p><form method="post" action="./aspnet_comp_webforms_servercontrols_databindingexpressionoverview_01a_02.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="RoJFqcNnMWPNP82aEFwpxHAU7rVEvNXRE/oaj+GO+XiHUqJANARn6WijvYUIMwqyHGQL+3vAOLMZRzNr51noaRr0cquMqXbXkoHv/BZr3ap8wbHETpDb0cle4M98gPuZpd8b3LUZmWIYSM0P5ILBMqcG3F6RmBySyknpW+G/4Z1e08bOVDEizBrXuW/jgVDtXGm4tbzQffPg/+/8MKHMXZtlDCWzsr+F0/jAB9BWjP7ziSrFxugGVGv/wLP+7mw+BsQXRM4mXp6w2ojOAjGn8XSbvuWfONtlK9vXVqkA1t1Cccuzks9ZWikfz0UaxuQJ+rRcKU+HgkD4Q4xWwA15ghhZy244lW6wdzBY0p47PGQ=" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="D2F39962" />
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
</div>
<h3>Data-Binding Expression Example</h3>
<table cellspacing="0" id="FormView1" style="border-collapse:collapse;">
<tr>
<td colspan="2">
<table>
<tr>
<td align="right"><b>Product ID:</b></td>
<td>001</td>
</tr>
<tr>
<td align="right"><b>Product Name:</b></td>
<td>Adjustable Race</td>
</tr>
<tr>
<td align="right"><b>Category ID:</b></td>
<td>AR-5381</td>
</tr>
<tr>
<td align="right"><b>Quantity Per Unit:</b></td>
<td>4</td>
</tr>
<tr>
<td align="right"><b>Unit Price:</b></td>
<td>5</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
HTML Web Page Embedded Output:
Using the Eval Method
The Eval method evaluates late-bound data expressions in the templates of data-bound controls such as the
GridView,
DetailsView, and
FormView controls. At run time, the Eval method calls the
Eval method of the
DataBinder object, referencing the current data item of the naming container. The naming container is generally the smallest part of the data-bound control that contains a whole record, such as a row in a
GridView control. You can therefore use the Eval method only for binding inside templates of a data-bound control.
The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. You can supply an optional second parameter to specify a format for the returned string. The string format parameter uses the syntax defined for the
Format method of the
String class.
Examples
Examples of ASP.NET C# page
Examples of ASP.NET C# page
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">
private class Record
{
public string ProductID { get; set; }
public string ProductName { get; set; }
}
void Page_Load()
{
var list = new List<Record>();
list.Add(new Record()
{
ProductID = "001",
ProductName = "Adjustable Race"});
list.Add(new Record()
{
ProductID = "002",
ProductName = "Bearing Ball"});
FormView1.DataSource = list;
FormView1.DataBind();
String[] myStringArray = new String[] {"one","two","three"};
rptr.DataSource = myStringArray;
rptr.DataBind();
}
</script>
</head>
<body>
<%Response.Write("<p>Results on "+ Request.ServerVariables["SERVER_SOFTWARE"] + " .net: " + System.Environment.Version + "</p>");%>
<form id="form1" runat="server">
<h3>Data-Binding Eval Example</h3>
<asp:FormView ID="FormView1"
DataKeyNames="ProductID"
RunAt="server">
<ItemTemplate>
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductID") %></td>
</tr>
<tr>
<td align="right">[Eval("ProductName")]<br><b>Product Name:</b></td>
<td><%# Eval("ProductName") %></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<h5>DataBinder.Eval(Container, "DataItem"):</h5>
<asp:repeater id=rptr runat="server">
<itemtemplate>Container.DataItem: <%# DataBinder.Eval(Container, "DataItem") %><br></itemtemplate>
</asp:repeater>
</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>
<p>Results on Microsoft-IIS/8.5 .net: 4.0.30319.42000</p><form method="post" action="./aspnet_comp_webforms_servercontrols_databindingexpressionoverview_01a_03.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="P/u+08lEKz58/NGxw1mQ7Zkzetoykh+5hah88fmAS1fw5IhfRBtn5ltzX+V9R5CJftCNz6XenV31Fp/s67ke+EUKMTj5XgDY9ByctBpSt8X9iGuPE2LlFIXqccg4cGSmF7/OXCVZaxr3LpYXZLYGMSOoClWxfZeRgZRWts8LLu0hV7+PEzHj1o5BuJYX2w+26wgYK0n80sO+FuxqTiBW/PmvxxCaAjJ4I1GlZ/6behnwogqe8ISKxjExq6/i5XrQe2p1U7M+31RHJhYytz5rJq6YzODfvMFg7K3LLCceKpgDgeylKfYkcuqloN4VuK02GLRSeeiWHLqIsOWWPy/SfOzBPHYwcwVEk6ZmtzeVEkPwfImkAfkZ0xxfsiUSuS2YklyoQEFIE5+XVCK/V1EBXaBT9WsP/8R2HOgkgD9Gsss=" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="EB4C8DD8" />
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
</div>
<h3>Data-Binding Eval Example</h3>
<table cellspacing="0" id="FormView1" style="border-collapse:collapse;">
<tr>
<td colspan="2">
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td>001</td>
</tr>
<tr>
<td align="right">[Eval("ProductName")]<br><b>Product Name:</b></td>
<td>Adjustable Race</td>
</tr>
</table>
</td>
</tr>
</table>
<h5>DataBinder.Eval(Container, "DataItem"):</h5>
Container.DataItem: one<br>Container.DataItem: two<br>Container.DataItem: three<br>
</form>
</body>
</html>
HTML Web Page Embedded Output:
Examples of ASP.NET VB page
Examples of ASP.NET VB page
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" >
Private Class Record
Public Property ProductID
Public Property ProductName
End Class
Sub Page_Load()
Dim list As new List(Of Record)()
list.Add(new Record() With { _
.ProductID = "001", _
.ProductName = "Adjustable Race" })
list.Add(new Record() With { _
.ProductID = "002", _
.ProductName = "Bearing Ball"})
FormView1.DataSource = list
FormView1.DataBind()
Dim myStringArray as String()
myStringArray = new String() {"one","two","three"}
rptr.DataSource = myStringArray
rptr.DataBind()
End Sub
</script>
</head>
<body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
<form id="form1" runat="server">
<h3>Data-Binding Eval Example</h3>
<asp:FormView ID="FormView1"
DataKeyNames="ProductID"
RunAt="server">
<ItemTemplate>
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductID") %></td>
</tr>
<tr>
<td align="right">[Eval("ProductName")]<br><b>Product Name:</b></td>
<td><%# Eval("ProductName") %></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<h5>DataBinder.Eval(Container, "DataItem"):</h5>
<asp:repeater id=rptr runat="server">
<itemtemplate>Container.DataItem: <%# DataBinder.Eval(Container, "DataItem") %><br></itemtemplate>
</asp:repeater>
</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>
<p>Results on Microsoft-IIS/8.5 .net: 4.0.30319.42000 VB Version 14.0</p><form method="post" action="./aspnet_comp_webforms_servercontrols_databindingexpressionoverview_01a_04.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="az7gjwi4lbO5tRmY5B1qx4ioA8uQY1ssI2+5R9xjQc/GJqNwJy36CpS78jaiKmHOgRPNmA+b2Xx7Fp7O/unPhRca1z0E3WoncMUi9jhpqyz+HOIZvACKOV5Ya4nTC+CNI0U8xk8ghDDClPBHJA37KJJV+F+Xexq1a82WIjKHiUXaXsL+DbZKVZiIfJFXffmZXQF/4gVkElb7EuD5Cq5K9Z+wN5G2Qyw9u5TPKlTINeClomiHq4kws33SqO+CYBGvFI/IeC/yD0gwZzvBIyoQck7PaYBZpHqBpRO4HbGRd7I5cHvwUvk8CGtx90wO7z29FGPIY9GFQ//prSFmu8iYjs2e03O5sL5edb4NhiVeAtkLfDOEih5rxVUyHL8ylzoloNpDVDPWykDI2mohDxuMJBcc84ASaA+0+s3KjcQRHsE=" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="8690EB86" />
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
</div>
<h3>Data-Binding Eval Example</h3>
<table cellspacing="0" id="FormView1" style="border-collapse:collapse;">
<tr>
<td colspan="2">
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td>001</td>
</tr>
<tr>
<td align="right">[Eval("ProductName")]<br><b>Product Name:</b></td>
<td>Adjustable Race</td>
</tr>
</table>
</td>
</tr>
</table>
<h5>DataBinder.Eval(Container, "DataItem"):</h5>
Container.DataItem: one<br>Container.DataItem: two<br>Container.DataItem: three<br>
</form>
</body>
</html>
HTML Web Page Embedded Output:
Using the Bind Method
The Bind method has some similarities to the Eval method, but there are significant differences. Although you can retrieve the values of data-bound fields with the Bind method, as you can with the Eval method, the Bind method is also used when data can be modified.
In ASP.NET, data-bound controls such as the
GridView,
DetailsView, and
FormView controls can automatically use the update, delete, and insert operations of a data source control. For example, if you have defined SQL Select, Insert, Delete, and Update statements for your data source control, using Bind in a
GridView,
DetailsView, or
FormView control template enables the control to extract values from child controls in the template and pass them to the data source control. The data source control in turn performs the appropriate command for the database. For this reason, the Bind function is used inside the EditItemTemplate or InsertItemTemplate of a data-bound control.
The Bind method is typically used with input controls such as the TextBox control rendered by a GridView row in edit mode. When the data-bound control creates these input controls as part of its own rendering, it can extract the input values.
The Bind method takes the name of a data field to associate with the bound property, as shown in the following example:
Security note
Security Note:
This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see
Script Exploits Overview.
Examples
Examples of ASP.NET C# page
Examples of ASP.NET C# page
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">
public void Page_Init(object o, EventArgs e)
{
ObjectDataSourceProducts.TypeName = this.GetType().AssemblyQualifiedName;
}
public class MyDataAccess
{
public static List<Product> GetProducts()
{
List<Product> results = new List<Product>()
{
new Product(){ ProductID=1, ProductName="Product One" },
new Product(){ ProductID=2, ProductName="Product Two" }
};
return results;
}
public static void SaveProduct(Product e)
{
// Persist Product e to the DB/cache etc. here
}
}
public static List<Product> GetProducts()
{
List<Product> results = new List<Product>()
{
new Product(){ ProductID=1, ProductName="Product One" },
new Product(){ ProductID=2, ProductName="Product Two" }
};
return results;
}
public class Product
{
public Int32 ProductID { get; set; }
public string ProductName { get; set; }
}
private class Record
{
public string ProductID { get; set; }
public string ProductDesc { get; set; }
}
void Page_Load()
{
var list = new List<Record>();
list.Add(new Record()
{
ProductID = "1",
ProductDesc = "Adjustable Race"});
list.Add(new Record()
{
ProductID = "2",
ProductDesc = "Bearing Ball"});
FormView1.DataSource = list;
FormView1.DataBind();
}
</script>
</head>
<body>
<%Response.Write("<p>Results on "+ Request.ServerVariables["SERVER_SOFTWARE"] + " .net: " + System.Environment.Version + "</p>");%>
<form id="form1" runat="server">
<asp:FormView ID="FormView1"
DataKeyNames="ProductID"
RunAt="server">
<ItemTemplate>
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductID") %></td>
</tr>
<tr>
<td align="right">Bind("ProductDesc")<br><asp:CompareValidator ID="ValueValidator1" runat="server" ErrorMessage="can input 'Adjustable Race' only..." ControlToValidate="EditProductDescTextBox" ValueToCompare="Adjustable Race" /> <b>Product Desc:</b></td>
<td><asp:TextBox ID="EditProductDescTextBox" RunAt="Server"
Text='<%# Bind("ProductDesc") %>' />
</td>
</tr>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductDesc")]<br><b>Product Desc:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductDesc") %></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSourceProducts" runat="server"
TypeName=""
DataObjectTypeName=""
SelectMethod="GetProducts"
UpdateMethod="SaveProduct"
InsertMethod="SaveProduct">
</asp:ObjectDataSource>
</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>
<p>Results on Microsoft-IIS/8.5 .net: 4.0.30319.42000</p><form method="post" action="./aspnet_comp_webforms_servercontrols_databindingexpressionoverview_01a_05.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="xCrQh3H7Ztfer6F/oipGvTtGuS8zT2r10OoHHOS1QZMBttGZbANq//mT+WUK1OCyMAjJuGk3rsn9s6y5BTSlp/hXwIEmMo/78tbFvesLxC1J9izzoKHjIOcrwUAUdWcJodwfipKdSuevIzkj4W28Di/gEDuD5NStw0xc6VIceEB0iLI1kSeOG0m6EIDPreZJ7H5a9Lt/8MbZNF5vEfQT1K3J/Ae4yC/PzIM6JTEsD1NszWojGrz4oHJj9sRqH3t0w58ZgArMXj3ZxmC2r0Rwmlonys07EQG0NgRcmHGElD0mcKZcHXhaZQe5R4iaaDIokF9P7iLYCP3Dq+qY8LCa80/gMuK97ns/bVaEYil2dzc=" />
</div>
<script src="/sideway/WebResource.axd?d=KTG0uDevdKWz2OmyVNVBYJQUfGzYPcBkQtffgbcLMp-XUh_hG-KITZAyKQSMEEO5V_FNrBJTWz4n-JMWOTVY6pSHvvSjU3dqLkZyNqqMjuM1&t=637688112097945149" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</script>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="1D7830FC" />
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="ROMt8q504+rIK4lVC22fAiqKvT5wpjzz0o6RWNy0l6LB4F7n+skPnozh6DJ8XoaAXraN3XcrbuNMuqTOheYPMWyCvG4gOXqZBbW3Z224p7DKRyFdtkQnwOaIkMzL0z8juZJFUSMCe8n8oDy6mgv8Pg==" />
</div>
<table cellspacing="0" id="FormView1" style="border-collapse:collapse;">
<tr>
<td colspan="2">
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td>1</td>
</tr>
<tr>
<td align="right">Bind("ProductDesc")<br><span id="FormView1_ValueValidator1" style="visibility:hidden;">can input 'Adjustable Race' only...</span> <b>Product Desc:</b></td>
<td><input name="FormView1$EditProductDescTextBox" type="text" value="Adjustable Race" id="FormView1_EditProductDescTextBox" />
</td>
</tr>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductDesc")]<br><b>Product Desc:</b></td>
<td>Adjustable Race</td>
</tr>
</table>
</td>
</tr>
</table>
<script type="text/javascript">
//<![CDATA[
var Page_Validators = new Array(document.getElementById("FormView1_ValueValidator1"));
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
var FormView1_ValueValidator1 = document.all ? document.all["FormView1_ValueValidator1"] : document.getElementById("FormView1_ValueValidator1");
FormView1_ValueValidator1.controltovalidate = "FormView1_EditProductDescTextBox";
FormView1_ValueValidator1.errormessage = "can input \'Adjustable Race\' only...";
FormView1_ValueValidator1.evaluationfunction = "CompareValidatorEvaluateIsValid";
FormView1_ValueValidator1.valuetocompare = "Adjustable Race";
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}
function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
//]]>
</script>
</form>
</body>
</html>
HTML Web Page Embedded Output:
Examples of ASP.NET VB page
Examples of ASP.NET VB page
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" >
public Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
ObjectDataSourceProducts.TypeName = sender.GetType().AssemblyQualifiedName
End Sub
Public Class Product
Public Property ProductID As Int32
Public Property ProductName As String
End Class
Public Shared Function GetProducts() As List(Of Product)
Dim Products As New List(Of Product)()
Products.Add(New Product() With { _
.ProductID = "1", _
.ProductName = "Product One"})
Products.Add(New Product() With { _
.ProductID = "2", _
.ProductName = "Product Two"})
Return Products
End Function
Private Class Record
Public Property ProductID
Public Property ProductDesc
End Class
Sub Page_Load()
Dim list As new List(Of Record)()
list.Add(new Record() With { _
.ProductID = "1", _
.ProductDesc = "Adjustable Race" })
list.Add(new Record() With { _
.ProductID = "2", _
.ProductDesc = "Bearing Ball"})
FormView1.DataSource = list
FormView1.DataBind()
End Sub
</script>
</head>
<body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
<form id="form1" runat="server">
<asp:FormView ID="FormView1"
DataKeyNames="ProductID"
RunAt="server">
<ItemTemplate>
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductID") %></td>
</tr>
<tr>
<td align="right">Bind("ProductDesc")<br><asp:CompareValidator ID="ValueValidator1" runat="server" ErrorMessage="can input 'Adjustable Race' only..." ControlToValidate="EditProductDescTextBox" ValueToCompare="Adjustable Race" /> <b>Product Desc:</b></td>
<td><asp:TextBox ID="EditProductDescTextBox" RunAt="Server"
Text='<%# Bind("ProductDesc") %>' />
</td>
</tr>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductDesc")]<br><b>Product Desc:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductDesc") %></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSourceProducts" runat="server"
TypeName=""
DataObjectTypeName=""
SelectMethod="GetProducts"
UpdateMethod="SaveProduct"
InsertMethod="SaveProduct">
</asp:ObjectDataSource>
</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>
<p>Results on Microsoft-IIS/8.5 .net: 4.0.30319.42000 VB Version 14.0</p><form method="post" action="./aspnet_comp_webforms_servercontrols_databindingexpressionoverview_01a_06.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="sboSue5YiD4FEXMGiF+GSu2+14mo7r08oCZ8NCRpvj21eDb5NaaSpt5iqAwn8nDjyHLP/Mp0gTAODLNSePhy1YIlOATcL+O1bJcetNWavLWj3u2GVIJvxRtRqilKKQ4fD70NceY82p1v1FbztNXcFMLYoiLWW6UGtQ9h6PPmfTqY9539j/5Q2ZVGPP/T1xeZ+Hu3tw8AdcY6QEGV0QXEunu4HIWRpYIJ9s4vBGfpMWD5bX8ckhxmnnd7dVCLIAAta2fhS7sxBilndu84e8f09dgKH/ndUUR+fc16DBVQdUBW25bVzeJYlaMMjBE6ipaQXp80UpsctMMGGeV3kUAJCx2ZWDjes8itJrAUFIQZnqA=" />
</div>
<script src="/sideway/WebResource.axd?d=KTG0uDevdKWz2OmyVNVBYJQUfGzYPcBkQtffgbcLMp-XUh_hG-KITZAyKQSMEEO5V_FNrBJTWz4n-JMWOTVY6pSHvvSjU3dqLkZyNqqMjuM1&t=637688112097945149" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</script>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="29B4809A" />
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="J1Luad0YaECqJ+YsmjkHmY5rznbxhxaAO5e4wf1H0PKdzfnhfKHGTwaNQbNvFlIwqFD080pu7LZZIj8PUa7Agd/erNZINg5m1NWm38mXp2miAVAi3uAlBHdPdcr03eEZftfc+xNHaW3WkEnXXA5a8w==" />
</div>
<table cellspacing="0" id="FormView1" style="border-collapse:collapse;">
<tr>
<td colspan="2">
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td>1</td>
</tr>
<tr>
<td align="right">Bind("ProductDesc")<br><span id="FormView1_ValueValidator1" style="visibility:hidden;">can input 'Adjustable Race' only...</span> <b>Product Desc:</b></td>
<td><input name="FormView1$EditProductDescTextBox" type="text" value="Adjustable Race" id="FormView1_EditProductDescTextBox" />
</td>
</tr>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductDesc")]<br><b>Product Desc:</b></td>
<td>Adjustable Race</td>
</tr>
</table>
</td>
</tr>
</table>
<script type="text/javascript">
//<![CDATA[
var Page_Validators = new Array(document.getElementById("FormView1_ValueValidator1"));
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
var FormView1_ValueValidator1 = document.all ? document.all["FormView1_ValueValidator1"] : document.getElementById("FormView1_ValueValidator1");
FormView1_ValueValidator1.controltovalidate = "FormView1_EditProductDescTextBox";
FormView1_ValueValidator1.errormessage = "can input \'Adjustable Race\' only...";
FormView1_ValueValidator1.evaluationfunction = "CompareValidatorEvaluateIsValid";
FormView1_ValueValidator1.valuetocompare = "Adjustable Race";
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}
function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
//]]>
</script>
</form>
</body>
</html>
HTML Web Page Embedded Output:
When the Update button for the row is clicked, the values of each control property bound using Bind syntax are extracted and passed to the data source control for the update operation.
Calling the DataBind Method Explicitly
Controls such as
GridView,
FormView, and
DetailsView controls perform binding by calling the DataBind method implicitly when they are bound to a data source control using the
DataSourceID property. However, there are situations in which you need to call the DataBind method explicitly.
One situation is if you have bound a control to a data source control using the
DataSource property instead of the
DataSourceID property. In that case, you need to call the DataBind method explicitly to perform data binding and resolve data-binding expressions.
Another situation is if you need to manually refresh the data in a data-bound control. Consider a page where you have two controls that display information from the same database (perhaps using different views). In that case, you might need to explicitly re-bind the control to data to keep the data display synchronized. For example, you might have a
GridView control displaying a list of products and a
DetailsView control that allows users to edit an individual product. Although the
GridView and
DetailsView controls display data from the same source, they are bound to different data source controls because they use different queries to get their data. A user might update a record using the
DetailsView control, causing the update to be performed by the associated data source control. However, because the
GridView control is bound to a different data source control, it will display old record values until the page is refreshed. Therefore, after the data is updated by the
DetailsView control, you can call the
DataBind method. This causes the
GridView control to update its view as well by re-executing any data-binding expressions and publicly scoped code within the <%# and %> delimiters. As a result, the
GridView control reflects the update made by the
DetailsView control.
Using Binding for a Lookup Table
A common scenario with data-bound controls is to enable users to update or insert a value by selecting it from a lookup table using a
DropDownList control or other list control. In that case, the lookup control is bound to a separate data source that returns the list of possible values, and the lookup control's selected value is bound to a field in the parent data-bound row.
You can add this functionality as follows. First, for the lookup control, you add a list control (a
DropDownList or
ListBox control) to a template in a data-bound control such as a
GridView,
DetailsView, or
FormView control. You bind the
SelectedValue property of the lookup control to the related field in the container control's data source. Then you set the lookup control's DataSourceID property to a data source control that retrieves the lookup values. You set the lookup control's
DataTextField property to the field from the lookup table that contains the values to display, and set its
DataValueField property to the field from the lookup table that contains the unique identifier for the lookup value, if applicable.
The following code example shows a
DropDownList control that is included in the
InsertItemTemplate template of a
FormView control (this could also be an
InsertItemTemplate template of a
TemplateField included in the
Fields property of a
DetailsView control or the Columns property of a
GridView control). The
SelectedValue property of the
DropDownList control uses the Bind method for two-way binding to the CategoryID field of the current row for the
FormView control. The
DataSourceID property of the
DropDownList control is set to a separate data source control that retrieves the list of possible category names and IDs. The
DataTextField property of the
DropDownList control is set to the CategoryName field from the lookup data source so that a list of possible category names is displayed. The
DataValueField property of the
DropDownList control is set to the CategoryID field from the lookup data source for the related category name. When a user selects a category name from the list, the
SelectedValue property of the
DropDownList control is set to the category ID for the selected category name.
Examples
Examples of ASP.NET C# page
Examples of ASP.NET C# page
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">
public void Page_Init(object o, EventArgs e)
{
ObjectDataSourceProducts.TypeName = this.GetType().AssemblyQualifiedName;
}
public class MyDataAccess
{
public static List<Product> GetProducts()
{
List<Product> results = new List<Product>()
{
new Product(){ ProductID=1, ProductName="Product One" },
new Product(){ ProductID=2, ProductName="Product Two" }
};
return results;
}
public static void SaveProduct(Product e)
{
// Persist Product e to the DB/cache etc. here
}
}
public static List<Product> GetProducts()
{
List<Product> results = new List<Product>()
{
new Product(){ ProductID=1, ProductName="Product One" },
new Product(){ ProductID=2, ProductName="Product Two" }
};
return results;
}
public class Product
{
public Int32 ProductID { get; set; }
public string ProductName { get; set; }
}
private class Record
{
public string ProductID { get; set; }
public string ProductDesc { get; set; }
}
void Page_Load()
{
var list = new List<Record>();
list.Add(new Record()
{
ProductID = "1",
ProductDesc = "Adjustable Race"});
list.Add(new Record()
{
ProductID = "2",
ProductDesc = "Bearing Ball"});
FormView1.DataSource = list;
FormView1.DataBind();
}
</script>
</head>
<body>
<%Response.Write("<p>Results on "+ Request.ServerVariables["SERVER_SOFTWARE"] + " .net: " + System.Environment.Version + "</p>");%>
<form id="form1" runat="server">
<asp:FormView ID="FormView1"
DataKeyNames="ProductID"
RunAt="server">
<ItemTemplate>
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductID") %></td>
</tr>
<tr>
<td align="right">[DataSourceID="ObjectDataSourceProducts";Bind("ProductID")]<br><b>Product Name:</b></td>
<td><asp:DropDownList ID="InsertCategoryDropDownList"
SelectedValue='<%# Bind("ProductID") %>'
DataSourceID="ObjectDataSourceProducts"
DataTextField="ProductName"
DataValueField="ProductID"
RunAt="Server" />
</td>
</tr>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductDesc")]<br><b>Product Desc:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductDesc") %></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<asp:GridView ID="grdCustomers" runat="server" DataSourceID="ObjectDataSourceProducts">
<PagerStyle HorizontalAlign="Left" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSourceProducts" runat="server"
TypeName=""
DataObjectTypeName=""
SelectMethod="GetProducts"
UpdateMethod="SaveProduct"
InsertMethod="SaveProduct">
</asp:ObjectDataSource>
</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>
<p>Results on Microsoft-IIS/8.5 .net: 4.0.30319.42000</p><form method="post" action="./aspnet_comp_webforms_servercontrols_databindingexpressionoverview_01a_07.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="642t2xNqI0ZGd2q6FzGC3+reURuXDaGBOron0i/c4CnWV4aZ67b2HqRwcqpqQFcUGbbmisz7sh2wK0wDrsInrS09ZMgiBTQY/3Kpd8q/sDKNnxjrjhw1hB2fazqPrN8tTbhIP4cXD59qnEgNNBq6XIvza8VkbKK/KdIXMKgL7UQBs11Aw8DNhkpwKaZYwdjoMu9Y8sgj4F3PDuv3VPPBCfUTlOZO+KraJjm7tWFjvnlJWN1BBGUwjCqtiOttZL9ADEctJVMcxtkAWO91tU268RvpVP2B1w9h43ZqCu2Fhfc9K7LUJC695QkhH3p808JFkgrTwSLNIw5p2Euwp1FqmbZ2ZPDjC4TwOO7pof/cJhUvxREJL/Vz+anGM7+zFeaIV2xu5usHcaTQxIoiz8gt6EbhsO7DFQdYEi3NZIdry/j9pozD5TCh9gyoASIR02AiOsCUlOLo2lSthR0vOIEkDoLH55bF4ssEo2gizCXaq34Tc42IZ02L9spqbaRaKMw6Z8zwOYM4rwm1RHDhu75VK7LzdJ2aOpPsQFNH5U7HzVbIeSwrT0f6NbVCbV3HY7UpQXJ7/af1FpAUWndsRx/N4y/Xbs0bg+3iD/vz8wwK5LU4piVo9eGvg/Vz62/T/wNYjMXXd7E06anGJYr9eDH7o1Qd1omHx+4UU4cMDvPKEbJs1pInB0VzyT9nUqyk3qLJGQn+J9zyM0UBQiKHbosG53c0C4pR4/FOjmi/4qNTSZyVv0KDIVlG4NDeFzOSvCZB" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="C09BC610" />
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="xEBgkwP3ir4rQKJw7uO7NKR0PB/+INrbQVGVPoh4k0FYbRObKao2sN1/DxZ1nenl/iUaiu4wwZMUNSQTTKnF7rsqEbDpiVPxN6KhLnkthFJeYHwdSsiNAQIoRHOaTo/l8QxKSOFhbnLs39+YQnWspodMbmr/H7soToFP4/qK4uk=" />
</div>
<table cellspacing="0" id="FormView1" style="border-collapse:collapse;">
<tr>
<td colspan="2">
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td>1</td>
</tr>
<tr>
<td align="right">[DataSourceID="ObjectDataSourceProducts";Bind("ProductID")]<br><b>Product Name:</b></td>
<td><select name="FormView1$InsertCategoryDropDownList" id="FormView1_InsertCategoryDropDownList">
<option selected="selected" value="1">Product One</option>
<option value="2">Product Two</option>
</select>
</td>
</tr>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductDesc")]<br><b>Product Desc:</b></td>
<td>Adjustable Race</td>
</tr>
</table>
</td>
</tr>
</table>
<div>
<table cellspacing="0" rules="all" border="1" id="grdCustomers" style="border-collapse:collapse;">
<tr>
<th scope="col">ProductID</th><th scope="col">ProductName</th>
</tr><tr>
<td>1</td><td>Product One</td>
</tr><tr>
<td>2</td><td>Product Two</td>
</tr>
</table>
</div>
</form>
</body>
</html>
HTML Web Page Embedded Output:
Examples of ASP.NET VB page
Examples of ASP.NET VB page
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" >
public Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
ObjectDataSourceProducts.TypeName = sender.GetType().AssemblyQualifiedName
End Sub
Public Class Product
Public Property ProductID As Int32
Public Property ProductName As String
End Class
Public Shared Function GetProducts() As List(Of Product)
Dim Products As New List(Of Product)()
Products.Add(New Product() With { _
.ProductID = "1", _
.ProductName = "Product One"})
Products.Add(New Product() With { _
.ProductID = "2", _
.ProductName = "Product Two"})
Return Products
End Function
Private Class Record
Public Property ProductID
Public Property ProductDesc
End Class
Sub Page_Load()
Dim list As new List(Of Record)()
list.Add(new Record() With { _
.ProductID = "1", _
.ProductDesc = "Adjustable Race" })
list.Add(new Record() With { _
.ProductID = "2", _
.ProductDesc = "Bearing Ball"})
FormView1.DataSource = list
FormView1.DataBind()
End Sub
</script>
</head>
<body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
<form id="form1" runat="server">
<asp:FormView ID="FormView1"
DataKeyNames="ProductID"
RunAt="server">
<ItemTemplate>
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductID") %></td>
</tr>
<tr>
<td align="right">[DataSourceID="ObjectDataSourceProducts";Bind("ProductID")]<br><b>Product Name:</b></td>
<td><asp:DropDownList ID="InsertCategoryDropDownList"
SelectedValue='<%# Bind("ProductID") %>'
DataSourceID="ObjectDataSourceProducts"
DataTextField="ProductName"
DataValueField="ProductID"
RunAt="Server" />
</td>
</tr>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductDesc")]<br><b>Product Desc:</b></td>
<td><%# DataBinder.Eval(Container.DataItem, "ProductDesc") %></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<asp:GridView ID="grdCustomers" runat="server" DataSourceID="ObjectDataSourceProducts">
<PagerStyle HorizontalAlign="Left" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSourceProducts" runat="server"
TypeName=""
DataObjectTypeName=""
SelectMethod="GetProducts"
UpdateMethod="SaveProduct"
InsertMethod="SaveProduct">
</asp:ObjectDataSource>
</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>
<p>Results on Microsoft-IIS/8.5 .net: 4.0.30319.42000 VB Version 14.0</p><form method="post" action="./aspnet_comp_webforms_servercontrols_databindingexpressionoverview_01a_08.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="oAMbmpxGEM6DIE/iX0bxVxZecO0d/x/wx4ojsE0w0Cqmssljwe+T/gIqc5iwVhSaWT9OUDjkWQmDN1FuYpWREJ6WVBQqfLv/S+uPq5Fx4AMmeiluyPTUYc7ma+oAPWzSLPpbAurtsVwhLAHos9oy6wiy4daDXKmM32bPwV2CPG/QP+uuikGQtYlwuocvCMDw9kmJmIiv2Ju9r6xtOPM1E3KRL1a4Wa4RhOhNoo1p6wnLMGdfex/ZKS/uNPpaQt0quE31OvSe5vX7jeBXcRtJF27ewnJP3SHxT1h0AhezcJImXVwXGAq4ZifK9raxkKayVBCQ6ULjXhoVa+qmMxiW3amKlRS2u6RokECPXvNQpMBhe5e4icVHdkWXDR9WWNanPF8JCSy2ZWH10qyMuqQV66uSB/bRDlQZMLKdVvsr+Z+ezG5qMEgyZFsWm9JNwO8f3n/jZ/lYv5gJQta4X1pEL0nWsVHWf5nU8zwPngO85L21r8t1QDHgN95EsXl2cY4Wc3bopQHTh6sswDe45v4h6V7EWTDqkraHro4UBthLjmgOM6baldHdZ50K7FC2iXPj7fnG7KNJ3MaUA+LkT8e+TKRfYrpwKXhO5f20SCbhkt0VcLMEPVuWFySJbK/hMCFpJNSg7d2tGlrpMmN6WOPV4C8KgNPhmD5x2dN2c5jLlyx5r8PC+zUh8E43/yPQcwP0Z55Umxb7Ggn+gKkqCXGodR3cncEeRrY2UbymnXmM3VkesYCnvlCYSbzWDJwJn+2d" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="D149555E" />
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="JOI2imtEHDAQ16ZMKJLREJ1PYjX59N7AoEozVEWGCvsq+OOw63V33LopNbVIg2k5bwqqko1h2pRmVaALMDqHDZsLptTwLYzGeWPZ+gj9h5mh7S23YDq1X4vwZEW6P8btOPJPjk0N0UpvKUEpHBPVZKl0iZVt/EuMkKXxDT7RvFQ=" />
</div>
<table cellspacing="0" id="FormView1" style="border-collapse:collapse;">
<tr>
<td colspan="2">
<table>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductID")]<br><b>Product ID:</b></td>
<td>1</td>
</tr>
<tr>
<td align="right">[DataSourceID="ObjectDataSourceProducts";Bind("ProductID")]<br><b>Product Name:</b></td>
<td><select name="FormView1$InsertCategoryDropDownList" id="FormView1_InsertCategoryDropDownList">
<option selected="selected" value="1">Product One</option>
<option value="2">Product Two</option>
</select>
</td>
</tr>
<tr>
<td align="right">[DataBinder.Eval(Container.DataItem, "ProductDesc")]<br><b>Product Desc:</b></td>
<td>Adjustable Race</td>
</tr>
</table>
</td>
</tr>
</table>
<div>
<table cellspacing="0" rules="all" border="1" id="grdCustomers" style="border-collapse:collapse;">
<tr>
<th scope="col">ProductID</th><th scope="col">ProductName</th>
</tr><tr>
<td>1</td><td>Product One</td>
</tr><tr>
<td>2</td><td>Product Two</td>
</tr>
</table>
</div>
</form>
</body>
</html>
HTML Web Page Embedded Output:
The same list control could be used in an edit item template as well.
See Also
Tasks
How to: Bind to Data in a Templated Control
Concepts
Sources and References
- https://docs.microsoft.com/en-us/previous-versions/aspnet/ms178366(v=vs.100)?redirectedfrom=MSDN