Prevent ControlParameter AutoPostBack from firing ObjectDataSource Select Method

389 Views Asked by At

i've created a simple ASP Gridview with an ObjectDataSource to get the data from my database and show it in the GridView. The ObjectDataSource looks like this:

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

The ControlParameter is a DropDownList that is used to filter my GridView. It's placed inside a <asp:Panel> and looks like this:

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

My problem is, that whenever i select something from the DropDownList it triggers the SelectMethod. I tried turning off the AutoPostBack on my DropDownList but the PostBack is important for other functions so i cant leave it on AutoPostBack="false" it has to be on True all the time.

My question is: How can i prevent this from happening. I want to keep the AutoPostBack on the DropDownList. But my SelectMethod should not trigger at the same time. I wanna be able to control when i filter my data with a search-button.

2

There are 2 best solutions below

0
J.C On

You can use a updatepanel to prevent autopostback on selectIndex I had the sam problem has you recently I wanted to create a cascading dropdown and I didnt want the page to refreash on selected index changed. If you wanted to known more this tutoriel has the solution too your problem https://www.aspsnippets.com/Articles/Cascading-DropDownList-for-CountryStateCity-in-ASPNet.aspx

Otherwise your code should look something like this.

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

Hope this helps. :)

0
AhmadMM On

First Method: I've used this method it's effective, add this to your dropdown onchange="javascript:setTimeout('__doPostBack(\'DropDownListXY\',\'\')', 0)", and make sure the AutoPostback is set to 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>

Second Method is to put the Dropdownlist inside an UpdatePanel and handle it's postback inside the Trigger