【问题标题】:ASP.NET Gridview refresh with cascading dropdown controls使用级联下拉控件刷新 ASP.NET Gridview
【发布时间】:2011-11-08 20:03:47
【问题描述】:

我在 asp.net 页面中有两个下拉列表和一个 gridview(lstDivisions、lstDistricts、gridIncidents),我在 ASPX 页面中使用 SqlDataSource ID 语句从 SQL Server 中检索所有三个控件的数据。

所有控件都正确加载,当我在第二个下拉列表中选择不同的值时,gridview 会按预期填充。

当我在第一个下拉列表中选择一个新的 Division 值时,第二个下拉列表会正确填充,但是 gridview 不会刷新,直到我从第二个下拉列表中选择一个新的 District 值。

我用什么方法来刷新gridview?

谢谢

来自 aspx 页面的代码:

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

【问题讨论】:

  • 解决方案:在“lstDivisions”DropDownList 的 OnSelectedIndexChanged ... 添加:lstDistricts.DataBind();

标签: asp.net gridview drop-down-menu


【解决方案1】:

感谢您提供答案。这已经让我发疯了 2 天。和你一样,我有多个链接的下拉列表(我有三个),并且一直试图通过挂钩数据源的各种 Bind/Load/Unload 事件来解决这个问题,但没有成功。

为了彻底解决我的问题,我还必须禁用所有 3 个下拉菜单的视图状态。

【讨论】:

    【解决方案2】:

    我相信在第一个下拉选择的索引更改事件中,您需要重新绑定 gridview。 GridView.DataBind(); .无论如何,您可以发布代码,它会更有帮助。

    希望对你有帮助

    【讨论】:

    • 添加 gridIncidents.DataBind();到 GetDivisions_SelectedIndexChanged 并没有导致 Grid 刷新。
    【解决方案3】:

    每次发布​​页面时,gridview 都必须刷新。这意味着如果您在页面上放置了一个测试按钮,并在该按钮上添加了一个点击事件,而没有在其中添加任何代码,则 gridview 必须引用,因为发生了回发。

    您是否检查了第一个下拉框并检查了它的回发属性。它必须更改为true。像这样

    AutoPostBack= true // ?
    

    【讨论】:

      【解决方案4】:

      解决方案

      在 OnSelectedIndexChanged 中为 lstDivisionsDropDownList 添加:lstDistricts.DataBind();

      【讨论】:

        猜你喜欢
        • 2014-01-12
        • 1970-01-01
        • 1970-01-01
        • 2021-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-23
        相关资源
        最近更新 更多