Saturday, 31 August 2013

DataList in ASP.net...

 

DataList Control:



            In this Artical i'm trying to explain What is DataList Control, How it's works, What is the specialty of this control. The unique feature of DataList control is Display the result in columns wise. For this we need to set the RepeatColumns property, based on that RepeatColumns value the result to be displayed. Here I'm discussing about how to work with datalist controls how to display the result into columns. Both Repeater Control and DataList control will works in same manner but the difference is display the result.


 

Use Of DataList:


             The unique feature of DataList control is Display the result in columns wise. For this we need to set the RepeatColumns property, based on that RepeatColumns value the result to be displayed. Here I'm discussing about how to work with datalist controls how to display the result into columns. Both Repeater Control and DataList control will works in same manner but the difference is display the result. Repeater control will display the result in faster execution but DataList can't that much. DataList display the result in columns manner but Repeater control can't do that.

Source Code:

Design a page like below

<%@ 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></title>
    <style type="text/css">
        .style1
        {
            font-size: x-large;
            color: #000066;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    
        <span class="style1"><strong><em>Working With DataList</em></strong></span><br />
        <br />

        <br />
        <asp:Label ID="lblName" runat="server" Text="Dept Name :"></asp:Label>
        
        
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="lblLoc" runat="server" Text="Location :"></asp:Label>
            
        <asp:TextBox ID="txtLoc" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
        <br />
        <br />
         
        <asp:LinkButton ID="lnkone" runat="server" onclick="lnkone_Click">One Column</asp:LinkButton>
     
        <asp:LinkButton ID="lnktwo" runat="server" onclick="lnktwo_Click"> Two Columns</asp:LinkButton>
     
        <asp:LinkButton ID="lnkthree" runat="server" onclick="lnkthree_Click">Three Columns</asp:LinkButton>

        <br />
        <br />

        <asp:DataList ID="DataList1" runat="server" CellPadding="4" 
            ForeColor="#333333"  >
            <HeaderTemplate>
                <table>
                    <tr>
                        <th align="center" width="100%">
                            Department Information                                   
                        </th>
                    </tr>
                </table>                
            </HeaderTemplate>

            <ItemTemplate>
                <table>
                    <tr>
                        <td align="center" width="50%">
                            <%#Container.ItemIndex+1 %>) <%#Eval("dname") %>   -   <%#Eval("loc") %>
                        </td>
                    </tr>
                </table>
                
            </ItemTemplate>
            <AlternatingItemStyle BackColor="White" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle HorizontalAlign="Left" BackColor="#1C5E55" Font-Bold="True" 
                ForeColor="White" />
            <ItemStyle HorizontalAlign="Left" BackColor="#E3EAEB" />
            <SelectedItemStyle BackColor="#C5BBAF" 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;
using System.Data.SqlClient;
using System.Data;

public partial class DataList : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("DataBase=ENGSOFT;User id=sa;Password=P@ssword9");
    SqlDataAdapter da;
    DataSet ds;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_GV();
        }
    }
    protected void Bind_GV()
    {
        con.Open();
        da = new SqlDataAdapter("select deptno,dname,loc from DEPT", con);
        ds = new DataSet();
        da.Fill(ds);

        con.Close();
        DataList1.DataSource = ds;
        DataList1.DataBind();

    }
        



    protected void Insert_Data()
    {
        con.Open();
        da = new SqlDataAdapter("insert into Dept values(@dname,@loc)", con);
        cmd = new SqlCommand("insert into Dept values(@dname,@loc)", con);
        cmd.Parameters.AddWithValue("@dname", txtName.Text);
        cmd.Parameters.AddWithValue("@loc", txtLoc.Text);
        cmd.ExecuteNonQuery();
        ds = new DataSet();
        con.Close();
        Bind_GV();
        Clear();
    }
    protected void Clear()
    {
        txtLoc.Text = "";
        txtName.Text = "";
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        Insert_Data();
        Clear();
    }
     



    protected void lnkone_Click(object sender, EventArgs e)
    {
        DataList1.RepeatColumns = 1;
        Bind_GV();
    }
    protected void lnktwo_Click(object sender, EventArgs e)
    {
        DataList1.RepeatColumns = 2;
        Bind_GV();
    }


    protected void lnkthree_Click(object sender, EventArgs e)
    {
        DataList1.RepeatColumns = 3;
        Bind_GV();
    }


OutPut:



Conclusion:

The Unique feature using DataList control we display the result in Column wise no other control will give the result like that.