简体   繁体   中英

Determine whether the selected index was changed or an autopostback event was triggered, in sections of the code-behind other than the event handler

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:

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) { 
            [...]
        }
        [...]

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 (ie 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM