Posted by : Anwar Hossain
Category : DataList example in C Sharp

How to bind data into DataList using code behind using ASP.Net C #

Dear viewer’s I will show how to bind data using code behind to Data List control using ASP.Net C #. In this example I have created a list of student to supply data to DatalList but real time data will be collected from database.After taking DatalList Control we need to take ItemTemplate within ItemTemplate we need to desing the DataList using table and need to create necessary column ,within table need to label and other required control.Then we need to click double click on ItemDataBound of the DataList Control.

Bind data into Data List using ASP.Net C #

HTML

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Databind into DataList in C Sharp</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <h2 style="color: #99CC00; font-weight: bold">

            Databind into DataList in C Sharp</h2>

        <asp:DataList ID="lstStudentlist" runat="server" OnItemDataBound="lstStudentlist_ItemDataBound"

            Width="50%" CellPadding="4" ForeColor="#333333">

            <AlternatingItemStyle BackColor="White" />

            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <ItemStyle BackColor="#EFF3FB" />

            <ItemTemplate>

                <table>

                    <tr>

                        <td valign="top" align="center" width="200">

                            <asp:HyperLink ID="lnkName" runat="server" Font-Bold="True" Target="_blank" ForeColor="#00466E"></asp:HyperLink>

                        </td>

                        <td valign="top" align="center" width="200">

                            <asp:Label ID="lblAddress" runat="server" Text=""></asp:Label>

                        </td>

                        <td valign="top" align="center" width="200">

                            <asp:Label ID="lblAge" runat="server" Text=""></asp:Label>

                        </td>

                    </tr>

                </table>

            </ItemTemplate>

            <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

        </asp:DataList>

    </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;

 

public partial class BindDataDataList : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

       

            LoadAllStudent();

      

    }

    private void LoadAllStudent()

    {

 

        List<Student> list = new List<Student>();

        list.Add(new Student("Samim", 39, "Dhaka"));

        list.Add(new Student("Asad", 48, "Chittagong"));

        list.Add(new Student("Nayed", 39, "Sylhet"));

        list.Add(new Student("Anwar", 48, "Dhaka"));

        lstStudentlist.DataSource = list;

        lstStudentlist.DataBind();

 

 

    }

    public class Student

    {

 

        public Student(string StudentName, int StudentAge, string Address)

        {

 

            _StudentName = StudentName;

            _StudentAge = StudentAge;

            _Address = Address;

 

 

        }

 

        private string _Address;

 

        public string Address

        {

            get { return _Address; }

            set { _Address = value; }

        }

 

 

        private int _StudentAge;

 

        public int StudentAge

        {

            get { return _StudentAge; }

            set { _StudentAge = value; }

        }

 

        private string _StudentName;

 

        public string StudentName

        {

            get { return _StudentName; }

            set { _StudentName = value; }

        }

 

 

    }

    protected void lstStudentlist_ItemDataBound(object sender, DataListItemEventArgs e)

    {

 

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

            Student entry = e.Item.DataItem as Student;

 

            HyperLink lnkName = e.Item.FindControl("lnkName") as HyperLink;

            lnkName.Text = entry.StudentName.ToString();

 

            Label lblAddress = e.Item.FindControl("lblAddress") as Label;

            lblAddress.Text = entry.Address;

           

 

            Label lblAge = e.Item.FindControl("lblAge") as Label;

            lblAge.Text = entry.StudentAge.ToString();

 

        }

 

    }

}


Out Put

Bind-data-datalist-in-Csharp



Realted Article Headline

How to bind data into DataList using code behind using ASP.Net C #

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection