注册asp:button单击事件处理程序 [英] Registering asp:button click event handler

查看:85
本文介绍了注册asp:button单击事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个使用jQuery动态创建的asp:button.

I have multiple asp:button that I created dynamically with jQuery.

对于每个人,我都有 OnClick ="ibtn_Click" .

但是在后面的代码上,我必须在Page_Load..like .. button.Click + = new EventHandler(this.ibtn_Click); 上手动注册每个.

But on the code behind, I have to manually register each one of them on Page_Load..like.. button.Click += new EventHandler(this.ibtn_Click); .

有没有一种方法可以自动注册此处理程序?还是可以用ajax jquery注册它?

Is there a way I can register this handler automatically? Or is it possible to register it with ajax jquery?

谢谢!

推荐答案

您不能在客户端的浏览器中添加ASP.NET Web控件.ASP.NET将这些< asp:button> 标记转换为常规HTML,如下所示:

You can't add ASP.NET web controls in the client's browser. ASP.NET transforms those <asp:button> tags into regular HTML, something like this:

<input type="submit" name="Button1" value="Ok" id="Button1" />

当用户单击这些提交控件之一时,浏览器将发送POST请求,其中包括按钮的 name 作为发布数据的一部分(您可以通过 Form ASP.NET中的集合):

When a user clicks one of these submit controls, the browser sends a POST request, which includes the button's name as part of the posted data (which you can access via the Form collection in ASP.NET):

if (this.IsPostBack && Request.Form["Button1"] != null) {
    // do something if the user clicked Button1
}

ASP.NET自动将服务器控件的点击绑定到 OnClick 属性中指定的方法.它在 Page.Load 完成后调用该方法(有关详细信息,请参见" ASP.NET页面生命周期概述).

ASP.NET automatically binds server control clicks to the methods specified in the OnClick attribute. It calls the method after Page.Load completes (for the gory details, see "ASP.NET Page Life Cycle Overview").

如果要在客户端上添加按钮,则可以自己完成所有这些操作.例如,您可以绑定" Page_PreRender 中的动态生成的按钮:

If you're adding the buttons on the client, you can do all this yourself. For example you could "bind" the dynamically generated button in Page_PreRender:

protected void Page_PreRender(object sender, EventArgs e) {
    if (this.IsPostBack && Request.Form["Button1"] != null) {
        // Call another method on the page
    }
}

最后,在这个时代,如果合适的话,您绝对应该考虑仅通过jQuery的 ajax 调用页面方法.请参阅这篇出色的Encosia文章对该技术进行介绍.

Finally, in this day and age, if it's appropriate, you should definitely consider just calling page methods via jQuery's ajax. See this excellent Encosia article for an introduction to that technique.

这篇关于注册asp:button单击事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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