ASP.NET Gridview refresh with cascading dropdown c

2019-08-11 16:20发布

问题:

I have two dropdowns and a gridview (lstDivisions, lstDistricts, gridIncidents) in an asp.net page and I use SqlDataSource ID statements in the ASPX page to retrieve the data from SQL Server for all three controls.

All controls load properly and when I select a different value in the second dropdown the gridview populates as expected.

When I select a new Division value in the first dropdown the second dropdown populates properly BUT the gridview does not refresh UNTIL I select a new District value from the second dropdown.

What method do I use to Refresh the gridview?

Thanks

Code from aspx page:

<asp:SqlDataSource ID="sourceIncidents" runat="server" ProviderName="System.Data.SqlClient"
     ConnectionString="<%$ ConnectionStrings:DB1Connection %>" 
     SelectCommand="SELECT incidentnumber, call_date, call_time, address , call_type, district FROM Incident ORDER BY create_date"
     FilterExpression="District='{0}'" EnableCaching="True">
     <FilterParameters>
       <asp:ControlParameter ControlID="lstDistricts" Name="district" PropertyName="SelectedValue" />
     </FilterParameters>        
    </asp:SqlDataSource>

    <asp:SqlDataSource ID="sourceIncidentDistricts" runat="server"
     ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:DB2Connection %>"
     SelectCommand="SELECT DivisionName, DistrictName as District FROM Divisions LEFT JOIN Districts ON Districts.DivisionID = Divisions.DivisionID" 
     FilterExpression="DivisionName ='{0}'" EnableCaching="True">
     <FilterParameters>
       <asp:ControlParameter ControlID="lstDivisions" Name="DivisionName" PropertyName="SelectedValue" />
     </FilterParameters> 
    </asp:SqlDataSource>

    <asp:SqlDataSource ID="sourceIncidentDivisions" runat="server"
     ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:DB2Connection %>"
     SelectCommand="SELECT DISTINCT DivisionName FROM Divisions" EnableCaching="True">
    </asp:SqlDataSource>
    <br />

    <asp:Label ID="lblDivision" runat="server" Text="Division"></asp:Label>
    <asp:DropDownList ID="lstDivisions" runat="server" DataSourceID="sourceIncidentDivisions"
        DataValueField="DivisionName" OnSelectedIndexChanged="GetDivisions_SelectedIndexChanged"  
        DataTextField="DivisionName" Width="205px" AutoPostBack="True">
    </asp:DropDownList>

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Label ID="lblDistrict" runat="server" Text="District"></asp:Label>
    <asp:DropDownList ID="lstDistricts" runat="server" DataSourceID="sourceIncidentDistricts"
       DataValueField="District" OnSelectedIndexChanged="GetDistricts_SelectedIndexChanged"
       DataTextField="District" Width="205px" AutoPostBack="True">
    </asp:DropDownList><br />
    <br />
    <asp:Label ID="lblIncidentsCaption" runat="server" Font-Bold="True" Font-Names="Verdana"
        Font-Size="Small"></asp:Label>
    <br />

    <asp:GridView ID="gridIncidents" runat="server" CellPadding="3" DataSourceID="sourceIncidents" 
        Font-Names="Verdana" Font-Size="Small" 
        OnSelectedIndexChanged="gridIncidents_SelectedIndexChanged"
        GridLines="Horizontal" AutoGenerateColumns="False" DataKeyNames="incidentnumber"
        AllowPaging="True" AllowSorting="True" BackColor="White" BorderColor="#4A3C8C" 
        BorderStyle="None" BorderWidth="1px"
        PagerSettings-Mode="NumericFirstLast" PagerSettings-Position="TopAndBottom" >
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <HeaderStyle BackColor="#4B6C9E" Font-Bold="True" ForeColor="#F7F7F7" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
        <Columns>
            <asp:CommandField SelectText=">" ShowSelectButton="True"  />

            <asp:TemplateField HeaderText="INCIDENT" SortExpression="incidentnumber">
                <ItemTemplate><%# Eval("incidentnumber").ToString() %></ItemTemplate>
                <ItemStyle BorderWidth="1px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="DATE" SortExpression="call_date">
                <Itemtemplate><%# Eval("call_date", "{0:MM/dd/yyyy}")%></Itemtemplate>
                <ItemStyle BorderWidth="1px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="TIME" SortExpression="call_time">
                <Itemtemplate><%# Eval("call_time") %></Itemtemplate>
                <ItemStyle BorderWidth="1px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ADDRESS" SortExpression="address">
                <Itemtemplate><%# Eval("address") %></Itemtemplate>
                <ItemStyle Width="300px" BorderWidth="1px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CALL TYPE" SortExpression="call_type">
                <Itemtemplate><%# Eval("call_type") %></Itemtemplate>
                <ItemStyle BorderWidth="1px" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="DISTRICT" SortExpression="district">
                <Itemtemplate><%# Eval("district")%></Itemtemplate>
                <ItemStyle BorderWidth="1px" />
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

回答1:

Thank you for posing the answer. This has been driving me crazy for 2 days. Like you, I have multiple chained dropdown lists (I have three) and have been trying to fix that problem by hooking into the datasource's various Bind/Load/Unload events with no success.

To completely solve my problem I had to disable viewstate on all 3 of my dropdowns as well.



回答2:

I believe in the first drop down selected index changed event you need to rebind the gridview. GridView.DataBind(); . Anyway could you post the code, it will be more helpful.

Hope this helps



回答3:

The gridview must refresh every time you post the page. That means if you put a test button on a page and add a click event to that button without putting any code in it, the gridview must referesh because a postback has occurred.

Have you checked the first dropdown box and check it postback property. It must be change to true. Like this

AutoPostBack= true // ?


回答4:

Solution

In the OnSelectedIndexChanged for the lstDivisions DropDownList added: lstDistricts.DataBind();