Search code examples
asp.netasp.net-ajaxasp.net-2.0cross-page-postback

Cancel a cross-page postback?


I have a page that has several ListBoxes that have some cascading filtering based on the selected values using an AutoPostBack. The form takes all the selected values and generates an excel doc by cross-page posting to a different ASPX. The problem is, after clicking submit once, it will continually fire the cross-page postback every time a selection has changed.

<asp:ScriptManager runat="server" />
<asp:UpdatePanel UpdateMode="Conditional" runat="server">
    <ContentTemplate>
  <asp:ListBox ID="ParentItems" runat="server" SelectionMode="Multiple" AutoPostBack="true"></asp:ListBox>    
  <asp:ListBox ID="ChildItems" runat="server" SelectionMode="Multiple" AutoPostBack="true"></asp:ListBox>  
 </ContentTemplate>
</asp:UpdatePanel>

<asp:Button ID="Submit" runat="server" PostBackUrl="~/AnotherPageThatGeneratesAnExcelDoc.aspx" />

How do I cancel the cross-page postback from the ListBoxes' SelectedIndexChanged events?

Here's the event in the codebehind:

Protected Sub ParentItems_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ParentItems.SelectedIndexChanged
 '' do some filtering of the ChildItems ListBox

 '' tried these but they do not work
 ''Submit.Enabled = False

 ''Submit.PostBackUrl = String.Empty

 '' I also tried wrapping the button in a PlaceHolder and hiding/removing it, neither worked
 ''Buttons.Visible = False
 ''Buttons.Controls.Remove(Submit)
End Sub

Solution

  • This is my current solution using javascript. It works, but seems like a hack:

    // using jQuery, add a click event that resets the form action
    $("select[multiple]").click(function () {
        this.form.action = this.form._initialAction;
    });
    

    Edit: adding a click event in the codebehind:

    ParentItems.Attributes("onclick") = "this.form.action = this.form._initialAction;"