This tutorial, how <asp:CheckBox/> worked in GridView, Repeater and DataList control to provide the user with an intuitive way of selecting multiple rows of the GridView, Repeater and DataList.
<asp:Repeater ID="RepeaterExample" runat="server" DataSourceID="AccessDataSourceExample">
<ItemTemplate>
<div>
<asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' />
</div>
</ItemTemplate>
</asp:Repeater>
<asp:GridView ID="GridViewExample" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="AccessDataSourceExample">
<Columns>
<asp:TemplateField HeaderText="CategoryID" InsertVisible="False">
<ItemTemplate>
<asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
</Columns>
</asp:GridView>
<asp:DataList ID="DataListExample" runat="server" DataKeyField="CategoryID"
DataSourceID="AccessDataSourceExample">
<ItemTemplate>
CategoryID:
<asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' /><br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<br />
CategoryName:
<asp:Label ID="CategoryNameLabel" runat="server" Text='<%# Eval("CategoryName") %>' />
<br /><br />
</ItemTemplate>
</asp:DataList></div>
<asp:Literal ID="rptLiteralExample" runat="server" /><br />
<asp:Literal ID="grD_listiteralExample" runat="server" /><br />
<asp:Literal ID="dstliteralExample" runat="server" /><br />
<asp:Button ID="ButtonExample" runat="server" Text="Button" />
<asp:AccessDataSource ID="AccessDataSourceExample" runat="server" DataFile="~/App_Data/MyDatabase.mdb"
SelectCommand="SELECT [CategoryID], [Description], [CategoryName] FROM [Categories]" />
GridView Example:
The <asp:GridView /> contains a collection of GridViewRow objects. Once you reference the collection, and iterate through it, you can use the FindControl method of the GridViewRow to access controls:
string Grd = "GridView Items Checked:<br />";
foreach (GridViewRow gvr in GridViewExample.Rows)
{
CheckBox chk = (CheckBox)gvr.FindControl("CategoryID");
if (chk.Checked)
{
Grd += (chk.Text + "<br />");
}
}
grD_listiteralExample.Text = Grd;
Repeater Example:
The <asp:Repeater /> has an Items collection. In this example, the Count property of the Items collection is retrieved and used with a for.. next loop While ... End to iterate the collection. The FindControl method of each Item is used to reference the CheckBox again:
string Rpt = "Repeater Items Checked:<br />";
for (int i = 0; i < RepeaterExample.Items.Count; i++)
{
CheckBox chk = (CheckBox)RepeaterExample.Items[i].FindControl("CategoryID");
if (chk.Checked)
{
Rpt+=(chk.Text + "<br />");
}
}
rptLiteralExample.Text = Rpt;
DataList Example:
The <asp:DataList /> also has an Items collection, but this time, foreach is used to iterate the collection. for... next could just as easily be used as in the Repeater example:
string Dl = "Datalist Items Checked:<br />";
foreach (DataListItem dli in DataListExample.Items)
{
CheckBox chk = (CheckBox)dli.FindControl("CategoryID");
if (chk.Checked)
{
Dl += (chk.Text + "<br />");
}
}
dstliteralExample.Text = Dl;
<asp:Repeater ID="RepeaterExample" runat="server" DataSourceID="AccessDataSourceExample">
<ItemTemplate>
<div>
<asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' />
</div>
</ItemTemplate>
</asp:Repeater>
<asp:GridView ID="GridViewExample" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="AccessDataSourceExample">
<Columns>
<asp:TemplateField HeaderText="CategoryID" InsertVisible="False">
<ItemTemplate>
<asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
</Columns>
</asp:GridView>
<asp:DataList ID="DataListExample" runat="server" DataKeyField="CategoryID"
DataSourceID="AccessDataSourceExample">
<ItemTemplate>
CategoryID:
<asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' /><br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<br />
CategoryName:
<asp:Label ID="CategoryNameLabel" runat="server" Text='<%# Eval("CategoryName") %>' />
<br /><br />
</ItemTemplate>
</asp:DataList></div>
<asp:Literal ID="rptLiteralExample" runat="server" /><br />
<asp:Literal ID="grD_listiteralExample" runat="server" /><br />
<asp:Literal ID="dstliteralExample" runat="server" /><br />
<asp:Button ID="ButtonExample" runat="server" Text="Button" />
<asp:AccessDataSource ID="AccessDataSourceExample" runat="server" DataFile="~/App_Data/MyDatabase.mdb"
SelectCommand="SELECT [CategoryID], [Description], [CategoryName] FROM [Categories]" />
GridView Example:
The <asp:GridView /> contains a collection of GridViewRow objects. Once you reference the collection, and iterate through it, you can use the FindControl method of the GridViewRow to access controls:
string Grd = "GridView Items Checked:<br />";
foreach (GridViewRow gvr in GridViewExample.Rows)
{
CheckBox chk = (CheckBox)gvr.FindControl("CategoryID");
if (chk.Checked)
{
Grd += (chk.Text + "<br />");
}
}
grD_listiteralExample.Text = Grd;
Repeater Example:
The <asp:Repeater /> has an Items collection. In this example, the Count property of the Items collection is retrieved and used with a for.. next loop While ... End to iterate the collection. The FindControl method of each Item is used to reference the CheckBox again:
string Rpt = "Repeater Items Checked:<br />";
for (int i = 0; i < RepeaterExample.Items.Count; i++)
{
CheckBox chk = (CheckBox)RepeaterExample.Items[i].FindControl("CategoryID");
if (chk.Checked)
{
Rpt+=(chk.Text + "<br />");
}
}
rptLiteralExample.Text = Rpt;
DataList Example:
The <asp:DataList /> also has an Items collection, but this time, foreach is used to iterate the collection. for... next could just as easily be used as in the Repeater example:
string Dl = "Datalist Items Checked:<br />";
foreach (DataListItem dli in DataListExample.Items)
{
CheckBox chk = (CheckBox)dli.FindControl("CategoryID");
if (chk.Checked)
{
Dl += (chk.Text + "<br />");
}
}
dstliteralExample.Text = Dl;
A DataBinding expression is a special type of expression that is not evaluated until runtime. You mark a Databinding expression in a page by wrapping the expression in opening <%# and closing %> brackets.
A DataBinding expression isn’t evaluated until a control’s DataBinding event is raised.
When you bind a DataBound control to a DataSource control declaratively, this event is raised automatically. When you bind a DataSource control to a data source programmatically, the DataBinding event is raised when you call the DataBind() method.
<%#Eval(“Title”)%>
<%#Eval(“DateReleased”, “{0:D}”) %>
Both DataBinding expressions call the Eval() method. The Eval() method is a protected method of the Page class. Behind the scenes, the Page.Eval() method calls the static (shared) DataBinder.Eval() method. If you want to be verbose, instead of using the Eval() method, you could use the following two expressions:
<%# DataBinder.Eval(Container.DataItem, “Title”) %>
<%# DataBinder.Eval(Container.DataItem, “DateReleased”, “{0:D}” ) %>
Technically, the Eval() method uses reflection when evaluating the data item to find a property with a certain name. You do pay a performance penalty when you use reflection.
As an alternative, you can improve the performance of your DataBinding expressions by casting the data items to a particular type like this:
<%# ((System.Data.DataRowView)Container.DataItem)[“Title”] %>
A DataBinding expression isn’t evaluated until a control’s DataBinding event is raised.
When you bind a DataBound control to a DataSource control declaratively, this event is raised automatically. When you bind a DataSource control to a data source programmatically, the DataBinding event is raised when you call the DataBind() method.
<%#Eval(“Title”)%>
<%#Eval(“DateReleased”, “{0:D}”) %>
Both DataBinding expressions call the Eval() method. The Eval() method is a protected method of the Page class. Behind the scenes, the Page.Eval() method calls the static (shared) DataBinder.Eval() method. If you want to be verbose, instead of using the Eval() method, you could use the following two expressions:
<%# DataBinder.Eval(Container.DataItem, “Title”) %>
<%# DataBinder.Eval(Container.DataItem, “DateReleased”, “{0:D}” ) %>
Technically, the Eval() method uses reflection when evaluating the data item to find a property with a certain name. You do pay a performance penalty when you use reflection.
As an alternative, you can improve the performance of your DataBinding expressions by casting the data items to a particular type like this:
<%# ((System.Data.DataRowView)Container.DataItem)[“Title”] %>
DataBinder..::.Eval Method (Object, String)
Evaluates data-binding expressions at run time.
Namespace: System.Web.UI
Assembly: System.Web (in System.Web.dll)
public static Object Eval(
Object container,
string expression
)
Parameters :
container
expression
The value of the expression parameter must evaluate to a public property.
This method is automatically called when you create data bindings in a rapid application development (RAD) designer such as Visual Studio. You can also use it declaratively to simplify casting to a text string. To do so, you use the <%# %> expression syntax, as used in standard ASP.NET data binding.
This method is particularly useful when binding data to controls that are in a templated list.
For any of the list Web controls, such as GridView, DetailsView, DataList, or Repeater, container should be Container.DataItem. If you are binding against the page, container should be Page.
<%# DataBinder.Eval (Container.DataItem, "Product") %>
Evaluates data-binding expressions at run time.
Namespace: System.Web.UI
Assembly: System.Web (in System.Web.dll)
public static Object Eval(
Object container,
string expression
)
Parameters :
container
- Type: System..::.Object
- The object reference against which the expression is evaluated. This must be a valid object identifier in the page's specified language.
expression
- Type: System..::.String
- The navigation path from the container object to the public property value to be placed in the bound control property. This must be a string of property or field names separated by periods, such as Tables[0].DefaultView.[0].Product in C# .
The value of the expression parameter must evaluate to a public property.
This method is automatically called when you create data bindings in a rapid application development (RAD) designer such as Visual Studio. You can also use it declaratively to simplify casting to a text string. To do so, you use the <%# %> expression syntax, as used in standard ASP.NET data binding.
This method is particularly useful when binding data to controls that are in a templated list.
For any of the list Web controls, such as GridView, DetailsView, DataList, or Repeater, container should be Container.DataItem. If you are binding against the page, container should be Page.
<%# DataBinder.Eval (Container.DataItem, "Product") %>