Skip to main content

How to hide and show data in the DataList?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListHide.aspx.cs" Inherits="DataListHide" %>

<!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>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <asp:DataList runat="server"  ID="Datalist1" Font-Size="10pt"
               Font-Name="Verdana" OnItemCommand="Datalist1_ItemCommand">
               <ItemTemplate>
                   <asp:LinkButton Style="text-decoration: none" runat="server" ID="btnDetails" Text="+"
                       CommandName="Show" Font-Name="Verdana" />
                   <b>
                       <%# DataBinder.Eval(Container.DataItem, "Id") %>
                   </b>
                   <br />
                   <asp:Label ID="lblEID" Visible="False" runat="Server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' />
                   <asp:DataGrid runat="server" ID="Datagrid1" Font-Name="Verdana" Font-Size="10pt"
                       HorizontalAlign="Center" Visible="False" Width="85%"/>
               </ItemTemplate>
           </asp:DataList>
       </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DataListHide : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            Datalist1.DataSource = CreateDS();
            Datalist1.DataBind();
        }

    }
    private DataSet CreateDS()
    {
        DataSet ds;
        if (Session["DataList_ParentChild"] == null)
        {
            ds = new DataSet();
            DataTable dt = new DataTable("Company");
            DataRow dr;
            dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
            dt.Columns.Add(new DataColumn("CompanyName", typeof(string)));
            dt.Columns.Add(new DataColumn("Address", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Dept", typeof(string)));
            for (int i = 1; i < 10; i++)
            {
                dr = dt.NewRow();
                dr[0] = i;
                dr[1] = "Company " + i;
                dr[2] = "Address " + i;
                dr[3] = "Manager name";
                dr[4] = "Adminstration";
                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);
            DataColumn[] Parent_PKColumns = new DataColumn[1];
            Parent_PKColumns[0] = dt.Columns["ID"];
            dt.PrimaryKey = Parent_PKColumns;

            dt = new DataTable("Employees");
            dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
            dt.Columns.Add(new DataColumn("CompanyID", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Dept", typeof(string)));
            for (int i = 1; i < 10; i++)
            {
                int imax = 0;
                if (i % 2 == 0) imax = 5;
                else imax = 4;
                for (int y = 2; y < imax; y++)    //3 emplyees for each company
                {
                    dr = dt.NewRow();
                    dr[0] = y + i * 5;
                    dr[1] = i;
                    dr[2] = "Employee # " + dr[0];
                    dr[3] = "Dept # " + (y + i);
                    dt.Rows.Add(dr);
                }
            }
            DataColumn[] Child_PKColumns = new DataColumn[1];
            Child_PKColumns[0] = dt.Columns["ID"];
            dt.PrimaryKey = Child_PKColumns;
            ds.Tables.Add(dt);
            DataColumn[] Child_FKColumns = new DataColumn[1];
            Child_FKColumns[0] = dt.Columns["CompanyID"];




            ds.Relations.Add("ParentChild", Parent_PKColumns, Child_FKColumns);
            Session["DataList_ParentChild"] = ds;
        }
        else
        {
            ds = (DataSet)Session["DataList_ParentChild"];
        }
        return ds;

    }
    protected void Datalist1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Show")
        {
            Label EmpIDlabel = (Label)e.Item.FindControl("lblEID");
            string strEmpID = EmpIDlabel.Text;
            ((DataGrid)e.Item.FindControl("Datagrid1")).DataSource = GetEmpDetails(strEmpID);
            ((DataGrid)e.Item.FindControl("Datagrid1")).DataBind();
            ((DataGrid)e.Item.FindControl("Datagrid1")).Visible = true;
            ((LinkButton)e.Item.FindControl("btnDetails")).Text = "-";
            ((LinkButton)e.Item.FindControl("btnDetails")).CommandName = "Hide";
        }
        if (e.CommandName == "Hide")
        {
            ((DataGrid)e.Item.FindControl("Datagrid1")).Visible = false;
            ((LinkButton)e.Item.FindControl("btnDetails")).Text = "+";
            ((LinkButton)e.Item.FindControl("btnDetails")).CommandName = "Show";
        }
    }
    protected DataView GetEmpDetails(string id)
    {

        DataSet ds = CreateDS() as DataSet;
        DataView dv = ds.Tables[1].DefaultView;
        dv.RowFilter = "CompanyID=" + id;
        return dv;

    }


}

Comments

Popular posts from this blog