Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Design:
 
label1 textbox1 button1 click on button1 then

label1 textbox1 button1
label2 textbox2 button2 click on button2 then
 

label1 textbox1 button1
label2 textbox2 button2
label3 textbox3 button3 click on button3 then
 

adds label4,textbox4 , button4 to above controls and
 
.
.
.
.

 

How to achieve this??
I tried but on clicking the dynamically generated button, the controls are getting vanished.
I used:
aspx:
 

<table border="1" cellpadding="0" cellspacing="0" id="tblGenerate" runat="server">
        <tr>
          <td>
              <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
          </td>
          <td>
              <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
          </td>
          <td>
              <asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="btnAdd_Click"/>
          </td>
          <td>
              <asp:Label ID="lblCost" runat="server"></asp:Label>
          </td>
      </tr>
      <tr>
          <td  id="trGenerate" runat="server" colspan="3">
          </td>
      </tr>
  </table>
 
aspx.cs:
protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void Page_Init(object sender, EventArgs e)
        {
            //Recreate Controls
            RecreateControls("lblName", "Label");
            RecreateControls("txtValue", "TextBox");
            RecreateControls("btnAddNew", "Button");
        }
 
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int cnt = FindOccurence("txtValue");
 
            CreateLabel("lblName_" + Convert.ToString(cnt + 1));
            CreateTextBox("txtValue_" + Convert.ToString(cnt + 1));
            CreateButton("btnAddNew_" + Convert.ToString(cnt + 1));
 

 
            //ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert",

            //             "<script type = 'text/javascript'>alert('" + ID +

            //              " fired OnTextChanged event5');</script>");
        }
 
        private int FindOccurence(string substr)
        {
 
            string reqstr = Request.Form.ToString();
 
            return ((reqstr.Length - reqstr.Replace(substr, "").Length)
 
                              / substr.Length);
 
        }
 
        private void CreateTextBox(string ID)
        {
            TextBox txt = new TextBox();
 
            txt.ID = ID;
 
            trGenerate.Controls.Add(txt);
 
            Label lblInstance = new Label();
 
            lblInstance.Width = 10;
 
            lblInstance.Height = 20;
 
            trGenerate.Controls.Add(lblInstance);
 
        }
 
        private void CreateLabel(string ID)
        {
            Label lblInstance = new Label();
 
            lblInstance.ID = ID;
 
            lblInstance.Text = "Name";
 
            lblInstance.Width = 50;
 
            trGenerate.Controls.Add(lblInstance);
        }
 
        private void CreateButton(string ID)
        {
            Button btnAddtxt = new Button();
 
            btnAddtxt.ID = ID;
 
            btnAddtxt.Text = "Add New";
 
            btnAddtxt.Click += new System.EventHandler(btnAdd_Click);
 
            btnAddtxt.CausesValidation = true;
 
            trGenerate.Controls.Add(btnAddtxt);
 
            Literal lt = new Literal();
 
            lt.Text = "<br />";
 
            trGenerate.Controls.Add(lt);
        }
 
         
 
        private void RecreateControls(string ctrlPrefix, string ctrlType)
        {
 
            string[] ctrls = Request.Form.ToString().Split('&');
 
            int cnt = FindOccurence(ctrlPrefix);
 
            if (cnt > 0)
            {
                for (int k = 1; k <= cnt; k++)
                {
 
                    for (int i = 0; i < ctrls.Length; i++)
                    {
 
                        if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString())
 
                            && !ctrls[i].Contains("EVENTTARGET"))
                        {
 
                            string ctrlID = ctrls[i].Split('=')[0];
 

 
                            if (ctrlType == "TextBox")
                            {
 
                                CreateTextBox(ctrlID);
 
                            }
 

 
                            if (ctrlType == "Label")
                            {
 
                                CreateLabel(ctrlID);
 
                            }
 

                            if (ctrlType == "Button")
                            {
                                CreateButton(ctrlID);
                            }
                        }
 
                    }
 
                }
 
            }
 
        }
 
    }
Posted 3-Dec-12 2:16am
Edited 3-Dec-12 3:02am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
If everytime you want to create some number of controls then it is good to create single control and then add that user control dynamically. also make sure for every postback your control is regenerated from the codebehind.
 
please check for controls that you already have added binding it's event after postback. add breakpoint there and monitor.
 
hope you got your resolution.
 
best luck
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 380
1 OriginalGriff 230
2 DamithSL 200
3 Zoltán Zörgő 149
4 BillWoodruff 120
0 OriginalGriff 7,610
1 DamithSL 5,559
2 Sergey Alexandrovich Kryukov 5,324
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100