Thursday, 7 June 2012

dynamic html control creations in asp.net






                              creating dynamic control in asp.net  in button click



















for this create html coding like this: on more database click event new div will create with textbox and onclick  of remove button div will remove which comes 1,2,3,...like this.



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

<!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 id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <fieldset>
            <div>
                <fieldset id="fst" style="width: 563px;">
                    &nbsp;&nbsp;&nbsp;&nbsp; Script Root: <span><span id="lblName">&nbsp;&nbsp; </span>&nbsp;<asp:TextBox
                        ID="txtScriptRoot" runat="server" />
                        <br/>
                        <br/>
                        <span id="Password">&nbsp;&nbsp;&nbsp; Log Path:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        </span>
                        <asp:TextBox ID="txtLogPath" runat="server" />
                        <br/>
                        <br/>
                        <span id="Span1">Out Put File Path:&nbsp; </span>
                        <asp:TextBox ID="txtOutPath" runat="server" />
                    </span>
                </fieldset>
            </div>
            <%--Data Base--%>
            <fieldset id="DataBase" style="width: 552px;">
                <div id="pnlDataBase" runat="server">
                </div>
                <div align="center" style="height: 32px; width: 563px">
                    <asp:Button ID="btnRemove" runat="server" Visible="false" Text="Remove"
                        onclick="btnRemove_Click"  />
                    <asp:Button ID="btnMore" runat="server" Text="More DataBase..."
                        onclick="btnMore_Click" />
                    &nbsp;
                </div>
            </fieldset>
            <%--Script Folder--%>
            <fieldset id="ScriptFolder" style="width: 552px;">
                <div>
                    <div id="pnLScriptFolder" runat="server">
                    </div>
                    <asp:Button ID="btnAddScriptFolder" runat="server" Text="Add Script Folder"
                        onclick="btnAddScriptFolder_Click"  />
                    <asp:Button ID="btnScriptRemove" runat="server" Visible="false"
                        Text="Remove Script Folder" onclick="btnScriptRemove_Click"
                         />
                </div>
            </fieldset>
            <div>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit"  />
            </div>
        </fieldset>
    </div>
    </form>
</body>
</html>

now come .cs file
 and do the following code->

#region Namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Xml;
using System.Configuration;
using System.Diagnostics;

#endregion

public partial class addcontrol : System.Web.UI.Page
{
    protected int _numDB
    {
        get { return (int)ViewState["NumDB"]; }
        set { ViewState["NumDB"] = value; }
    }
    protected int _numScript
    {
        get { return (int)ViewState["numScript"]; }
        set { ViewState["numScript"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                //Set _numDB value
                _numDB = 0;

                //Set _numScript value
                _numScript = 0;

                // Add dynamic default DataBase fieldset
                AddDataBase(_numDB);

                //Set _numDB on 1 when one default DB is add dynamic
                _numDB = 1;
            }
            else
            {
                // Add dynamic DataBase fieldset
                AddDataBase();

                // Add dynamic Script Folder fieldset
                AddScriptFolder();

              

            }
           
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void AddScriptFolder()
    {
        try
        {
            for (int scriptNo = 0; scriptNo < _numScript; scriptNo++)
            {
                //Add Dynamic ScriptFolder fieldset
                AddScriptFolder(scriptNo);
            }
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }
    }
    private void AddScriptFolder(int scriptNo)
    {
        try
        {
            HtmlGenericControl ctrlSpan = null;
            HtmlGenericControl ctrlGeneric = null;
            HtmlGenericControl ctrlFieldSet = null;
            HtmlGenericControl ctrlBRTage = null;

            Label lblValue = null;
            TextBox textField = null;

            // Create fieldset
            ctrlFieldSet = new HtmlGenericControl("fieldset");
            ctrlFieldSet.ID = string.Format("fstScript{0}", scriptNo);

            // Create legend
            ctrlGeneric = new HtmlGenericControl("legend");
            ctrlGeneric.Controls.Add(new LiteralControl(string.Format("Script Folder....({0})  ", scriptNo + 1)));

            // Create Label
            lblValue = new Label();
            lblValue.ID = string.Concat("lblScriptFolderDBName", scriptNo);
            lblValue.Text = string.Concat("DB Name", ": ");

            // Create TextBox
            textField = new TextBox();
            textField.ID = string.Concat("txtScriptFolderDBName", scriptNo);

            ctrlSpan = new HtmlGenericControl();

            //Add Label & textBox in Span
            ctrlSpan.Controls.Add(lblValue);
            ctrlSpan.Controls.Add(textField);

            //Add Break tag
            ctrlBRTage = new HtmlGenericControl("BR");
            ctrlSpan.Controls.Add(ctrlBRTage);

            ctrlFieldSet.Controls.Add(ctrlSpan);

            // Create Label and TextBox
            lblValue = new Label();
            lblValue.ID = string.Concat("lblPath", scriptNo);

            lblValue.Text = string.Concat("Path", ": ");
            textField = new TextBox();
            textField.ID = string.Concat("txtScriptFolderDBNamePath", scriptNo);

            //Add Label & textBox in Span
            ctrlSpan.Controls.Add(lblValue);
            ctrlSpan.Controls.Add(textField);

            //Add Break tag
            ctrlBRTage = new HtmlGenericControl("BR");
            ctrlSpan.Controls.Add(ctrlBRTage);

            //Add ctrlSpan
            ctrlFieldSet.Controls.Add(ctrlSpan);

            //Add legend In fieldset
            ctrlFieldSet.Controls.Add(ctrlGeneric);
            ctrlFieldSet.Attributes.Add("style", "width: 455px;");

            //Add fieldset In DataBase Div
            pnLScriptFolder.Controls.Add(ctrlFieldSet);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void AddDataBase(int dbNo)
    {
        try
        {
            HtmlGenericControl ctrlSpan = null;
            HtmlGenericControl ctrlGeneric = null;
            HtmlGenericControl ctrlFieldSet = null;
            HtmlGenericControl ctrlBRTage = null;
            //CheckBox checkBoxField = null;

            Label lblValue = null;
            TextBox textField = null;

            // Create fieldset
            ctrlFieldSet = new HtmlGenericControl("fieldset");
            ctrlFieldSet.ID = string.Format("fst{0}", dbNo);

            // Create legend
            ctrlGeneric = new HtmlGenericControl("legend");

            //legend text Default DataBase if Db count is equal to zero.
            if (dbNo == 0)
            {
                ctrlGeneric.Controls.Add(new LiteralControl("Default DataBase"));
            }
            else
            {
                ctrlGeneric.Controls.Add(new LiteralControl(string.Format("DataBase....({0})  ", dbNo)));
            }

            // Create Label
            lblValue = new Label();
            lblValue.ID = string.Concat("lblName", dbNo);
            lblValue.Text = string.Concat("Name", ": ");

            // Create TextBox
            textField = new TextBox();
            textField.ID = string.Concat("txtName", dbNo);

            ctrlSpan = new HtmlGenericControl();

            //Add Label & textBox in Span
            ctrlSpan.Controls.Add(lblValue);
            ctrlSpan.Controls.Add(textField);

            //Add Break tag
            ctrlBRTage = new HtmlGenericControl("BR");
            ctrlSpan.Controls.Add(ctrlBRTage);

            ctrlFieldSet.Controls.Add(ctrlSpan);

            // Create Label and TextBox
            lblValue = new Label();
            lblValue.ID = string.Concat("lblServer", dbNo);
            lblValue.Text = string.Concat("Server", ": ");
            textField = new TextBox();
            textField.ID = string.Concat("txtServer", dbNo);

            //Add Label & textBox in Span
            ctrlSpan.Controls.Add(lblValue);
            ctrlSpan.Controls.Add(textField);

            //Add Break tag
            ctrlBRTage = new HtmlGenericControl("BR");
            ctrlSpan.Controls.Add(ctrlBRTage);

            //Add ctrlSpan
            ctrlFieldSet.Controls.Add(ctrlSpan);

           

            //Add legend In fieldset
            ctrlFieldSet.Controls.Add(ctrlGeneric);
            ctrlFieldSet.Attributes.Add("style", "width: 455px;");

            //Add fieldset In DataBase Div
            pnlDataBase.Controls.Add(ctrlFieldSet);
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }
    }
    private void AddDataBase()
    {
        try
        {
            for (int dbNo = 0; dbNo < _numDB; dbNo++)
            {
                //Add Dynamic DataBase fieldset
                AddDataBase(dbNo);
            }
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }
    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        int count = 0;
        HtmlGenericControl ctrlFieldSet = null;
        try
        {
            count = _numDB - 1;

            //Find fieldset control by ID
            ctrlFieldSet = pnlDataBase.FindControl(string.Format("fst{0}", count)) as HtmlGenericControl;

            if (ctrlFieldSet != null)
            {
                //Remove fieldset from pnlDataBase div
                pnlDataBase.Controls.Remove(ctrlFieldSet);

                //reduce 1 from _numDB
                _numDB = _numDB - 1;
            }
            //If _numDB equal to 1 the hide db remove button.
            if (_numDB == 1)
                btnRemove.Visible = false;
        }
        catch (Exception ex)
        {
            throw ex;   // Logger.LogException(ex);
        }
    }
    protected void btnMore_Click(object sender, EventArgs e)
    {
        try
        {
            //Add Dynamic DataBase
            AddDataBase(_numDB);

            //Increment of 1 in _numDB value after adding Dynamic DataBase
            _numDB++;

            //Show DB remove button after Adding Dynamic DataBase
            btnRemove.Visible = true;
        }
        catch (Exception ex)
        {
           // Logger.LogException(ex);
        }
    }
    protected void btnScriptRemove_Click(object sender, EventArgs e)
    {

    }
    protected void btnAddScriptFolder_Click(object sender, EventArgs e)
    {

    }
}

No comments:

Post a Comment