我用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
。它被放置在<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
但是PostBack对于其他功能非常重要,所以我不能把它放在AutoPostBack="false"
上,它必须一直在True
上。
我的问题是:我怎样才能防止这种情况发生。我想把AutoPostBack
放在DropDownList
上。但是我的SelectMethod不应该同时触发。我希望能够控制何时使用搜索按钮过滤数据。
您可以使用更新面板来防止selectIndex上的autopostback我有sam问题你最近我想创建一个级联下拉列表我不希望页面重新更新选定的索引已更改。如果你想知道更多这个tutoriel也有解决方案你的问题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>
希望这可以帮助。 :)
第一种方法:我使用这种方法有效,将其添加到你的下拉列表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中处理它的回发