Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 事件目标问题确定发送方_C#_Asp.net_Forms - Fatal编程技术网

C# 事件目标问题确定发送方

C# 事件目标问题确定发送方,c#,asp.net,forms,C#,Asp.net,Forms,我正试图弄清楚点击了什么按钮,这段代码在IE中运行得很好,但如果我使用Chrome、Firefox或Safari,它就什么都做不了。在firefox中使用firebug时,我查看了表单的详细信息,它显示EVENTTARGET没有值,只是空白。我怎样才能让它在FF、Chrome和Safari上工作 方法: Control postbackControlInstance = null; string postbackControlName = page.Request

我正试图弄清楚点击了什么按钮,这段代码在IE中运行得很好,但如果我使用Chrome、Firefox或Safari,它就什么都做不了。在firefox中使用firebug时,我查看了表单的详细信息,它显示EVENTTARGET没有值,只是空白。我怎样才能让它在FF、Chrome和Safari上工作

方法:

       Control postbackControlInstance = null;

        string postbackControlName = page.Request.Params.Get("__EVENTTARGET");
        if (postbackControlName != null && postbackControlName != string.Empty)
        {
            postbackControlInstance = page.FindControl(postbackControlName);
        }
        else
        {
            for (int i = 0; i < page.Request.Form.Keys.Count; i++)
            {
                postbackControlInstance = page.FindControl(page.Request.Form.Keys[i]);
                if (postbackControlInstance is System.Web.UI.WebControls.Button)
                {
                    return postbackControlInstance;
                }
            }
        }
        if (postbackControlInstance == null)
        {
            for (int i = 0; i < page.Request.Form.Count; i++)
            {
                if ((page.Request.Form.Keys[i].EndsWith(".x")) || (page.Request.Form.Keys[i].EndsWith(".y")))
                {
                    postbackControlInstance = page.FindControl(page.Request.Form.Keys[i].Substring(0, page.Request.Form.Keys[i].Length - 2));
                    return postbackControlInstance;
                }
            }
        }
        return postbackControlInstance;

最好的方法是确定导致回发的控件是覆盖
受保护的
页面。RaisePostBackEvent
方法。ASP.NET Infrastructure使用此方法通知导致回发的服务器控件它应处理传入的回发事件:

public class MyPage : Page
{
    protected override void RaisePostBackEvent(
        IPostBackEventHandler sourceControl, 
        string eventArgument
    )
    {
        // here is the control that caused the postback
        var postBackControl = sourceControl;

        base.RaisePostBackEvent(sourceControl, eventArgument);
    }
}
当客户端
\uuu doPostBack
功能仅呈现到页面时(例如,如果仅使用一个按钮,如
,则不会呈现),您提供的代码应适用于scenarious

如果即使在呈现
\uuuuu doPostBack
函数的情况下,但
\uuuu EVENTTARGET
参数为空,这意味着在大多数情况下,
\uuuu doPostBack
函数的默认行为被自定义/不兼容的javascript代码违反。在这种情况下,即使ASP.NET基础结构也无法正确处理回发事件

public class MyPage : Page
{
    protected override void RaisePostBackEvent(
        IPostBackEventHandler sourceControl, 
        string eventArgument
    )
    {
        // here is the control that caused the postback
        var postBackControl = sourceControl;

        base.RaisePostBackEvent(sourceControl, eventArgument);
    }
}