Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added html control using string bulder and pass to div tag like below
C#
StringBuilder sbTest = new StringBuilder(string.Empty);

sbTest.Append("<input type='text'  runat="server" name='txtName' id='txtId' />");

dv.InnerHtml = sbTest.ToString();

Now I want to access that textbox in code behind .cs file.How can I access that?
Posted
Updated 23-Sep-13 2:40am
v3

1 solution

Why do you use the StringBuilder?? Why not just :
XML
dv.InnerHtml = "<input type="text" runat="server" name="txtName" id="txtId" />";

The StringBuilder makes no sense at all in your code.

Further more, if you just add a textbox to your page, why don't you just drag the textbox on our webform? If you want thetextbox to post to a code-behind, you may just want to always add the textbox to your page and hide it using css.

Eduard
 
Share this answer
 
Comments
PrachiMhatre 23-Sep-13 8:50am    
Actually I have to create html control dynamically. For that I have used Stringbuilder.
Eduard Keilholz 23-Sep-13 8:54am    
A StringBuilder is only faster when adding 3 or more 'pieces' of string into one. You cannot add the runat="server" attribute to a control dynamically. I think the best way is to just add the control to your form and hide it with CSS, then show it once nessecary.
piyush_singh 23-Sep-13 9:07am    
In asp.net if you would add a control dynamically then, you will not get its value on postback. Once a Page is PostBack, SaveStateComplete event is raised after values of all the controls will be saved in the ViewState, which will conveniently ignore your label as it was added after the Page was rendered. And hence on Page load, you will get a new label.

I would suggest, that if the no. of Labels are fixed, then simply change their visibility using css. It will be much simpler to implement.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900