Tuesday, April 3, 2012

Asp.Net - DataList

The DataList control in Asp.Net is similar to the Repeater control, with a few additional features, the Repeater control does not provide Update/Delete functionality, but this can be achieved using the DataList control.

The DataList control is flexible like the Repeater control, allows HTML tags in the Item definition, the DataList also provided an ItemTemplate & EditItemTemplate which helps us to define the layout for View & Edit modes separately.

The DataList control does not provide feature like Paging & Sorting which are available in the GridView & ListView controls.

Ideally the DataList is a middleman between the Repeater control at the lower end and the Grid/List view controls at the higher end in terms of features provided.

Let us now see an example of how to use the DataList control to display data on an Asp.Net web page.

In the Asp.Net page add the DataList control with the details of the Item/Edit template controls.

<asp:DataList ID="dlSample" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>Column1</td>
<td>Column2</td>
<td>Column3</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:TextBox ID="txt1" runat="server" Text='<%# Eval("COL1") %>'></asp:TextBox></td>

<td><asp:TextBox ID="txt2" runat="server" Text='<%# Eval("COL2") %>'></asp:TextBox></td>

<td><asp:TextBox ID="txt3" runat="server" Text='<%# Eval("COL3") %>'></asp:TextBox></td>

</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>

Now that we have defined the Template, we will have to bind data to the Template to display the data in the Page.

strQuery = "SELECT COL1, COL2, COL3 FROM SAMPLE;

DataSet dsSample = DataAccessLayer.GetDataSet(strQuery, "dtSample");
dlSample.DataSource = dsSample.Tables[“dtSample”];
dlSample.DataBind();

That’s it, we have a defined a DataList control template and have bound data to the template.


Search Flipkart Products:
Flipkart.com

1 comment:

Kamila said...

All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
Dot Net Training in Chennai
Software Testing Training in Chennai
Java Training Institute in Chennai
PHP Course in Chennai