Taking control of what to burn postback in page_init

I have a gridview that includes a dynamically created dropdownlist. When changing the dropdowns and doing a bulk update on the grid (btnUpdate.click), I need to create controls in init so that they are accessible to viewstate. However, I have a few other buttons that also trigger the postback, and I don't want to create controls in the init page, but later in button click events.

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

+4
source share
1 answer

You can determine which control called PostBack by looking at Request.Form["__EVENTTARGET"] . The problem is that the ids buttons will not be displayed unless you set their UseSubmitBehavior to false . Here is 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> 
+7
source

Source: https://habr.com/ru/post/1304591/


All Articles