问题:按钮的onclick()没有触发 [英] problem: Button's onclick() not firing

查看:185
本文介绍了问题:按钮的onclick()没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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");
       }





我正在使用此代码创建一个按钮。按钮出现在屏幕上,但问题是onclick()事件处理程序没有触发。请帮助我..



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..

推荐答案

这里,您使用的是纯HTML控件,而不是ASP.NET控件(可以在服务器部件上使用回发)。在您的情况下,您无需在服务器端调用 btn_onclick ,因此您需要 btn_onclick 作为Javascript函数。在HTML或外部文件中相应地定义它,忘记C#函数。或者,如果您真的想从按钮单击的Javascript处理程序中调用某个.NET方法,则可以使用Ajax发送HTTP请求:

http://en.wikipedia.org/wiki/Ajax_%28programming%29 [ ^ ],

http://en.wikipedia.org/wiki/List_of_Ajax_frameworks [ ^ ],

https://api.jquery.com/jQuery。 ajax [ ^ ]。



或者,您可以使用ASP.NET 按钮控件:http://quickstarts.asp.net/ QuickStartv20 / aspnet / doc / ctrlref / standard / button.aspx [ ^ ]。



问题表明你不知道Web是如何工作的,尤其是ASP.NET。请从这里开始: http://www.asp.net/get-started [ ^ ]。



是的,来自一开始。而且我不确定这对你来说是一个开始。你对.NET充满信心吗?如果是这样,我不确定您是否了解Web机制,以及服务器和客户端部件上发生的情况。也许你还需要这个:

http://en.wikipedia.org/wiki/HTTP [ ^ ],

< a href =http://en.wikipedia.org/wiki/WWW> http://en.wikipedia.org/wiki/WWW [ ^ ],

http://en.wikipedia.org/wiki/WorldWideWeb [ ^ ]。



-SA
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


根据我的理解你的代码,你想从后面的代码添加一个按钮到你的页面,单击按钮时按钮点击事件应该被激活。如果我的理解是正确的,那么这里有两件事。

1)为什么要添加HTML按钮控件。您可以通过使用ASP.NET按钮控件来实现相同的目标。



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)如果您只想使用HTML控件,则需要通过JS或AJAX处理它。



请浏览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.


这篇关于问题:按钮的onclick()没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆