0

In my application I have created a button dynamically & its name is "Dynamic_Button". Is it possible to make the button to be runat=server. I just tried the code but it doesn't work.

Dynamic_Button.Attributes.Add("runat","server");

Is there anyother ways to make it in serverside ?

bash.d
  • 13,029
  • 3
  • 29
  • 42
Elango_Thanumalayan
  • 105
  • 1
  • 2
  • 10

2 Answers2

0

When you manually instantiate a server control, there's no need to add the runat="server" attribute. This is a special attribute that's used only by the ASP.NET page parser to distinguish server controls from other markup.

The OnClick attribute in markup corresponds to the Click server-side event, which you hook up using the += operator.

So:

LinkButton lb = new LinkButton();
lb.ID = "LinkButton1";
lb.Click += click_event;
lb.Text = "testtext";

And the event handler

protected void click_event(object sender, EventArgs e)
{
}

In this example there is no need to add runat server because it already has it.

Have a look at this answer: https://stackoverflow.com/a/11337397/284240

Community
  • 1
  • 1
Nick N.
  • 12,902
  • 7
  • 57
  • 75
  • Yep, and cleaned it a bit. – Nick N. May 02 '13 at 09:37
  • It's ok to quote another answer if you add the reference(what you've done now), but the answer is still a little bit off-topic since OP has no `LinkButton` and he also don't ask how to add a click-event programmatically. – Tim Schmelter May 02 '13 at 09:40
  • Yes you are right, but I want to show that is not needed to add a runat server attribute. – Nick N. May 02 '13 at 09:49
0

This and this will guide you in creating and adding buttons at run time as well assigning events(client and server side).

Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61