如何识别回发事件在Page_Load中 [英] How to Identify Postback event in Page_Load

查看:107
本文介绍了如何识别回发事件在Page_Load中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些遗留的code,需要在哪些事件导致回发的Page_Load中辨认。
目前,这是通过检查请求的数据是这样实现的...

We have some legacy code that needs to identify in the Page_Load which event caused the postback. At the moment this is implemented by checking the Request data like this...

如果(的Request.Form [__ EVENTTARGET]!= NULL

&功放;&安培; (的Request.Form [__ EVENTTARGET]。的IndexOf(BaseGrid)> -1 // BaseGrid事件(例如排序)

        ||的Request.Form [btnSave]!= NULL //保存按钮

if (Request.Form["__EVENTTARGET"] != null
&& (Request.Form["__EVENTTARGET"].IndexOf("BaseGrid") > -1 // BaseGrid event ( e.g. sort)
       || Request.Form["btnSave"] != null // Save button

这是pretty丑陋和休息,如果有人重命名的控制。是否有这样做的更好的办法?

This is pretty ugly and breaks if someone renames a control. Is there a better way of doing this?

重写每个网页,使得它不需要在Page_Load中检查这不是此刻的一个选项。

Rewriting each page so that it does not need to check this in Page_Load is not an option at the moment.

推荐答案

这应该让你的控制造成回发的:

This should get you the control that caused the postback:

public static Control GetPostBackControl(Page page)
{
    Control control = null;

    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != string.Empty)
    {
        control = page.FindControl(ctrlname);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control c = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control = c;
                break;
            }
        }
    }
    return control;
}

了解更多关于此此页上:
<一href=\"http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx\">http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx

这篇关于如何识别回发事件在Page_Load中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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