Thursday, March 7, 2013

How to create Dynamic Tab in ASP.Net



  I have a project where a page has a tab control with two static tabs. But more tabs and controls can be added with those fixed tab dynamically.These tabs and controls are defined in masters properly. It was really a interesting and challenging work for me. Also DataBase operations are there. For this project I needed AJAX tab container (N.B: how to use AJAX in Asp.Net? Please search and follow the procedure). Here I have used LINQ . I am retrieving data from Master Table and creating Tab and Controls Dynamically

N.B: It is not bound that we will use LINQ . You can use other way like DataReader or  DataAdaptor




Step-1

  Static Design View :

<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:TabContainer ID="TabContainer1" runat="server">
         <asp:TabPanel ID="TabGeneral" runat="server">
           <HeaderTemplate>General_1</HeaderTemplate>
           <ContentTemplate>
               //Add Controls As per your requirements
           </ContentTemplate>
         </asp:TabPanel>


        <asp:TabPanel ID="TabGeneral1" runat="server">
           <HeaderTemplate>General_2</HeaderTemplate>
           <ContentTemplate>
               //Add Controls As per your requirements
           </ContentTemplate>
         </asp:TabPanel>
        </asp:TabContainer>
    </div>
    </form>
</body>



Step-2


  Dynamic Code :


 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;



namespace Website

{
    public partial class Inventory_DynamicTab : System.Web.UI.Page
    {


      //Declare Variables and Objects
        TabPanel pnl;
        Table tbl;
        TableRow tbr;
        TableCell tcl;
        Button objButton;
        Button objButtonReset;
        Button objButtonSelect;   
        DropDownList objDDL;
        Label objLavel;      
        TextBox objTextBox;      
        CalendarExtender objCalender;


       protected void Page_PreInit(object sender, EventArgs e)
          {

            try
            {

                fillTabname();
            }
            catch (Exception ex)
            {
            }
         }

        void fillTabname()
        {

            var lnqParameter = (from Item in GetTabSequence() // own query to get data from DB
                                orderby Item.Sequence
                                select Item.TabName).Distinct();
            if (lnqParameter.ToArray().Length > 0)
            {
                for (int i = 0; i < lnqParameter.ToList().Count; i++)
                {

                     pnl = new TabPanel();
                    pnl.HeaderText = lnqParameter.ToList()[i].ToString();
                    pnl.ID = (i).ToString();
                    TabContainer1.Tabs.Add(pnl);


                    tbl = new Table();
                    tbl.BorderColor = System.Drawing.Color.Black;
                    tbl.ID = i.ToString();

                    TabContainer1.Tabs[i + 1].Controls.Add(tbl);

                    //// table
                    var lnqParameterCount = from Item in GetSequence()
// own query to get data from DB
                                            orderby Item.Sequence
                                            where Item.TabName == pnl.HeaderText 

                                            select new
                                            {
                                                fieldId = Item.Field_ID,
                                                fieldNm = Item.FieldName,
                                                fieldNm_Id = Item.Field_Name_ID,
                                                fieldType = Item.Control_type,
                                                multipleRow = Item.Multiple_Rows,
                                                sequence = Item.Sequence,
                                                maxlength = Item.Max_Length
                                            };
                    if (lnqParameterCount.ToList().Count > 0)
                    {

                        for (int j = 0; j < lnqParameterCount.ToList().Count; j++)
                        {                          

                            objLavel = new Label();
                            objTextBox = new TextBox();
                            objButton = new Button();


                            if (j <= 6)
                            {
                                if (lnqParameterCount.ToList()[j].fieldType == "TextBox" )
                                {
                                    //________________________
                                    objLavel.Text = lnqParameterCount.ToList()[j].fieldNm.ToString() + " :";
                                    objLavel.Width = 90;
                                    objLavel.Height = 15;
                                    objLavel.Font.Size = FontUnit.Smaller;
                                    //_____________________________

                                    objTextBox.ID = lnqParameterCount.ToList()[j].fieldId.ToString() + "_" + "Txt" + lnqParameterCount.ToList()[j].fieldNm_Id.ToString() + "_" + pnl.ID;
                                    objTextBox.Width = 120;
                                    objTextBox.Height = 15;
                                    objTextBox.MaxLength = int.Parse(lnqParameterCount.ToList()[j].maxlength.ToString());



                                    tcl = new TableCell();
                                    tcl.Controls.Add(objLavel);
                                    tcl.Controls.Add(objTextBox);


                                    tbr = new TableRow();
                                    tbr.Cells.Add(tcl);
                                    tbr.BackColor = System.Drawing.Color.Cyan;
                                    tbr.Width = 100;
                                    tbr.BorderWidth = 5;
                                    tbl.Rows.Add(tbr);


                                }
                                else if (lnqParameterCount.ToList()[j].fieldType == "Dropdown" )
                                {

                                    objLavel.Text = lnqParameterCount.ToList()[j].fieldNm.ToString() + " :";
                                    objLavel.Width = 90;
                                    objLavel.Height = 15;
                                    objLavel.Font.Size = FontUnit.Smaller;
                                    //________________________                              
                                    //objComboBox = new ComboBox();
                                    objDDL = new DropDownList();
                                    objDDL.ID = lnqParameterCount.ToList()[j].fieldId.ToString() + "_" + "Cmb" + lnqParameterCount.ToList()[j].fieldNm_Id.ToString() + "_" + pnl.ID;
                                    objDDL.Width = 120;
                                    objDDL.Height = 20;
                                    objDDL.SelectedIndexChanged+=new EventHandler(objDDL_SelectedIndexChanged);


                                    tcl = new TableCell();
                                    tcl.Controls.Add(objLavel);
                                    tcl.Controls.Add(objDDL);


                                    tbr = new TableRow();
                                    tbr.Cells.Add(tcl);
                                    tbr.BackColor = System.Drawing.Color.Cyan;
                                    tbr.Width = 100;
                                    tbr.BorderWidth = 5;
                                    tbl.Rows.Add(tbr);


                                    var getCmbData = from data in GetSequenceDetails()
// own query to get data from DB
                                                     where data.ParameterID == int.Parse(lnqParameterCount.ToList()[j].fieldId.ToString()) && data.Reference_Parameter_ID == 0
                                                     select data;

                                    if (getCmbData.ToList().Count > 0)
                                    {

                                        List<TAGGING_Property> colors = new List<TAGGING_Property>();
                                        TAGGING_Property objcol = new TAGGING_Property();
                                        objcol.Data_Value = "Field_ID";
                                        objcol.Data_Display = "Select";
                                        colors.Add(objcol);
                                        foreach (TAGGING_Property country in getCmbData.ToList())
                                        {
                                            colors.Add(country);
                                        }
                                        string idd = objDDL.ID.ToString();
                                        objDDL.DataSource = colors;
                                        objDDL.DataTextField = "Data_Display";
                                        objDDL.DataValueField = "Field_ID";
                                        objDDL.DataBind();
                                        objDDL.SelectedIndex = 0;
                                    }
                                }
                                else if (lnqParameterCount.ToList()[j].fieldType == "Calender" )
                                {
                                    objCalender = new CalendarExtender();
                                    //________________________
                                    objLavel.Text = lnqParameterCount.ToList()[j].fieldNm.ToString() + " :";
                                    objLavel.Width = 90;
                                    objLavel.Height = 15;
                                    objLavel.Font.Size = FontUnit.Smaller;
                                    //_____________________________

                                    objTextBox.ID = lnqParameterCount.ToList()[j].fieldId.ToString() + "_" + "Txt" + lnqParameterCount.ToList()[j].fieldNm_Id.ToString() + "_" + pnl.ID;
                                    objTextBox.Width = 120;
                                    objTextBox.Height = 15;
                                    objTextBox.MaxLength = int.Parse(lnqParameterCount.ToList()[j].maxlength.ToString());
                                    objCalender.TargetControlID = objTextBox.ID;
                                    objCalender.Format = "dd/MM/yyyy";


                                    tcl = new TableCell();
                                    tcl.Controls.Add(objLavel);
                                    tcl.Controls.Add(objTextBox);
                                    tcl.Controls.Add(objCalender);

                                    tbr = new TableRow();
                                    tbr.Cells.Add(tcl);
                                    tbr.BackColor = System.Drawing.Color.Cyan;
                                    tbr.Width = 100;
                                    tbr.BorderWidth = 5;
                                    tbl.Rows.Add(tbr);

                                }
                            }                      

                        }                    
                    }



                    objButton = new Button();
                    objButton.Text = "Save";
                    objButton.ID = pnl.ID + objButton.Text;
                    objButton.Width = 80;
                    objButton.Height = 25;
                    objButton.Click += new EventHandler(objButton_Click);
                    pnl.Controls.Add(objButton);

                    objButtonReset = new Button();
                    objButtonReset.Text = "Reset";
                    objButtonReset.ID = pnl.ID + objButtonReset.Text;
                    objButtonReset.Width = 80;
                    objButtonReset.Height = 25;
                    objButtonReset.Click+=new EventHandler(objButtonReset_Click);
                    pnl.Controls.Add(objButtonReset);

                }
            }
        }



       




      





No comments:

Post a Comment