Getting control that fired postback in page_init

荒凉一梦 提交于 2019-12-01 05:25:09

问题


I have a gridview that includes dynamically created dropdownlist. When changing the dropdown values and doing a mass update on the grid (btnUpdate.click), I have to create the controls in the page init so they will be available to the viewstate. However, I have several other buttons that also cause a postback and I don't want to create the controls in the page init, but rather later in the button click events.

How can I tell which control fired the postback while in page_init? __EVENTTARGET = "" and request.params("btnUpdate") is nothing


回答1:


It is possible to determine which control caused a PostBack by looking at Request.Form["__EVENTTARGET"]. The problem with this is that button ids will not show unless you set their UseSubmitBehavior to false. Here's an example:

.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        switch (Request.Form["__EVENTTARGET"].ToString())
        {
            case "ddlOne":
                break;
            case "btnOne":
                break;
            case "btnTwo":
                break;
        }
    }
}

.aspx

<form id="form1" runat="server">
  <asp:DropDownList ID="ddlOne" AutoPostBack="true" runat="server">
      <asp:ListItem Text="One" Value="One" />
      <asp:ListItem Text="Two" Value="Two" />
  </asp:DropDownList>  
  <asp:Button ID="btnOne" Text="One" UseSubmitBehavior="false" runat="server" />
  <asp:Button ID="btnTwo" Text="Two" UseSubmitBehavior="false" runat="server" />
</form>


来源:https://stackoverflow.com/questions/2479175/getting-control-that-fired-postback-in-page-init

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!