ASP.Net Data Controls


GridView Control

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1" Width="100%" AllowPaging="true" PageSize="6">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country" 
SortExpression="Country" />
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT * FROM [students]"></asp:SqlDataSource>

Result

IdNameCityCountry
1Firoz KhanAllahabadIndia
2Anastasiya PutinaMoscowRussia
3Imran KhanIslamabadPakistan
4Fatima AliTehranIran
5Mehmet Ali IstanbulTurkey
6Ram PrashadMughalsaraiIndia
12

DataList Control

<asp:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="SqlDataSource2" Width="100%" RepeatColumns="3">
<ItemTemplate>
<table class="dataList"> 
<tr>     
<td><h4><asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /></h4></td>
</tr>         
<tr>      
<td> 
<asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /> <br />
<asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' /> <br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT TOP 9 * FROM students"></asp:SqlDataSource>

Result

Firoz Khan

Allahabad
India

Fatima Ali

Tehran
Iran

Abdul Qadir

Baghdad
Iraq

Anastasiya Putina

Moscow
Russia

Mehmet Ali

Istanbul
Turkey

Ahemed Erdogan

Nicosia
Cyprus

Imran Khan

Islamabad
Pakistan

Ram Prashad

Mughalsarai
India

Repeater Control

<asp:Repeater ID="RptCntl" runat="server">
<HeaderTemplate>
 <table border="1" cellpadding="0" cellspacing="0">
<tr>
<th>Id</th>
<th>Name</th>
<th> City </th>
<th>Country</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("Id") %></td>
<td><%# Eval("Name") %></td>
<td><%# Eval("City") %></td>
<td><%# Eval("Country") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

C#

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.BindRepeater();
        }
    }

    private void BindRepeater()
    {
        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 * FROM Students", con))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    RptCntl.DataSource = dt;
                    RptCntl.DataBind();
                }
            }
        }
    }

Result

Id Name City Country
1 Firoz Khan Allahabad India
2 Anastasiya Putina Moscow Russia
3 Imran Khan Islamabad Pakistan
4 Fatima Ali Tehran Iran
5 Mehmet Ali Istanbul Turkey

ListView Control

<asp:ListView ID="Listv1" runat="server"  GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1"  OnPagePropertiesChanging="OnPagePropertiesChanging">

<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="Listv1" PageSize="3">                
<Fields>    
<asp:NextPreviousPagerField  ButtonType="Link" ShowFirstPageButton="false" ShowLastPageButton="false" ShowNextPageButton="false" />
<asp:NumericPagerField ButtonCount="4" />
<asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="false" ShowNextPageButton="true" ShowPreviousPageButton="false"  />
</Fields>
</asp:DataPager>  

</LayoutTemplate>

<GroupTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</GroupTemplate> 
    
<ItemTemplate>
<div class="ListBox">
<h1> <%# Eval("Name") %></h1>
<%# Eval("City") %>, <%# Eval("Country") %> 
</div>       
</ItemTemplate>

</asp:ListView>

C#

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack)
        {
         this.BindListView();
 }
 }       

        

    private void BindListView()
    {

        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {

                cmd.CommandText = "select * from students";

                cmd.Connection = con;


                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    Listv1.DataSource = dt;
                  
                    Listv1.DataBind();
                   

                }
            }
        }




    }

    protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        (Listv1.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

        this.BindListView();

    }

Result

Firoz Khan

Allahabad, India

Anastasiya Putina

Moscow, Russia

Imran Khan

Islamabad, Pakistan
Previous 1 2  ... Next