刷新页面时,级联下拉列表保持选定值不变

时间:2014-04-16 13:47:37

标签: asp.net vb.net visual-studio-2012

我有三个下拉列表,如下所示

下拉A

<asp:DropDownList ID="DropDownListA" class="ddStyle" runat="server" DataSourceID="SqlDataSource2" DataTextField="country" DataValueField="country" AppendDataBoundItems="true" AutoPostBack="True" EnableViewState="true" ViewStateMode="Enabled">
                   <asp:ListItem Text="--Select One--" Value="" Selected="True" />  </asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select distinct country from table1 order by country"></asp:SqlDataSource>

DropDownListB

  <asp:DropDownList ID="DropDownListB" class="ddStyle" runat="server" DataSourceID="SqlDataSource1" DataTextField="GICS" DataValueField="GICS" AutoPostBack="True"></asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select distinct GICS, country from table1 where country=@country">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="DropDownListA" Name="country" PropertyName="SelectedValue" />
                        </SelectParameters>
                    </asp:SqlDataSource>

DropDownListC

    <asp:DropDownList ID="DropDownListC" class="ddStyle" runat="server" DataSourceID="SqlDataSource3" DataTextField="company" DataValueField="id" AutoPostBack="True"></asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select company, GICS, country, id from table1 where gics=@gics and country=@country">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="DropDownListB" Name="gics" PropertyName="SelectedValue" />
                                 <asp:ControlParameter ControlID="DropDownListA" Name="Country" PropertyName="SelectedValue" />
                            </SelectParameters>
                        </asp:SqlDataSource>

我想要实现的是每次刷新页面时,每个下拉列表的选定值都不会改变(不要返回第一个),我尝试过以下vb代码:

      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim SelectionA As String = Nothing
    Dim SelectionB As String = Nothing
    Dim SelectionC As String = Nothing



    If Not DropDownListA.Text Is Nothing Then SelectionA = DropDownListA.Text
    Session("SelectedA") = SelectionA
    If Not DropDownListB.Text Is Nothing Then SelectionB = DropDownListB.Text
    Session("SelectedB") = SelectionB
    If Not DropDownListC.SelectedValue Is Nothing Then SelectionC = DropDownListC.SelectedValue
    Session("SelectedC") = Selection


End Sub

我的网页加载

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        If Not Session("SelectedA") Is Nothing Then DropDownListA.SelectedValue = Session("SelectedA").ToString
        If Not Session("SelecteB") Is Nothing Then DropDownListB.SelectedValue = Session("SelecteB").ToString
        If Not Session("SelectedC") Is Nothing Then DropDownListC.SelectedValue = Session("SelectedC").ToString


        If Not Session("SelectedE") Is Nothing Then DropDownListE.SelectedValue = Session("SelectedE").ToString

    End If
End Sub

但它不起作用。 我想知道解决方案是什么?我看了http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx 但我认为这不能解决我的问题吗?

感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

为每个下拉列表添加autopostback = true