Library tutorials & articles
In Depth ASP.NET using ADO.NET
- Introduction
- Developing a Templated DataBound Control
- Table, TableRow, and TableCell Web Server Controls
- Adding Rows and Cells to a Table Web Server Contro
- Data Binding Button, TextBox and DropDownList cont
- Data Binding Expressions
- Exploring with Web Based Address Database
- Adding CheckBox Control to DataGrid Control
- Data Navigation Form with Unbound Controls
Data Binding Expressions
Now we will use another approach to this issue of data binding; Data Binding Expression. Data binding expressions create bindings between any property on an ASP.NET page, including server control properties, and a data source when the DataBind method is called on the page. We 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="<%# databinding expression %> runat="server" />
Or
literal text <%# databinding expression %>
Regardless of where we place them, all data binding expressions, must be contained in <%# and %> characters. ASP.NET supports a hierarchical data binding model that supports associative bindings between server control properties and parent data sources. Any server control property can be data bound against any public field or property on the containing page or on the server control's immediate naming container.
The ASP.NET supplies a static method, called DataBinder.Eval that evaluates late-bound data binding expressions and optionally formats the result as a string. This method eliminates much of the explicit casting we must do to coerce values to the data type we desire. For example; in the following code piece, an integer is displayed as a currency string. We must first cast the type of the data row in order to retrieve the data field, IntegerValue, with the standard ASP.NET data binding syntax. Next, this is passed as an argument to the String.Format method:
<%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %>
Contrast this syntax with that of DataBinder.Eval, which has only three arguments: the naming container for the data item, the data field name, and a format string. In a templated list like DataList Class, DataGrid Class, or Repeater Class, the naming container is always Container.DataItem.
<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>
The format string argument is optional. DataBinder.Eval returns a value of type object, if it is omitted, as in the following example:
<%# (bool)DataBinder.Eval(Container.DataItem, "BoolValue") %>
When data binding controls within a templated list DataBinder.Eval is particularly useful, since often both the data row and the data field must be cast. The following source code demonstrates how we can data bind against properties in an ASP.NET server control. The Label Web server control data binds against the selected item in the list when a user selects a state from the DropDownList Web server control. <html>
<head>
<script language="C#" runat="server">
void SubmitBtn_Click(Object sender, EventArgs e) {
// Rather than explictly pull out the variable from the "StateList"
// and then manipulate a label control, just call "Page.DataBind".
// This will evaluate any <%# %> expressions within the page.
Page.DataBind();
}
</script>
</head>
<body>
<h3><font face="Verdana">Data 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>
Binding a RadioButtonList to a DataSource
The RadioButtonList control's programmatic functionality is almost identical to the CheckBoxList, but in fact it is different. When rendered, each item from the RadioButtonList.DataSource will be part of a group of RadioButton controls and we can only select one RadioButton out of the group at one time. The RadioButtonList is part of the List controls suite, therefore its behavior is the same as the proceeding List controls as far as how we enable automatic post back (AutoPostBack), how we tell if an item is checked (ListItem.Selected), and how to handle the SelectedIndexChanged on the post back. Therefore, we will be continuing with the code example now. This source code piece demonstrates how to use all the proceeding attributes and events to determine item selection.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server" language="C#" >
void Page_Load(Object sender, EventArgs e) {
if (! IsPostBack) {
rbl_DataBind();
}
}
void rbl_DataBind(){
SqlConnection SqlCon = new SqlConnection("server=localhost;uid=sa;pwd=;database=northwind");
SqlCommand SqlCmd = new SqlCommand("SELECT TOP 21 ProductName, ProductID FROM Products", SqlCon);
SqlCon.Open();
rbl.DataSource = SqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
rbl.DataTextField = "ProductName";
rbl.DataValueField = "ProductID";
rbl.DataBind();
}
void RadioButtonList_SelectedIndexChanged(Object sender, EventArgs e) {
StringBuilder sb = new StringBuilder("<b><u>Item Selected</u></b><p>");
int i;
for(i = 0; i < rbl.Items.Count; i++){
if(rbl.Items[i].Selected) {
sb.Append(i);
sb.Append(" - ");
sb.Append(rbl.Items[i].Text);
sb.Append("<br>");
}
}
lCheckBoxList.Text = sb.ToString();
}
</script>
<html>
<body>
<form runat="server">
<asp:Label
width="100%"
runat="server"
text="<center>Pick Products</center>"
BackColor="white"
ForeColor="Navy"
Font-Bold="true"
Font-Size="13"
/>
<asp:RadioButtonList
runat="server"
id="rbl"
CellPadding="4"
CellSpacing="0"
RepeatLayout="table"
RepeatColumns="3"
RepeatDirection="Vertical"
AutoPostBack="true"
OnSelectedIndexChanged="RadioButtonList_SelectedIndexChanged"
font-size="10"
BackColor="white"
ForeColor="Navy"
Font-Bold="true"
width="100%"
BorderWidth="1"
BorderColor="Navy"
/>
<p>
<asp:label
runat="server"
id="lCheckBoxList"
Font-Bold="false"
Font-Size="8"
ForeColor="Navy"
/>
</form>
</body>
</html>
We will get a page with three columns of RadioButton controls and a product name next to each when we execute this source code. The page is posted back to the server and the name of the product we selected is printed out to the screen when we select one. We can only check one product at a time when using the RadioButtonList control, unlike the CheckBoxList.
Related articles
Related discussion
-
PrintDocument printing using DOS print
by squrrel (1 replies)
-
hey developers out there
by pitsophera (0 replies)
-
building query at run time in ADO.NET using c#
by sudam.chavan@gmail.com (1 replies)
-
Using ADO.NET with SQL Server
by Manjot Bawa (23 replies)
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
Related podcasts
-
ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX
Wally walks through using ASP.NET Podcast Show #114 - ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX.
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
singhswat@yahoo.co.in
Actually ado.net and asp.net connectivity has lot more than it this indepth article has of no use because it doesnt hv any example/s associted with it
This is not ASP.NET It's VC++.NET ASP.NET is a web based language, not a windows forms language.
This thread is for discussions of In Depth ASP.NET using ADO.NET.