In this example, 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.
Output:
WebForm1.aspx:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h2 style="color: #D33257; 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> |
Student.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication2 { public class Student { private string _Address; private int _StudentAge; private string _StudentName; public Student(string StudentName, int StudentAge, string Address) { _StudentName = StudentName; _StudentAge = StudentAge; _Address = Address; } public string Address { get { return _Address; } set { _Address = value; } } public int StudentAge { get { return _StudentAge; } set { _StudentAge = value; } } public string StudentName { get { return _StudentName; } set { _StudentName = value; } } } } |
WebForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class WebForm1 : 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("Tom", 39, "Blank")); list.Add(new Student("Jerry", 48, "Knight")); list.Add(new Student("Jeffery", 39, "Silvar")); list.Add(new Student("Angela", 48, "Dramma")); lstStudentlist.DataSource = list; lstStudentlist.DataBind(); } 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(); } } } } |