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?
First off, you don't need that
lb.Attributes.Add("runat", "server")
line. Allrunat="server"
does is make the control visible to the code-behind. Since you're adding the control in the code-behind, that's a moot point.As for the script, note that the one that works is usingOnClick
and your broken one is usingOnClientClick
. The difference is important becauseOnClick
is natively understood by javascript, and it will find the function with the nametest
and execute it.OnClientClick
, on the other hand, is arbitrary javascript that the ASP.NET runtime binds to the click event manually. Meaning, it's just going to executetest
. That's nothing.test()
, on the other hand, is proper javascript.So, change that line to
lb.OnClickClick = "test()";
edit: I mixed myself up, see the other answer.
Are you trying to register server side event? If so you can do like this.
Event
You almost got it right. Just a couple things:
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.OnClick
attribute in markup corresponds to theClick
server-side event, which you hook up using the+=
operator. (On the other hand, theOnClientClick
attribute in markup corresponds to theonclick
client-side attribute, which typically contains a snippet of JavaScript code. The fact thatOnClick
doesn't correspond toonclick
is admittedly a bit confusing.)Thus:
And your event handler (which you can even make
private
if there are no references to it from markup):You don't need to. Since the controls are created on the server side via C# code, they are already server-side controls. The problem you are experiencing has to do with the page life cycle: when you create a control on the server side as you are doing now, you need to re-add the control on every postback and rehook any event handlers that you want to handle for them.
Also note that your code shows 2 different things: on the markup that you posted, you are displaying a handler for the
OnClick
event whereas on the C# code you are adding a handler for theOnClientClick
event. They are 2 different things. TheOnClientClick
event simply fires any javascript code that you have on your page when the control is clicked. It's exactly the same as doing this: