【问题标题】:How to know the target control for postback in asp.net如何知道asp.net中回发的目标控件
【发布时间】:2013-06-19 13:06:24
【问题描述】:

首页

<html >
<head >
</head>
<body>
<form id="form1" runat="server">

<asp:Button ID="btnSubmit" runat="server" 
            OnClick="btnSubmit_Click"            
            Text="Submit to Second Page" />
</div>
</form>
</body>

btnSubmit_Click 事件

Response.redirect("Page2.aspx");

在Page2的Page Load中,如何查找是哪个按钮导致了postback?

【问题讨论】:

  • 你可以使用 URL 参数。
  • 这不是重复的,因为提供的链接是针对同一页面的,而我的链接是针对不同页面的。
  • @Dylan 你是对的,对不起。我看错了

标签: c# asp.net


【解决方案1】:

btnSubmit_Click 事件中,您可以传递查询字符串参数并在Page2 中获取查询字符串参数。

Response.redirect("Page2.aspx?btnName=button1");

在Page2.aspx页面的加载事件中

protected void Page_Load(object sender, EventArgs e)
{
      string queryString = Request.QueryString["btnName"].ToString();
      //Here perform your action
}

【讨论】:

    【解决方案2】:

    Page_Load 中,无法找到导致PostBack 的按钮。在第 2 页的观点中,它甚至是不是帖子。这是一个全新的要求。这就是 Response.Redirect 所做的,它指示客户端浏览器执行一个新请求。

    如果你真的需要知道,你可以添加一个 URL 参数,并将其作为查询字符串获取,因为 Pankaj Agarwal 展示了他的答案。

    评论后编辑:除了查询字符串,您还可以在 onClick 中使用Session

    Session["POST-CONTROL"] = "button2"
    

    Page_Load 中,您将其解读为:

    var postControl = Session["POST-CONTROL"] != null ? Session["POST-CONTROL"].toString() : "";
    

    【讨论】:

      【解决方案3】:

      如果您确实需要帖子来自哪个按钮的信息,您可以实现Cross-Page Posting。这是一篇带有示例的好文章。与使用 QueryString 相比,我更喜欢这种方法(实现起来可能会稍微快一些)。

      【讨论】:

      • 使用Cross-Page Posting如何知道回发调用了哪个按钮。
      • 我喜欢this approach
      【解决方案4】:

      尝试过这个解决方案吗? 这里:On postback, how can I check which control cause postback in Page_Init event

      public static string GetPostBackControlId(this Page page)
      {
          if (!page.IsPostBack)
              return string.Empty;
      
          Control control = null;
          // first we will check the "__EVENTTARGET" because if post back made by the controls
          // which used "_doPostBack" function also available in Request.Form collection.
          string controlName = page.Request.Params["__EVENTTARGET"];
          if (!String.IsNullOrEmpty(controlName))
          {
              control = page.FindControl(controlName);
          }
          else
          {
              // if __EVENTTARGET is null, the control is a button type and we need to
              // iterate over the form collection to find it
      
              // ReSharper disable TooWideLocalVariableScope
              string controlId;
              Control foundControl;
              // ReSharper restore TooWideLocalVariableScope
      
              foreach (string ctl in page.Request.Form)
              {
                  // handle ImageButton they having an additional "quasi-property" 
                  // in their Id which identifies mouse x and y coordinates
                  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;
              }
          }
      
          return control == null ? String.Empty : control.ID;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-21
        • 2011-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        相关资源
        最近更新 更多