【问题标题】:Prevent ControlParameter AutoPostBack from firing ObjectDataSource Select Method防止 ControlParameter AutoPostBack 触发 ObjectDataSource Select 方法
【发布时间】:2019-08-10 04:22:25
【问题描述】:

我创建了一个简单的带有ObjectDataSource 的ASP Gridview 来从我的数据库中获取数据并将其显示在GridView 中。 ObjectDataSource 看起来像这样:

<asp:ObjectDataSource 
    ID="ObjectDataSourceTest" 
    runat="server"
    SelectMethod="GetTestData" 
    TypeName="DataManager" 
    <SelectParameters>
        <asp:Parameter Name="sortExpression" Type="String" />
        <asp:ControlParameter ControlID="DropDownListXY" Name="xyFilter" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

ControlParameter 是一个DropDownList,用于过滤我的GridView。它位于&lt;asp:Panel&gt; 中,如下所示:

<div class="grid-100">
    <asp:DropDownList ID="DropDownListXY" OnSelectedIndexChanged="DropDownListXY_SelectedIndexChanged" DataSourceID="ObjectDataSourceApplikationTyp" runat="server" DataValueField="test_guid" DataTextField="test" AppendDataBoundItems="true" AutoPostBack="true">
    <asp:ListItem Text="-- all --" Value=""></asp:ListItem>
    </asp:DropDownList>
</div>

我的问题是,每当我从DropDownList 中选择某些东西时,它都会触发SelectMethod。我尝试在DropDownList 上关闭AutoPostBack,但回发对于其他功能很重要,所以我不能将它留在AutoPostBack="false" 上,它必须一直在True 上。

我的问题是:我怎样才能防止这种情况发生。我想将AutoPostBack 保留在DropDownList 上。但是我的 SelectMethod 不应该同时触发。我希望能够控制何时使用搜索按钮过滤我的数据。

【问题讨论】:

  • 换个策略怎么样。在您的 SelectMethod GetTestData 中,您能否检测到它由于 DropDownList 更改而正在运行,然后退出?

标签: c# asp.net gridview postback objectdatasource


【解决方案1】:

您可以使用更新面板来防止 selectIndex 上的自动回发 我遇到了同样的问题,您最近是否想创建级联下拉列表,并且我不希望页面在所选索引更改时重新刷新。如果您想了解更多,这个教程也有解决您的问题https://www.aspsnippets.com/Articles/Cascading-DropDownList-for-CountryStateCity-in-ASPNet.aspx

否则你的代码应该看起来像这样。

    <div class="grid-100">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
        <ContentTemplate>
        <asp:DropDownList ID="DropDownListXY" OnSelectedIndexChanged="DropDownListXY_SelectedIndexChanged" DataSourceID="ObjectDataSourceApplikationTyp" runat="server" DataValueField="test_guid" DataTextField="test" AppendDataBoundItems="true" AutoPostBack="true">
        <asp:ListItem Text="-- all --" Value=""></asp:ListItem>
        </asp:DropDownList>
        </ContentTemplate>
         <Triggers>
             <asp:AsyncPostbackTrigger ControlID="DropDownListXY" EventName="SelectedIndexChanged" />
              <asp:PostBackTrigger ControlID="btnConfirmPurchases" />
          </Triggers>
         </asp:UpdatePanel>

    </div>

希望这会有所帮助。 :)

【讨论】:

    【解决方案2】:

    第一种方法:我用过这个方法很有效,把这个加到你的下拉列表中 onchange="javascript:setTimeout('__doPostBack(\'DropDownListXY\',\'\')', 0)",并确保 AutoPostback 设置为 true

    <div class="grid-100">
        <asp:DropDownList ID="DropDownListXY" OnSelectedIndexChanged="DropDownListXY_SelectedIndexChanged" DataSourceID="ObjectDataSourceApplikationTyp" runat="server" DataValueField="test_guid" DataTextField="test" AppendDataBoundItems="true" onchange="javascript:setTimeout('__doPostBack(\'DropDownListXY\',\'\')', 0)"  AutoPostBack="true">
        <asp:ListItem Text="-- all --" Value=""></asp:ListItem>
        </asp:DropDownList>
    </div>
    

    第二种方法就是把Dropdownlist放到一个UpdatePanel里面,然后在Trigger里面处理它的回发

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多