Sunday, March 27, 2011

Bind DataSource into DataList example in Asp.Net




Please visit my new Web Site WWW.Codedisplay.com



The DataList control is a standard control of Asp.Net like others. It is used to display repeated list of data or items. You can bind any datasource in this DataList control like DataTable, SqlDataReader and XML etc. In most e-commerce site use DataList control to display products insted of repeater control. Here in this example i will show you how one can bind data to DataList control in runtime.

Table snapshot that we bound to the control:
DataList table structure


Now add an Aspx page in your project and write the below HTML markup code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="DataList" %>

<!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>DataList DataBinding Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList id="DataList1" runat="server">
            <ItemTemplate>
                Title: 
                <asp:Label id="Label6" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'></asp:Label>
                <br />
                Published: 
                <asp:Label id="Label7" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Published") %>'></asp:Label>
                <br />
                Modified: 
                <asp:Label id="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ModifedDate") %>'></asp:Label>
            </ItemTemplate>
            <HeaderTemplate>
                <asp:Label id="Label1" runat="server" Font-Names="Tahoma" Font-Italic="True">List of Articles:</asp:Label>
                <hr />
            </HeaderTemplate>
            <FooterTemplate>
                <hr />
            </FooterTemplate>
            <SeparatorTemplate>
                <hr />
            </SeparatorTemplate>
        </asp:DataList>    
    </div>
    </form>
</body>
</html>

Now under Page_Load event write the below server side code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataList1.DataSource = PopulateData();
            DataList1.DataBind();
        }
    }

    public SqlDataReader PopulateData()
    {
        string conStr=@"Data Source=.\SQLExpress;Initial Catalog=TESTDB;User Id=sa;Password=Comm!pass@1;";
        SqlConnection Conn = new SqlConnection(conStr);
        string sQuery="SELECT * FROM Article";
        SqlCommand sqlCommand = new SqlCommand(sQuery, Conn);
        Conn.Open();
        SqlDataReader dataReader=sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        return dataReader;
    }

Dont forget to add the "using System.Data.SqlClient;" namespace.

Now run the project & hope you will get below output:
DataList output
Hope now you bound the DataList control to display repeated data.

1 comments:

DuyTan said...

Great

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash