Data List in asp.net

DataList is a web server control.It is used for representing data in customizable format with different direction flows.In order to understand about this, Start new website project. Add ASP.NET Folder App_Data through Solution Explorer.

Prepare database in msaccess file, name it Database.mdb with table Name product, take three fields in table. 

ProductId         autonumber
productName    text
price               Currency

Place this msaccess file in App_code  Folder in your project and refresh the project folder.

Put some data in table like
productName       price

Tubelight                 80
Bulb                        20
Switch                    35
Microwave            5000
TV                    10000
Vedio Game         2000
Key Board           1500
Mouse                 500
UPS                   2000

Now drag DataList Control on Default.aspx , in source you will find following code.

 

        <asp:DataList ID="DataList1" runat="server">
        </asp:DataList>

Put ItemTemplate for displaying Items in DataList

        <asp:DataList ID="DataList1" runat="server">
  <ItemTemplate>
       
        </ItemTemplate>
        </asp:DataList>

Take two lables in ItemTamplate one for ProductName field and other for price field


        <asp:DataList ID="DataList1" runat="server">
        <ItemTemplate>

        <asp:Label ID="lblProduct" Text='<%#Eval("ProductName") %>' runat="server"></asp:Label>
        <asp:Label ID="lblPrice" Text='<%#Eval("Price") %>' runat="server"></asp:Label>

        </ItemTemplate>
       
        </asp:DataList>

Now set some more properties of datalist Like RepeatDirection="Horizontal" (for horizontal direction of items) or

RepeatDirection="Vertical" (for vertical direction of items) for direction flow and RepeatColumns="3" for number of columns.


        <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal">
        <ItemTemplate>

        <asp:Label ID="lblProduct" Text='<%#Eval("ProductName") %>' runat="server"></asp:Label>
        <asp:Label ID="lblPrice" Text='<%#Eval("Price") %>' runat="server"></asp:Label>

        </ItemTemplate>
       
        </asp:DataList>

after finishing this, press F7 in order to go on Default.aspx.cs .Add namespace in Default.aspx.cs

using System.Data.OleDb;

Here make a method to connect dataList with Database.

 


    protected void GetList()
    {
       
        DataTable table = new DataTable();
        OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=|DataDirectory|Database.mdb;Persist Security Info=True");
        OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from Product", connection);
        adapter.Fill(table);
        DataList1.DataSource = table;
        DataList1.DataBind();

    }

call is method on page load event of page

 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetList();

        }
    }
Now DataList is ready to display database items...............