Dynamically Adding Textboxes
-
I have a placeholder sitting on a page with a button above it. When the button is clicked, I want it to add a textbox and to the placeholder so I have the following code execute for the onclick...
TextBox temp = new TextBox(); temp.TextMode = TextBoxMode.MultiLine; temp.CssClass = "txtArea"; temp.ID = "txtArea" + phModNotes.Controls.Count.ToString(); phModNotes.Controls.Add(temp);
The expectation being that the controls will be named sequentially starting at txtArea0 and counting up. However, everytime I click the button, phModNotes gets reloaded and is empty. [Reposted] Question I asked in the C# forum but I was pointed to this forum instead [/Reposted] -
I have a placeholder sitting on a page with a button above it. When the button is clicked, I want it to add a textbox and to the placeholder so I have the following code execute for the onclick...
TextBox temp = new TextBox(); temp.TextMode = TextBoxMode.MultiLine; temp.CssClass = "txtArea"; temp.ID = "txtArea" + phModNotes.Controls.Count.ToString(); phModNotes.Controls.Add(temp);
The expectation being that the controls will be named sequentially starting at txtArea0 and counting up. However, everytime I click the button, phModNotes gets reloaded and is empty. [Reposted] Question I asked in the C# forum but I was pointed to this forum instead [/Reposted]Each time you call the page, remember that asp.net has to recreate all the control, by parsing the aspx page. So each time it loads it will create a new placeholde object which has 0 child controls, because that it what is in you aspx page. So on each successive post you will need to add code to work out how many text boxes to add, depending on how many times the button has been click. As a suggestion you could store the number of textboxes within a viewstate variable. Hope this points you in the right direction.
-
I have a placeholder sitting on a page with a button above it. When the button is clicked, I want it to add a textbox and to the placeholder so I have the following code execute for the onclick...
TextBox temp = new TextBox(); temp.TextMode = TextBoxMode.MultiLine; temp.CssClass = "txtArea"; temp.ID = "txtArea" + phModNotes.Controls.Count.ToString(); phModNotes.Controls.Add(temp);
The expectation being that the controls will be named sequentially starting at txtArea0 and counting up. However, everytime I click the button, phModNotes gets reloaded and is empty. [Reposted] Question I asked in the C# forum but I was pointed to this forum instead [/Reposted] -
Each time you call the page, remember that asp.net has to recreate all the control, by parsing the aspx page. So each time it loads it will create a new placeholde object which has 0 child controls, because that it what is in you aspx page. So on each successive post you will need to add code to work out how many text boxes to add, depending on how many times the button has been click. As a suggestion you could store the number of textboxes within a viewstate variable. Hope this points you in the right direction.
Thanks for pointing me in the right direction. It works good!
-
I don't know if I got it right but isn't this better to use client side scripting in such scenario? If yes, then simply create those controls using javascript. If no, then please let me know why. Regards, Zee_Zee
I want the controls to runat="server" and i don't think adding them via client side will work.
-
I don't know if I got it right but isn't this better to use client side scripting in such scenario? If yes, then simply create those controls using javascript. If no, then please let me know why. Regards, Zee_Zee
Depends on what he's trying to do in the long run. I don't think we have enough information either way, but if they are created on the client side using javascript then they will not be part of the ASP.NET control structure and not able to participate in the ViewState, or be accessed from the asp.net as a control. Unless I'm wrong, which I could be...