DataGrid and DataList controls are derived from the WebControl class, while the Repeater control is derived from the Control class. The WebControl class contains a number of aesthetic properties, such as BackColor, ForeColor, CssClass, BorderStyle and so on.
In ASP .NET basically there are three kinds of the Data Presentation Controls.
Features of a GridView:
GridView example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridViewExample" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle"
SortExpression="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Region" HeaderText="Region"
SortExpression="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode"
SortExpression="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SQLConnectionString1 %>"
SelectCommand="SELECT * FROM [Company]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
DataList Example:
<h2 style="color: Green">
DataList in ASP.NET using C#</h2>
<asp:DataList ID="DataListExample" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
DataSourceID="SqlDataSource1"
Font-Bold="True" Font-Names="Verdana" Font-Size="Small" GridLines="Both" RepeatColumns="2"
RepeatDirection="Horizontal" Width="463px">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" Font-Size="Large" ForeColor="White"
HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderTemplate>
Student Information
</HeaderTemplate>
<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<ItemTemplate>
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'
ForeColor="#000099" />
<br />
DOB:
<asp:Label ID="DOBLabel" runat="server" Text='<%# Eval("DOB") %>' />
<br />
Age:
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
<br />
Course:
<asp:Label ID="CourseLabel" runat="server" Text='<%# Eval("Course") %>' />
<br />
Branch:
<asp:Label ID="BranchLabel" runat="server" Text='<%# Eval("Branch") %>' />
<br />
City:
<asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
<br />
MobileNo:
<asp:Label ID="MobileNoLabel" runat="server" Text='<%# Eval("MobileNo") %>' />
<br />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SQLConnectionString %>"
SelectCommand="SELECT * FROM [Student]"></asp:SqlDataSource>
<br />
Repeater Example:
<asp:Repeater ID="NewsListRepeater" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>Repeater Example<br /><br /></HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "TestEnglish") %><br />
<%# DataBinder.Eval(Container.DataItem, "TestMath") %><br />
</ItemTemplate>
<SeparatorTemplate>
******<br />
</SeparatorTemplate>
<AlternatingItemTemplate>
<i><%# DataBinder.Eval(Container.DataItem, "TestEnglish") %></i><br />
<i><%# DataBinder.Eval(Container.DataItem, "TestMath") %></i><br />
</AlternatingItemTemplate>
<FooterTemplate><br />End of the funky stuff</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SqlConnectionString %>"
SelectCommand="SELECT * FROM [Student]"></asp:SqlDataSource>
In ASP .NET basically there are three kinds of the Data Presentation Controls.
- GridView or DataGrid
- DataList
- Repeater
Features of a GridView:
- Displays data as a table
- Updateable
- Item as row
- Control over
- Alternate item
- Header
- Footer
- Colors, font, borders, etc.
- Paging
- List format
- No default output
- More control
- More complexity
- Item as row
- Not updateable
- Directional rendering
- Good for columns
- Item as cell
- Alternate item
- Updateable
- Datagrid has paging while Datalist doesnt.
- Datalist has a property called repeat. Direction = vertical/horizontal. (This is of great help in designing layouts). This is not there in Datagrid.
- A repeater is used when more intimate control over html generation is required.
- When only checkboxes/radiobuttons are repeatedly served then a checkboxlist or radiobuttonlist are used as they involve fewer overheads than a Datagrid.
GridView example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridViewExample" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle"
SortExpression="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Region" HeaderText="Region"
SortExpression="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode"
SortExpression="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SQLConnectionString1 %>"
SelectCommand="SELECT * FROM [Company]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
DataList Example:
<h2 style="color: Green">
DataList in ASP.NET using C#</h2>
<asp:DataList ID="DataListExample" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
DataSourceID="SqlDataSource1"
Font-Bold="True" Font-Names="Verdana" Font-Size="Small" GridLines="Both" RepeatColumns="2"
RepeatDirection="Horizontal" Width="463px">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" Font-Size="Large" ForeColor="White"
HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderTemplate>
Student Information
</HeaderTemplate>
<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<ItemTemplate>
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'
ForeColor="#000099" />
<br />
DOB:
<asp:Label ID="DOBLabel" runat="server" Text='<%# Eval("DOB") %>' />
<br />
Age:
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
<br />
Course:
<asp:Label ID="CourseLabel" runat="server" Text='<%# Eval("Course") %>' />
<br />
Branch:
<asp:Label ID="BranchLabel" runat="server" Text='<%# Eval("Branch") %>' />
<br />
City:
<asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />
<br />
MobileNo:
<asp:Label ID="MobileNoLabel" runat="server" Text='<%# Eval("MobileNo") %>' />
<br />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SQLConnectionString %>"
SelectCommand="SELECT * FROM [Student]"></asp:SqlDataSource>
<br />
Repeater Example:
<asp:Repeater ID="NewsListRepeater" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>Repeater Example<br /><br /></HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "TestEnglish") %><br />
<%# DataBinder.Eval(Container.DataItem, "TestMath") %><br />
</ItemTemplate>
<SeparatorTemplate>
******<br />
</SeparatorTemplate>
<AlternatingItemTemplate>
<i><%# DataBinder.Eval(Container.DataItem, "TestEnglish") %></i><br />
<i><%# DataBinder.Eval(Container.DataItem, "TestMath") %></i><br />
</AlternatingItemTemplate>
<FooterTemplate><br />End of the funky stuff</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SqlConnectionString %>"
SelectCommand="SELECT * FROM [Student]"></asp:SqlDataSource>
Related Topic: |