【问题标题】:Cancel a cross-page postback?取消跨页回发?
【发布时间】:2010-12-17 16:28:59
【问题描述】:

我有一个页面,其中包含多个ListBoxes,它们基于使用AutoPostBack 的选定值进行了一些级联过滤。该表单采用所有选定的值,并通过跨页发布到不同的 ASPX 来生成一个 excel 文档。问题是,点击一次提交后,每次选择更改时都会不断触发跨页回发。

<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" />

如何取消ListBoxes'SelectedIndexChanged事件的跨页回发?

这是代码隐藏中的事件:

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

【问题讨论】:

    标签: asp.net asp.net-ajax asp.net-2.0 cross-page-postback


    【解决方案1】:

    这是我目前使用 javascript 的解决方案。它可以工作,但看起来像个黑客:

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

    编辑:在代码隐藏中添加点击事件:

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

    【讨论】:

      【解决方案2】:

      问题在于,使用 PostbackUrl 属性会将表单操作重置为新 URL,并且您的 Ajax 调用(或任何后续回发)使用表单的当前操作。

      您的解决方案不起作用,因为提交按钮不是更新面板的一部分,因此它永远不会被修改。

      最简单的解决方案可能是将您的 Excel 文件生成代码移出它所在的页面,并移到您正在查看的页面中,在按钮的单击处理程序中。

      您也可以在您正在查看的页面上包含一个 iframe,并在提交时将 iframe 的源设置为 Excel 生成页面,而不是转到新页面。

      这两种方法都可以避免使用 PostbackUrl。

      【讨论】:

      • 嗯,我认为这根本与 ajax 无关,因为即使我完全删除了 UpdatePanel,我也遇到了同样的问题
      • 编辑:重新排列第一句并添加更多信息
      猜你喜欢
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多