Friday 12 July 2013

DataList in ASP.NET using C#


In this article I will explain what is datalist control in ASP.NET? how to bind data to Dalalist ?

The DataList control like the Repeater control is a template driven, light weight control, and acts as a container of repeated data items. The templates in this control are used to define the data that it will contain. It is flexible in the sense that you can easily customize the display of one or more records that are displayed in the control. You have a property in the DataList control called RepeatDirection that can be used to customize the layout of the control.


Live Example :

<asp:DataList runat="server" ID="DataList1" DataKeyField="ID" GridLines="Both"
                                                        RepeatDirection="Horizontal" OnItemCommand="DataList1_ItemCommand" ShowHeader="false"
                                                        ShowFooter="false" Width="80%">
                                                        <ItemTemplate>
                                                            <asp:Label runat="server" ID="lblIDIT" Text='<%# Bind("ID") %>' Visible="false"></asp:Label>
                                                            <asp:LinkButton runat="server" ID="lbNameIT" CommandName="Select" Text='<%# Bind("Name") %>'
                                                                CommandArgument='<%# Eval("ID") %>'></asp:LinkButton>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:Label runat="server" ID="lblIDET" Text='<%# Bind("ID") %>' Visible="false"></asp:Label>
                                                            <asp:Label runat="server" ID="lblNameET" Text='<%# Bind("Name") %>'></asp:Label>
                                                        </EditItemTemplate>
                                                        <ItemStyle Width="50px" HorizontalAlign="Center" VerticalAlign="Middle" CssClass="gridgrayrow1"
                                                            BackColor="#edeff0" />
                                                        <EditItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="gridbluerow1"
                                                            BackColor="#cae4fd" Font-Bold="true" />
                                                    </asp:DataList>


// Get data from DataBase and bind data to DataList

  public void BindDataList()
    {
        DataList1.DataSource = ds.Tables[0];
        DataList1.DataBind();
    }


// Below is the code for datalist item command event code.

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        dlClassList.EditItemIndex = e.Item.ItemIndex;
        BindDataList();
        ID = Convert.ToInt32(e.CommandArgument);
        Session["ID"] = ID;       
}

No comments:

Post a Comment