【问题标题】:EVENTTARGET Problem determing senderEVENTTARGET 确定发件人的问题
【发布时间】:2011-04-19 00:35:22
【问题描述】:

我试图弄清楚点击了哪个按钮,这段代码在 IE 中运行良好,但如果我使用 Chrome、Firefox 或 Safari,它什么也做不了。在 firefox 中使用 firebug 时,我查看了 Form Details,它显示 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;

代码调用方法:

        if (Page.IsPostBack)
        {
            try
            {
                Control cause = GetPostBackControl(Page);
                string statecause = cause.ID;
                if (statecause == "buttonName1")
                {
                    search(statecause);
                }
                else if (statecause == "buttonNAME2")
                {
                    resetfields();
                }
            }
            catch { }
        }

【问题讨论】:

    标签: c# asp.net forms


    【解决方案1】:

    最好的方法是确定导致回发的控制是覆盖protectedPage.RaisePostBackEvent方法。 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);
        }
    }
    

    您提供的代码应该仅适用于客户端__doPostBack 函数呈现到页面时的场景(例如,如果您使用像&lt;asp:Button runat="server" ID="btnSubmit" Text="submit" UseSubmitBehavior="true" /&gt; 这样的唯一一个按钮,它将不会被呈现)。

    即使在呈现__doPostBack 函数的情况下,但__EVENTTARGET 参数为空,这意味着在大多数情况下自定义/不兼容的javascript 代码违反了__doPostBack 函数的默认行为。在这种情况下,即使是 ASP.NET 基础设施也无法正确处理回发事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 2011-01-02
      • 2020-04-17
      • 2017-01-16
      • 1970-01-01
      • 2011-01-14
      相关资源
      最近更新 更多