1

I have a webforms page with a gridview and several other buttons. In debugging, I've noticed all the binding to gridview executes during postback in the Page_Load sub. Only after this is all done do the click handler(s) get invoked and in my case the handler does a Response.Redirect to another page.

So, I found this on SO: Run the button event handler before page_load.

It suggests the possibility of detecting the target of the postback during page_load and I was thinking of exiting the page_load in this case and letting the button click handler load a new page (i.e. avoiding all the wasted building and formatting of the gridview.

Here is a snippet from my Page_Load:

    If IsPostBack Then
        Dim targetOfPostBack As String = Request.Params("__EVENTTARGET").ToString()
    End If

During PostBack the __EVENTTARGET is always an empty string rather than something suggesting the "add" button I clicked (i.e. ctl00$MainContentPlaceHolder$btnAddEvent). Trapped here: enter image description here

The OP on SO above is expressing this same approach in C# while my implementation is VB.Net. What am I doing wrong?

Community
  • 1
  • 1
John Adams
  • 4,773
  • 25
  • 91
  • 131

3 Answers3

1

On your asp button try setting UseSubmitBehavior="False"

Use the UseSubmitBehavior property to specify whether a Button control uses the client browser's submit mechanism or the ASP.NET postback mechanism. By default the value of this property is true, causing the Button control to use the browser's submit mechanism. If you specify false, the ASP.NET page framework adds client-side script to the page to post the form to the server.

When the UseSubmitBehavior property is false, control developers can use the GetPostBackEventReference method to return the client postback event for the Button. The string returned by the GetPostBackEventReference method contains the text of the client-side function call and can be inserted into a client-side event handler.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior(v=vs.110).aspx

Rick S
  • 6,476
  • 5
  • 29
  • 43
  • Thanks. Adding UseSubmitBehavior FALSE now lets me see the targetOfPostBack = "ctl00$MainContentPlaceHolder$btnAddEvent". How would I use this string to find the type of control that caused PostBack? In short, if the PostBack is for the gridview, I want to finish out Page_Load by calling my LoadGridView sub which binds the datasource to the gridview; if any other control such as the above Button, I'd like to exit Page_Load and let the button click handlers do their thing (i.e. avoiding the whole gridview thing). How to parse the string to determine control type (perhaps FindControl?) – John Adams Aug 28 '14 at 21:50
0

You can write a client side script to redirect the page thus avoiding the postback all together. Set "OnClientClick" (Asp.NET Button) or "OnClick" (HTML Buttom) property and then simply return false.

Eg:

Script:

             function RedirectToURL()             
            try {
             window.open("<Your URL Here>", "_self", "menubar=0,resizable=             
  } catch (e) {

        }
         return flase;
     }

Button :

 <input type="button" id="btnToggleConfig" onclick="RedirectToURL()" runat="server"
                        value="Click here to Goto Link" />
0

This is the ASP.NET page life cycle at work: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.85).aspx

A few options:

  1. check out this post for _EVENTTARGET being empty: __EVENTTARGET is empty on postback of button click
  2. Handle the redirect on the client side (if possible)
  3. Only fill the gridview only once on page_load (If Not IsPostback) if that's an option
  4. If you're only worried about the gridview being recreated and eating up some 'processing power' or time, depending on size, users, etc, may not be a big issue, so could leave as-is.
Community
  • 1
  • 1
user2175152
  • 50
  • 1
  • 8