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>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

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();

            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.ItemTemplate = Page.LoadTemplate("MenuData.ascx"); // what should I do here??
            ////drplist.ID = y[j].MenuID.ToString();

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



            #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();
            Repeater Repeater1 = new Repeater();
            Repeater1.DataSource = myDataSet;
            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";
                // 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; ";
                // 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 />";

    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();
    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.

shibashish mohanty