Getting control that fired postback in page_init

后端 未结 1 1926
我寻月下人不归
我寻月下人不归 2021-01-14 01:20

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 t

相关标签:
1条回答
  • 2021-01-14 01:55

    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>
    
    0 讨论(0)
提交回复
热议问题