Reputation: 921
How do you dynamically add an asp taged control to a form on asp.net C# code behind? I am trying to do something similar to the following, but specifically where the string can change from Button to Textbox if needed.
string control1 = "asp:button";
HtmlGenericControl xControl = new HtmlGenericControl(control1);
body1.Controls.Add(xControl);
Is there a way to do this on a ASP.Net webform? I have tried the HtmlGenericControl but this is obviously not going to work. Any suggestions?
Upvotes: 0
Views: 1588
Reputation: 707
You can by adding div runat server c# code
Button btn = new Button();
btn.Text = "test";
maindiv.Controls.Add(btn);
html
<body>
<form id="form1" runat="server">
<div runat="server" id="maindiv">
</div>
</form>
</body>
https://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx
Upvotes: 2