【问题标题】:Determining which control caused PostBack确定哪个控件导致 PostBack
【发布时间】:2013-08-18 01:57:00
【问题描述】:

我有这个aspx:

<asp:ImageButton ID="check" runat="server" ImageUrl="../img/process.png"  OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/>

现在在Page_Load 中,我想确定是PostBack 是否由check 引起的,所以我使用此代码遵循this 问题的方法:

if(FindControl(Page.Request.Params.Get("__EVENTTARGET"))!=check)//if not caused by "check"
    //do something

但是Page.Request.Params.Get("__EVENTTARGET") 是空的。(我在UpdatePanel 中使用我的ImageButton

我怎样才能达到我的目标?

【问题讨论】:

  • 您可以尝试在 IE 开发人员工具中查看。它会告诉您发送的哪个请求有 404 或任何错误代码。这导致了这个问题。它还将帮助您了解哪个请求为您提供了响应。而且时间也过去了!在 IE 中按 F12。
  • 对不起;现在我很忙;我会在接下来的几个小时里检查答案!!!

标签: c# asp.net


【解决方案1】:
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        return;
    Control control = null;
    string controlName = Page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = Page.FindControl(controlName);
    }
    else
    {
        string controlId;
        Control foundControl;
        foreach (string ctl in Page.Request.Form)
        {
            if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
            {
                controlId = ctl.Substring(0, ctl.Length - 2);
                foundControl = Page.FindControl(controlId);
            }
            else
            {
                foundControl = Page.FindControl(ctl);
            }
            if (!(foundControl is Button || foundControl is ImageButton)) continue;
            control = foundControl;
            break;
        }
    }
    Label1.Text = control.ID; // label1 must be in UpdatePanel
}

【讨论】:

    【解决方案2】:

    试试这个:

    Control ctrl = null;
    
    string target = Page.Request.Params.Get("__EVENTTARGET");
    if (!String.IsNullOrEmpty(target))
        ctrl = page.FindControl(target);
    
    if(ctrl == check){
         //check is the control that caused postback
    }
    

    ** 更新 **

    好的,结果 ImageButtons 的功能有点不同。将此用于您的标记:

    <asp:ImageButton onClientClick="setTarget(this.id)" ID="check" runat="server" ImageUrl="../img/process.png"  OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/>
    
    <asp:HiddenField ID="targetId" runat="server" />
    

    现在创建一个 javascript 函数,它将使用启动 PostBack 的字段的 ID 填充我们的隐藏字段:

    function SetSource(id)
    {
        var targetId=
        document.getElementById("<%=targetId.ClientID%>");
        targetId.value = id;
    }
    

    最后我们在 PostBack 中检查它:

    Control ctrl = null;
    
    if (Request.Form[targetId.UniqueID] != null &&
        Request.Form[targetId.UniqueID] != string.Empty)
    {
        ctrl = Page.FindControl(Request.Form[targetId.UniqueID]);
    }
    
    if(ctrl == check){
     //check is the control that caused postback
    }
    

    参考:http://www.aspsnippets.com/Articles/How-to-find-the-control-that-caused-PostBack-in-ASP.Net.aspx

    【讨论】:

    • 什么是目标?如果你的意思是ctrl,是的,它是null
    • 我的意思是变量targetPage.Request.Params.Get("__EVENTTARGET") 返回什么?
    • Page.Request.Params.Get("__EVENTTARGET") 返回“”!
    【解决方案3】:

    由于这是一个更新面板,请尝试通过ScriptManager 获取值,如下所示:

    protected void Page_Load(object sender, EventArgs e)
    {
        var updatePanelControlIdThatCausedPostBack = String.Empty;
        var scriptManager = ScriptManager.GetCurrent(Page);
    
        if (scriptManager != null)
        {
            var smUniqueId = scriptManager.UniqueID;
            var smFieldValue = Request.Form[smUniqueId];
    
            if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains("|"))
            {
                updatePanelControlIdThatCausedPostBack = smFieldValue.Split('|')[1];
            }
        }
    
        if (!String.IsNullOrEmpty(updatePanelControlIdThatCausedPostBack))
        {
            // Do something with control ID value that caused UpdatePanel postback here
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果控件导致PostBack,则其ID在请求集合中将不是nullForm.Request[controlID]

      如果是Image,则请求集合中有两项,一项用于 x 坐标,一项用于 y 坐标,用于表示鼠标在图像中的确切位置。

      这样做:

      if(Form.Request["Img1.x"] != null)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        相关资源
        最近更新 更多