1

I have an ASP DropDownList control with the AutoPostBack property set to true. When the user changes the selection, the form posts back as expected. I want to know how to determine, in the code-behind, whether the page posted back for this specific reason.


I know that I can define an event handler like this...
protected void MyDropDownList_SelectedIndexChanged(object sender, EventArgs e) {
    // Run some code if the selection for the "MyDropDownList" control was changed
}

...but what I want to know is how to check whether the form posted back because the selected index was changed outside the event handler.

Specifically, in the Page_Load() method, I have an if (IsPostback) {} section, and I want this section not to execute if the postback was caused by changing the selection in the DropDownList. So, in pseudocode, I want something like:

if (IsPostback && (! <DropDownList's selection was changed, causing an autopostback>)) {

I tried defining a global boolean variable and setting it to true in an event handler, then checking it in Page_Load(), like this:

public partial class MyWebApp : System.Web.UI.Page {
    [...]
    static bool selectedIndexChanged = false;
    [...]
    protected void DomainDropDownList_SelectedIndexChanged(object sender, EventArgs e) {
        selectedIndexChanged = true; // Set this flag to true if selected index was changed
    }
    [...]
    protected void Page_Load(object sender, EventArgs e) {
    [...]
        if (IsPostBack && selectedIndexChanged == false) { 
            [...]
        }
        [...]

This didn't work for a reason I presume experienced ASP.NET developers will easily spot: the event handler executes after Page_Load(), regardless of the order of the code.

I also tried to see if the control's selectedIndexChanged event can be used as a boolean condition to determine if the event triggered, like this

if (IsPostBack && !MyDropDownList.SelectedIndexChanged) {

but Visual Studio gives me the following error:

The event 'System.Web.UI.WebControls.ListControl.SelectedIndexChanged' can only appear on the left hand side of += or -="

A search on the error message led to this answer, but that doesn't seem to help because it relies on the event handler, which executes after Page_Load().

In my particular use case, where there is only one DropDownList and only one other way to submit the form (the submit button), it would be equally effective to check whether the selected index was changed, whether an AutoPostBack was triggered, or whether the submit button was clicked, but I'd also like to know how to do this in a broader range of scenarios, for example if there are multiple AutoPostBack controls and/or multiple ways to submit the form other than AutoPostBack.


So, my question breaks down as follows (though some of these may be essentially the same question, depending on what the answer is):

  • Is there a way to determine in general whether an AutoPostBack was triggered, as opposed to the form posting back for any other reason, such as clicking a button?
  • Is there a way to determine if a specific control's AutoPostBack was triggered (i.e. if there are multiple controls with AutoPostBack true, can it be determined which control caused the AutoPostBack)?
  • Is it possible to check in the Page_Load() method or any other code that executes before the SelectedIndexChanged event handler whether a DropDownList's selected index was changed?

If there's a better way to achieve what I'm trying to accomplish in this particular case, I'm open to suggestions, but I'd still like to know the answer(s) to the above.

Community
  • 1
  • 1
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
  • Have you used debugging tools to look at what gets posted back? It could be there is some property of the request that you can use and the easiest way to check is to look at what is posted. – Guvante Jun 01 '15 at 22:22
  • I'm not sure how to do that, I'm very new to ASP.NET and Visual Studio. It seems to me that this is a very basic task and there should be an easy way to do it, but I've had no luck either trying to google it or trying to guess at it. – Adi Inbar Jun 01 '15 at 22:37
  • Look up developer tools for the browser you use, for instance I use chrome and access the tools via F12. – Guvante Jun 01 '15 at 22:47
  • _Generally speaking_ if you can rely on Events, you'll simplify things. In other words, if you can determine what needs to actually run on `Load` vs. what _can_ be handled by control events (after `Load`), then it will make life easier. Otherwise, the answer by Matt below is it (aka "bare metal" inspection of POST). At some point though if you have a bunch of controls, it will get unwieldy...Hth... – EdSF Jun 01 '15 at 23:51
  • I'm not sure how you can rely on events if you want a bunch of code to *not* execute if a certain event happens. It occurred to me that since in my case the submit button is currently the only other way to post the form back, I can just move the contents of the `if (IsPostBack...) section to an event handler for clicking the button, so that's what I ended up doing. However, I tried Matt's solution first, and it worked. So that's the answer when you want the code to execute in all cases *except* when a particular event fires. – Adi Inbar Jun 02 '15 at 21:31

1 Answers1

6

During Page_Load inspect Page.Request.Form["__EVENTTARGET"]. This will contain an identifier representing the control that caused the post back. From this you should be able to determine whether the post back was caused by the control you're interested in.

if (IsPostBack && Request.Form["__EVENTTARGET"] != "<control ID>") {
    ...
}

Is it possible to check in the Page_Load() method or any other code that executes before the SelectedIndexChanged event handler whether a DropDownList's selected index was changed?

Not without resorting to a custom technique. The SelectedIndexChanged event fires too late during the page event lifecycle to be useful to your scenario. One option is to store the DropDownList.SelectedIndex into the Page.ViewState collection during Page.OnPreRender and then comparing this value to the new DropDownList.SelectedIndex on the post back during Page_Load.

Matt Brooks
  • 1,584
  • 1
  • 14
  • 27
  • Thanks, that does the trick. Regarding my third bullet point, though, is there a way to check whether a control's selected index was changed outside the event handler? Could be useful in cases where the DropDownList doesn't autopostback. I suppose one could use a ViewState variable that stores the current selected index at the end of **Page_Load()** and then checks if it's different at the beginning, but that seems a bit kludgy, is there there a direct way to check if a particular event happened in sections of the code-behind other than the associated event handler? – Adi Inbar Jun 02 '15 at 21:38
  • I've updated my answer to address your final bullet point. Hope that helps. – Matt Brooks Jun 02 '15 at 22:26