ASP: runat=server for dynamic control
Posted
by
Jan
on Stack Overflow
See other posts from Stack Overflow
or by Jan
Published on 2012-07-05T02:43:40Z
Indexed on
2012/07/05
3:15 UTC
Read the original article
Hit count: 257
ASP.NET
|runatserver
In the Page_Load method I create a couple of controls, based on various conditions. I would like to register server side code with those controls. However, for the last part I need to declare my controls as server controls. This is normally done by runat=server, but I don't know how to set this attribute in the C# code. myControl.Attributes.Add("runat", "server") does not do the trick. This one works, meaning that the "test" method is called when I click on it:
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="test">testtext</asp:LinkButton>
This one does not work:
LinkButton lb = new LinkButton();
lb.ID = "LinkButton1";
lb.OnClientClick = "test";
lb.Text = "testtext";
lb.Attributes.Add("runat", "server");
I can click on it, and the page is loaded, but the test-method is not called.
Any hints?
© Stack Overflow or respective owner