Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
protected void Page_Load(object sender, EventArgs e)
       {

           StringBuilder sb = new StringBuilder("");
           sb.Append(@" <input  type='button' runat='server' onclick='btn_onclick' value='click me' id='bt'/>");
           Page.Controls.Add(Page.ParseControl(sb.ToString()));
       }
       protected void btn_onclick(object sender, EventArgs e)
       {
           Response.Write("Test");
       }


i am using this code to create a button. The button appears on the screen but, the problem is that the onclick() event handler is not firing. please help me..
Posted
Updated 15-Apr-14 18:06pm
v3
Comments
Richard C Bishop 15-Apr-14 16:46pm    
Why the double-quotes and single-quote around runat?
Sergey Alexandrovich Kryukov 15-Apr-14 16:57pm    
Here is the thing: "runat" is irrelevant: this is not the ASP.NET button, but HTML button. "Runat" is not applicable. Please see my answer.
—SA
Arsalaan Ahmed 15-Apr-14 16:50pm    
its single quote mistake in writing
ZurdoDev 15-Apr-14 17:06pm    
SA's solution is right but I have to ask what are you trying to accomplish? Surely there is a better way.

Here, you are using pure HTML control, not ASP.NET control (which could work on the server part, with postback). In your case, you do nothing to call btn_onclick on server side, so you need btn_onclick to be a Javascript function. Define it accordingly in your HTML or external file, forget about C# function. Or, if you really want to invoke some .NET method from your Javascript handler of the button click, you can send an HTTP request using Ajax:
http://en.wikipedia.org/wiki/Ajax_%28programming%29[^],
http://en.wikipedia.org/wiki/List_of_Ajax_frameworks[^],
https://api.jquery.com/jQuery.ajax[^].

Alternatively, you can use ASP.NET Button control: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/standard/button.aspx[^].

The question suggests that you have no idea on how Web works, especially ASP.NET. Please start here: http://www.asp.net/get-started[^].

Yes, from the very beginning. And I'm not really sure it would be a beginning for you. Are you confident with .NET? If so, I'm not sure you understand the Web mechanisms, what happens on server and client parts. Perhaps you would need also this:
http://en.wikipedia.org/wiki/HTTP[^],
http://en.wikipedia.org/wiki/WWW[^],
http://en.wikipedia.org/wiki/WorldWideWeb[^].

—SA
 
Share this answer
 
Comments
Rahul VB 16-Apr-14 0:04am    
Very nice Sir,
-Rahul
Sergey Alexandrovich Kryukov 16-Apr-14 0:21am    
Thank you, Rahul.
—SA
As per my understanding from your code, you want to add a button to your page from the code behind and the button click event should fired up upon clicking the button. If my understanding is correct then, here are 2 things.
1) Why you want to add a HTML button control. You can achieve the same by using the ASP.NET button control.

protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.ID = "bt";
btn.Text = "click me";
btn.Click += new EventHandler(btn_onclick);
form1.Controls.Add(btn);
}
protected void btn_onclick(object sender, EventArgs e)
{
Response.Write("Test");
}

2)In case, you want to use only HTML control, you need to handle it through JS or AJAX.

Please go through the links provided by SA to understand more detail about the way these controls work.
 
Share this answer
 

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