Using the Databound Control on a Page
The following sample shows an ASP.NET page that uses the TemplatedList control.
<%@ Page language="C#" %>
<%@ Register TagPrefix="custom" Namespace="CustomControls" Assembly="CustomControls"%>
<html>
<head>
<title>Databound Control Sample</title>
</head>
<body>
<form runat="server" ID="Form1">
<h3>TemplatedList Control Sample</h3>
<hr>
<custom:TemplatedList runat="server" id="FirstList" Font-Name="Verdana" Font-Size="16pt"
BorderColor="Gray" BorderWidth="1px" CellSpacing="0" CellPadding="2" GridLines="Both"
onItemCreated="FirstList_ItemCreated" onSelectedIndexChanged="FirstList_SelectedIndexChanged">
<ItemStyle ForeColor="Black" BackColor="#EEEEEE"/>
<AlternatingItemStyle BackColor="#DCDCDC"/>
<SelectedItemStyle ForeColor="White" BackColor="#000084"/>
<ItemTemplate>
<asp:Button runat="server" id="selectButton" CommandName="Select"
Text="Select" ForeColor="Blue"></asp:Button>
<asp:Label runat="server" Text='<%# Container.DataItem %>' ID="Label1" NAME="Label1"/>
</ItemTemplate>
</custom:TemplatedList>
<hr>
<asp:Label runat="server" id="infoLabel"></asp:Label>
<hr>
</form>
<script runat="server">
private int selectedIndex = -1;
private void LoadData()
{
ArrayList data = new ArrayList();
for (int i = 0; i < 10; i++)
{
data.Add("Item " + i);
}
FirstList.DataSource = data;
FirstList.DataBind();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!IsPostBack)
{
LoadData();
}
}
protected void FirstList_ItemCreated(object sender, ListItemTemplatedEventArgs e) {
if (e.Item.ItemType == ListItemType.SelectedItem)
{
selectedIndex = e.Item.ItemIndex;
Button selectButton = (Button)e.Item.FindControl("selectButton");
selectButton.Enabled = false;
}
}
protected void FirstList_SelectedIndexChanged(object sender,EventArgs e)
{
if (selectedIndex != -1)
{
Control item = FirstList.Controls[0].Controls[selectedIndex];
Button selectButton = (Button)item.FindControl("selectButton");
selectButton.Enabled = true;
}
selectedIndex = FirstList.SelectedIndex;
infoLabel.Text = "SelectedIndex: " + selectedIndex;
if (selectedIndex != -1)
{
Control item = FirstList.Controls[0].Controls[selectedIndex];
Button selectButton = (Button)item.FindControl("selectButton");
selectButton.Enabled = false;
}
}
On a Web Forms page, the Table Web server control creates a general-purpose table. Rows in the table are created as TableRow Web server controls, and cells within each row are implemented as TableCell Web server controls. The Table Web server control allows us to create server-programmable tables on our Web Forms page. The TableRow and TableCell Web server controls provide a way to display the actual content for the Table control.
Comparing the Table Web Server Control to Other Table Elements
Tables are typically used not just to present tabular information, but also as a method of formatting information on a Web page. There are many ways to create tables on our Web Forms page:
HTML table: We can add a <TABLE> element from the HTML tab of the Toolbox.
HtmlTable control: This is a <TABLE> HTML element that has been converted to a control by adding the runat=server attribute. We can program this control in server code.
Table Web Server control: A Web control that allows us to create and manipulate tables using an object model that is consistent with other Web controls (for example, adding table rows and cells).
In general, when we intend to add rows and cells (columns) to the table in code at run time, we use a Table Web server control. Although we can use it as a static table with predefined rows and columns, in that case it is easier to work with the HTML <TABLE> element.
Because the Table Web server control offers an object model with typed properties that is consistent with other Web server controls, it can be easier to program than the HtmlTable control. (The model is also consistent between the Table, TableRow, and TableCell controls.)
Comparing the Table Web Server Control to Other List Web Server Controls
We might accomplish some of the functions with a Table Web server control; we can also accomplish this with the list Web server controls: the Repeater, DataList, and DataGrid controls. All of these controls render (or have the option to render) as HTML tables.
The differences between the list controls and the Table control are:
Loading


Join the conversation! Your thoughts help the community grow.