Data Controls in ASP.NET 4.0


In this artical i'm trying to expalin about DataControls. What is data control. Different types of dataControls and Description about that controls and i give small example using all those controls at a time.

Data Controls in ASP.NET 4.0:



What is DataControls:



DataControls are used to display table format of data. Upto previous we display single cell informaition in labels and some other controls. If we need to display table format data for that purpose we refer Data Controls to display that data.

There are 2 types of controls to display the table format of data.

• Single Row Data Controls
• Multiple Row Data Controls

Single Row Data Controls:



FormView The Unique feature of this control is inserting . We no need to insert the data in outside just we did in that control itself.

DetailsView A more restricted way to display a single record in table format, no need to Design a page in source. Just drag & drop the control and bind the datasource it will work.

Multiple Row Data Controls:



GridView : It used to display the table format of data. Default behavior of this control is display the data without Auto generate columns. This will give flexible result to display table structure of data.

Repeater: A simple control for displaying data in tabular format, but the specialty of this controls is faster execution.

DataList:The unique feature of this control is display the result in column wise.

ListView: This will give all format of data like GridView , DataList, Repeater and Form view type of data. This is the richest control to display table structure of data.


Source Code:




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataControls.aspx.cs" Inherits="DataControls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
height: 523px;
font-size: x-large;
color: #CC0000;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<span class="style1"><strong><em>Working with Data Controls</em></strong></span><br />
<br />
<br />
<asp:Button ID="btnView" runat="server" Text="Single Row Bind Controls" OnClick="btnView_Click" />
<asp:Button ID="btnView1" runat="server" Text="Multiple Row Bind Controls" OnClick="btnView1_Click" />
<br />
<br />
<br />
<br />
<table width="100%" id="multiplerowbindcontrols" runat="server">
<tr>
<td width="50%">
<asp:Label ID="lblGV" runat="server" Text="GridView" ForeColor="Red" Font-Bold="true" Font-Size="X-Large" ></asp:Label>
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
GridLines="None" AutoGenerateColumns="False" Width="232px"
>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />

<Columns>
<asp:BoundField DataField="DName" HeaderText="Department Name" />
<asp:BoundField DataField="Loc" HeaderText="Location" />
</Columns>
</asp:GridView>
</td>
<td width="50%">
<asp:Label ID="lblRep" runat="server" Text="Repeater" ForeColor="Red" Font-Bold="true" Font-Size="X-Large"></asp:Label>
<br />
<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate>
<table style="background-color:#1C5E55; color: White">
<tr>
<th align="center" width="50%">
Department Name
</th>
<th align="center" width="50%">
Location
</th>
</tr>
</table>
</HeaderTemplate>
//itemTemplate is required to implement Repeater

<ItemTemplate>
<table>
<tr>
<td align="center" width="50%">
<%#Container.ItemIndex+1 %>) <%#Eval("dname") %>
</td>
<td align="center" width="50%">
<%#Eval("loc") %>
</td>
</tr>
</table>

</ItemTemplate>
</asp:Repeater>
</td>
</tr>
<tr>
<td width="50%">
<asp:Label ID="lblDL" runat="server" Text="DataList" ForeColor="Red" Font-Bold="true" Font-Size="X-Large" ></asp:Label>
<br />
<asp:DataList ID="DataList1" runat="server" CellPadding="4"
ForeColor="#333333" >
<HeaderTemplate>
<table>
<tr>
<td align="center" width="50%">
Department Name
</td>
<td align="center" width="50%">
Location
</td>
</tr>
</table>
</HeaderTemplate>

//itemTemplate is required for implementing DataList
<ItemTemplate>
<table>
<tr>
<td align="center" width="50%">
<%#Eval("dname") %>
</td>
<td align="center" width="50%">
<%#Eval("loc") %>
</td>
</tr>
</table>

</ItemTemplate>
<AlternatingItemStyle BackColor="White" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle HorizontalAlign="Left" BackColor="#1C5E55" Font-Bold="True"
ForeColor="White" />
<ItemStyle HorizontalAlign="Left" BackColor="#E3EAEB" />
<SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
</td>
<td width="50%">
<asp:Label ID="lblLV" runat="server" Text="ListView" ForeColor="Red" Font-Bold="true" Font-Size="X-Large" ></asp:Label>
<br />
<asp:ListView ID="ListView1" runat="server" >
//for Header we use Layout template in ListView
<LayoutTemplate>
<table ID="itemPlaceholderContainer" cellpadding="0" cellspacing="1" border="0" width="50%" >
<tr>
<th width="50%" align="center" style="background-color:#1C5E55; color:White;"> Department Name</th>
<th width="50%" align="center" style="background-color:#1C5E55; color:White;"> Location</th>
</tr>
<tr runat="server" ID="itemPlaceholder">
</tr>

</table>

</LayoutTemplate>
<ItemTemplate>
<table cellpadding="0" cellspacing="1" border="0" width="50%">
<tr>
<td align="center" width="50%">
<%#Eval("dname") %>
</td>
<td align="center" width="50%">
<%#Eval("loc") %>
</td>
</tr>
</table>

</ItemTemplate>
//Give altenating style using ltAlternatingItemTemplate
<AlternatingItemTemplate>
<table width="50%">
<tr align="center" id="tr" runat="server" style="background-color:#D4DFE1">
<td align="center" width="50%">
<%#Eval("dname") %>
</td>
<td align="center" width="50%">
<%#Eval("loc") %>
</td>
</tr>
</table>
</AlternatingItemTemplate>

</asp:ListView>
</td>
</tr>

</table>
<table width="100%" id="singlerowbindcontrols" runat="server">
<tr>
<td width="50%">
<asp:Label ID="lblFV" runat="server" Text="FormView" ForeColor="Red" Font-Bold="true" Font-Size="X-Large" ></asp:Label>
<br />
<asp:FormView ID="FormView1" runat="server" >
<HeaderTemplate>
<table>
<tr>
<td align="center" width="50%">
Department Name
</td>
<td align="center" width="50%">
Location
</td>
</tr>
</table>
</HeaderTemplate>
// itemtemplate is required for design

<ItemTemplate>
<table>
<tr>
<td align="center" width="50%">
<%#Eval("dname") %>
</td>
<td align="center" width="50%">
<%#Eval("loc") %>
</td>
</tr>
</table>

</ItemTemplate>

<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle HorizontalAlign="Left" BackColor="#1C5E55" Font-Bold="True"
ForeColor="White" />

</asp:FormView>

</td>
<td width="50%">
<asp:Label ID="lblDV" runat="server" Text="DetailsView" ForeColor="Red" Font-Bold="true" Font-Size="X-Large"></asp:Label>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" >
</asp:DetailsView>
</td>

</tr>
</table>

</div>
</form>
</body>
</html>



Code Behind:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class DataControls : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("DataBase=ENGSOFT;User id=sa;Password=P@ssword9");
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblDL.Visible = false;
lblDV.Visible = false;
lblFV.Visible = false;
lblGV.Visible = false;
lblLV.Visible = false;
lblRep.Visible = false;
}
}
protected void Controls_Visibile_For_Multiple_Row_Bind_Controls()
{
singlerowbindcontrols.Visible = false;
multiplerowbindcontrols.Visible = true;
lblLV.Visible = false;
lblDV.Visible = false;
lblGV.Visible = true;
lblRep.Visible = true;
lblDL.Visible = true;
lblFV.Visible = true;
}
protected void Controls_Visible_For_Single_Row_Bind_Controls()
{
multiplerowbindcontrols.Visible = false;
singlerowbindcontrols.Visible = true;
lblLV.Visible = true;
lblDV.Visible = true;
lblGV.Visible = false;
lblRep.Visible = false;
lblDL.Visible = false;
lblFV.Visible = false;
}
protected void Multiple_Rows_Bind_Controls()
{
Controls_Visibile_For_Multiple_Row_Bind_Controls();
con.Open();
da = new SqlDataAdapter("select top 4 dname,loc from DEPT", con);
ds = new DataSet();
da.Fill(ds);

con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();

DataList1.DataSource = ds;
DataList1.DataBind();

Repeater1.DataSource = ds;
Repeater1.DataBind();

ListView1.DataSource = ds;
ListView1.DataBind();

}
protected void Single_Row_Bind_Controls()
{
Controls_Visible_For_Single_Row_Bind_Controls();
con.Open();
da = new SqlDataAdapter("select top 4 dname,loc from DEPT", con);
ds = new DataSet();
da.Fill(ds);

con.Close();

FormView1.DataSource = ds;
FormView1.DataBind();

DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
protected void btnView_Click(object sender, EventArgs e)
{
Single_Row_Bind_Controls();
}

1
protected void btnView1_Click(object sender, EventArgs e)
{
Multiple_Rows_Bind_Controls();
}

2

}


Conclusion:


In the above all controls give some great features. But in that total list ListView works beautiful and this will give all format of data like Columns, Faster Execution, Insertion and Table structure also.

Hope this will give brief descritpion about all Data controls.


Attachments

  • Source Code (45082-23239-Source-Code.zip)
  • Comments

    Guest Author: Pink17 Sep 2013

    I literally jumped out of my chair and danced after reading this!



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: