Search This Blog

Tuesday 3 February 2015

Retain dynamically created text box and DropDownList value after post-back in ASP.NET?

Step 1 :- Default.aspx Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GenDynamicControlValue.aspx.cs" Inherits="ASP_DOT_NET.GenDynamicControl" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Create" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="Get" OnClick="Button2_Click" />
        </div>
        <div>
            <asp:Panel ID="Panel1" runat="server">
            </asp:Panel>
        </div>
    </form>
</body>

</html>


Step 2:- Default.aspx.cs Code

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

namespace ASP_DOT_NET
{
    public partial class GenDynamicControlValue : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Occure only first Time Page Load
            }
            else
            {
                // Occure each time on Page PostBack

                // We need to re-create dynamic control each time of page PostBack

                // Just Comment the CreateDynamicControls() method and click the Button2 to get the value

                // TextBox value returns null.

                // Now UnComment the CreateDynamicControls() method and click the Button2 to get the value

                // Finally you got the TextBox value.

                CreateDynamicControls();
            }
        }

        private void CreateDynamicControls()
        {
            #region Before adding dynamic control to the panel clear the Panel1 then add dynamic control to it
            Panel1.Controls.Clear();
            #endregion

            Table tbldynamic = new Table();

            TableRow tr;

            // Totalc Column to Create
            int tblCols = 1;

            // Total Row to Create
            int tblRows = 3;

            #region Now iterate through the table row and add your controls
            for (int i = 0; i < tblRows; i++)
            {
                tr = new TableRow();

                #region Now iterate through the table column and add your controls
                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc1 = new TableCell();

                    TableCell tc2 = new TableCell();

                    TableCell tc3 = new TableCell();

                    #region Add the Label control to the TableCell
                    Label lblLabel = new Label();
                    lblLabel.ID = "lblLabel" + (i + 1);
                    lblLabel.EnableViewState = true;
                    lblLabel.Text = "Label" + (i + 1);

                    tc1.Controls.Add(lblLabel);
                    #endregion

                    #region Add the TextBox control to the TableCell
                    TextBox txtTextBox = new TextBox();
                    txtTextBox.ID = "txtTextBox" + (i + 1);
                    txtTextBox.EnableViewState = true;

                    tc2.Controls.Add(txtTextBox);
                    #endregion

                    #region Add the DropDownList control to the TableCell
                    DropDownList drpDropDownList = new DropDownList();
                    drpDropDownList.ID = "drpDropDownList" + (i + 1);
                    drpDropDownList.EnableViewState = true;

                    #region Binding Dynamic Control DropDownList
                    drpDropDownList.DataSource = BindDynamicDropDown();
                    drpDropDownList.DataTextField = "CountryName";
                    drpDropDownList.DataValueField = "CountryId";
                    drpDropDownList.DataBind();
                    #endregion

                    tc3.Controls.Add(drpDropDownList);
                    #endregion

                    #region Add the TableCell to the TableRow
                    tr.Cells.Add(tc1);

                    tr.Cells.Add(tc2);

                    tr.Cells.Add(tc3);
                    #endregion
                }
                #endregion

                #region Add the TableRow to the Table
                tbldynamic.Rows.Add(tr);
                #endregion
            }
            #endregion

            Panel1.Controls.Add(tbldynamic);
        }

        public DataTable BindDynamicDropDown()
        {
            DataTable dtTable = new DataTable();
            dtTable.Columns.Add("CountryId");
            dtTable.Columns.Add("CountryName");

            DataRow dr;

            // Row  1
            dr = dtTable.NewRow();

            dr[0] = "1";
            dr[1] = "India";

            dtTable.Rows.Add(dr);

            // Row  2

            dr = dtTable.NewRow();

            dr[0] = "2";
            dr[1] = "UK";

            dtTable.Rows.Add(dr);

            // Row  3

            dr = dtTable.NewRow();

            dr[0] = "3";
            dr[1] = "UAE";

            dtTable.Rows.Add(dr);


            return dtTable;
        }

        #region Create Dyanamic Control on Button1_Click
        protected void Button1_Click(object sender, EventArgs e)
        {
            CreateDynamicControls();
        }
        #endregion

        #region Get Dyanamic Created TextBox Control value on Button1_Click
        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                // Total Row to Create
                int tblRows = 3;

                #region Now iterate through the table row to get dynamic TextBox controls value.
                for (int i = 0; i < tblRows; i++)
                {
                    string DynamicTextBoxID = "txtTextBox" + (i + 1);

                    string DynamicDropDownListID = "drpDropDownList" + (i + 1);

                    TextBox txtTextBox = (TextBox)Panel1.FindControl(DynamicTextBoxID);

                    DropDownList drpDropDownList = (DropDownList)Panel1.FindControl(DynamicDropDownListID);

                    Response.Write(txtTextBox.Text + "<br/>");
                    Response.Write(drpDropDownList.SelectedValue + "-" + drpDropDownList.SelectedItem + "<br/>");
                }
                #endregion
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }
        #endregion
    }

}

Note :-

In case of dynamic created control need to re-created each time on page post back.

Just Comment the CreateDynamicControls() method and click the Button2 to get the value - TextBox value returns null.

Now UnComment the CreateDynamicControls() method and click the Button2 to get the value - You got the TextBox value.

Imp Note :- 

In case of dynamic created DropDownList control binding need to re-bind each time on page post back.


No comments:

Post a Comment