Tuesday, April 24

Create Dynamic Ajax Tabcontainer With Dynamic Repeater Having Linkbuttons with Raising Click events

Here i am Describing About how to create Ajax Tabcontainer dynamically as well as how to bind repeater inside that Tab.Finally i have to bind data with inner linkbuttons of repeater from database.

Here is my Database diagram:-














Here is my Page OverView:-










































Here is my Source code:-

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

<!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>Om Namah Shivaya</title>
    <h1>Dynamic Ajax TabContainer With dynamic repeater Having linkbuttons With click Event(created by Shibashish Mohanty)</h1>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
   
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
   
    </div>
    </form>
  
</body>
</html>

Here is my Page Code Behind:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;

public partial class DynamicTabWithRepeaterAndLinkButton : System.Web.UI.Page
{
    AjaxControlToolkit.TabContainer DynamicTab;
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        var x = GetTabInfo();

        for (int i = 0; i < x.Count(); i++)
        {
            Table tb1 = new Table();
            TableRow tr1 = new TableRow();
            TableCell tc1 = new TableCell();

            DynamicTab.Tabs[i].Controls.Add(tb1);
            var y = GetNewItemInfo(x[i].PrID);
            #region comment for Gridview

            ////DropDownList drplist = new DropDownList();
            //GridView datalisting = new GridView();

            ////datalisting.RepeatColumns = 1;

            ////for (int j = 0; j < y.Count(); j++)
            ////{//
            //// datalisting.ID = y[j].MenuID.ToString();

            //datalisting.DataSource = y;
            //datalisting.DataBind();
            //// datalisting.ItemTemplate = Page.LoadTemplate("MenuData.ascx"); // what should I do here??
            ////
            ////drplist.ID = y[j].MenuID.ToString();

            ////drplist.DataSource = y;
            ////drplist.DataTextField = "MenuName";
            ////drplist.DataBind();
            ////drplist.AutoPostBack = true;
            ////if (drplist.SelectedIndex == 1)
            ////{
            ////   Label1.Text ="hello Friend";
            ////}

            ////}

            //tc1.Controls.Add(datalisting);
            //tr1.Cells.Add(tc1);
            //tb1.Rows.Add(tr1);
            #endregion

            #region comment for Repeater

            string connecStr = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connecStr);
            SqlDataAdapter da = new SqlDataAdapter("select * from ProjectMenu  where PrID =" + x[i].PrID + "", con);

            DataSet myDataSet = new DataSet();
            da.Fill(myDataSet);
            Repeater Repeater1 = new Repeater();
            Repeater1.DataSource = myDataSet;
            Repeater1.DataBind();
            foreach (RepeaterItem repeatItem in Repeater1.Items)
            {
                // if condition to add HeaderTemplate Dynamically only Once
                if (repeatItem.ItemIndex == 0)
                {
                    RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
                    HtmlGenericControl hTag = new HtmlGenericControl("h4");
                    hTag.InnerHtml = "Menu Names";
                    repeatItem.Controls.Add(hTag);
                }
                // Add ItemTemplate DataItems Dynamically
                RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
                LinkButton lKbt = new LinkButton();
                lKbt.ID = string.Format("{0}", myDataSet.Tables[0].Rows[repeatItem.ItemIndex]["MenuID"]);
                lKbt.CommandArgument = lKbt.ID;
                lKbt.Command += new CommandEventHandler(lKbt_Command);
                lKbt.Text = string.Format("{0} {1} <br/>", myDataSet.Tables[0].Rows[repeatItem.ItemIndex]["MenuName"], myDataSet.Tables[0].Rows[repeatItem.ItemIndex]["CreatedDate"]);
                Label lbl = new Label();
                lbl.Text = string.Format("{0} ", myDataSet.Tables[0].Rows[repeatItem.ItemIndex]["CreatedDate"]);
                Label lb = new Label();
                lb.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
                repeatItem.Controls.Add(lbl);
                repeatItem.Controls.Add(lb);
                repeatItem.Controls.Add(lKbt);
              
                // Add SeparatorTemplate Dynamically
                repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);

                //If you want to separate Each row With a header then uncomment below Line

                //LiteralControl ltrlHR =new LiteralControl();
               // ltrlHR.Text = "<hr />";
                //repeatItem.Controls.Add(ltrlHR);
            }

            tc1.Controls.Add(Repeater1);
            tr1.Cells.Add(tc1);
            tb1.Rows.Add(tr1);
            #endregion
        }
        PlaceHolder1.Controls.Add(DynamicTab);
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        try { createTab(); }
        catch { }

    }
    private List<Project> GetTabInfo()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return (from c in context.Projects

                    select c).ToList();
        }
    }
    private List<ProjectMenu> GetNewItemInfo(int Proid)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return (from n in context.ProjectMenus
                    where n.PrID == Proid

                    select n).ToList();
        }
    }
    private void createTab()
    {
        DynamicTab = new AjaxControlToolkit.TabContainer();


        DataClassesDataContext dc = new DataClassesDataContext();
        var x = GetTabInfo();


        for (int i = 0; i < x.Count(); i++)
        {
            TabPanel TbPnlCategry = new TabPanel();
            TbPnlCategry.HeaderText = x[i].PrName;
            TbPnlCategry.ID = x[i].PrID.ToString();
            DynamicTab.Tabs.Add(TbPnlCategry);
        }
    }
    void lKbt_Command(object sender, CommandEventArgs e)
    {
        Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('" + e.CommandArgument.ToString() +" "+ "Shibashish Mohanty');</script>");
       
    }
}

After running The page It will display As:-




After Click on  The Linkbutton It will display As:-








































No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty