c# - 防止 ControlParameter AutoPostBack 触发 ObjectDataSource Select 方法

标签 c# asp.net gridview postback objectdatasource

我创建了一个简单的 ASP Gridview,其中包含 ObjectDataSource从我的数据库获取数据并将其显示在 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>

ControlParameterDropDownList用于过滤我的 GridView 。它被放置在 <asp:Panel> 内看起来像这样:

<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 。我尝试关闭 AutoPostBack在我的DropDownList上但回发对于其他功能很重要,所以我不能将其保留在AutoPostBack="false"上它必须位于 True每时每刻。

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

最佳答案

您可以使用 updatepanel 来防止 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>

希望这有帮助。 :)

关于c# - 防止 ControlParameter AutoPostBack 触发 ObjectDataSource Select 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55244419/

相关文章:

C# 多重约束

c# - 什么时候类型不是类型?错误 : 'is a type, which is not valid in the given context'

asp.net - ASPX VS CSHTML 做网格处理

c# - 如何在 asp.net mvc 的 url 中附加字符串?

asp.net - 将多项目 MVC4 解决方案部署到 Azure 失败

java - 清除 GridView 中的所有选定项目

PowerShell Out-GridView 处理 Click/DoubleClik 事件

c# - 如何清除 TextBox 以便默认的 Undo 方法仍然有效?

c# - 允许编辑一列但不允许编辑另一列

c# - 企业 N 层设置