重新加载页面时,列表框无法获取值

时间:2014-04-23 14:06:26

标签: asp.net vb.net listbox

我试图在我的asp母版页上放置一个搜索框,一个下拉列表和lostbox控件。从下拉列表中选择一个值后,该值将传递给列表框;一旦将某个单词输入搜索框并输入命中,则将最接近的值传递给列表框。

我的控件的asp代码如下:

搜索框:

   <asp:TextBox ID="TextBox1" runat="server" ToolTip="Enter Company" Width="120px">

下拉列表:

    <asp:DropDownList ID="DropDownListCI"  runat="server" DataSourceID="SqlDataSource3" DataTextField="company" DataValueField="id"  AppendDataBoundItems="true" AutoPostBack="True" width="160px"> 
                                <asp:ListItem Text="--Select One--" Value="" Selected="True" /> </asp:DropDownList>

     <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select company, id from table1 order by company">

                            </asp:SqlDataSource>

列表框:

    <asp:ListBox ID="ListBox1" runat="server" DataTextField="company" DataValueField="id" Rows="1" Width="160px">
                              <asp:ListItem Text="--Null--" Value="" selected="true" />
                         </asp:ListBox>
来自搜索框的

数据源是:

    <asp:SqlDataSource ID="SqlDataSourceSE" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select company, id from table1 where company like '%'+ @Company + '%'">
                                <SelectParameters>
                                    <asp:ControlParameter ControlID="TextBox1" Name="Company" PropertyName="Text" />
                                </SelectParameters>
                            </asp:SqlDataSource>

来自下拉列表的数据源是

      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select company, id from table1 where id=@id">
                             <SelectParameters>
                                 <asp:ControlParameter ControlID="DropDownListCI" Name="ID" PropertyName="selectedvalue" />
                             </SelectParameters>
                         </asp:SqlDataSource>

我的vb代码如下:

- 存储会话变量

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


    Dim SelectionCI As String = Nothing
    Dim SelectionSE As String = Nothing


    If Not DropDownListCI.Text Is Nothing Then SelectionCI = DropDownListCI.Text
    Session("SelectedCI") = SelectionCI
    If Not TextBox1.Text Is Nothing Then SelectionSE = TextBox1.Text
    Session("SelectedSE") = SelectionSE

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("SelectedCI") Is Nothing Then DropDownListCI.Text = Session("SelectedCI").ToString
        If Not Session("SelectedSE") Is Nothing Then TextBox1.Text = Session("SelectedSE").ToString

    End If

    If Not DropDownListCI.Text = "" Then ListBox1.DataSourceID = SqlDataSource1.ID
    If Not TextBox1.Text = "" Then ListBox1.DataSourceID = SqlDataSourceSE.ID
End Sub

-----关于改变

 Protected Sub DropDownListCI_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownListCI.SelectedIndexChanged
    If Not DropDownListCI.SelectedIndex = 0 Then
        TextBox1.Text = ""
        ListBox1.DataSourceID = SqlDataSource1.ID
    Else : ListBox1.SelectedIndex = 0
    End If
End Sub


Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    If Not TextBox1.Text = "" Then
        DropDownListCI.SelectedIndex = 0
        ListBox1.DataSourceID = SqlDataSourceSE.ID
    End If

End Sub

我想要实现的是在我做出选择之后(比如公司A),当我刷新页面时,公司A仍然被选中。如果公司A通过搜索框,它现在运作良好。但是如果我从下拉列表中选择公司A,然后存储会话变量,然后刷新页面,那么公司A仍然会在下拉列表中被选中,但是&#34; - Null - &#34;出现在列表框中。我想知道问题是什么?

感谢您的任何建议!

1 个答案:

答案 0 :(得分:1)

我终于意识到我的page_load存在问题。以下是正确的page_load代码

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        If Not Session("SelectedCI") Is Nothing And Session("SelectedCI") <> "" Then
            DropDownListCI.Text = Session("SelectedCI").ToString
            ListBox1.DataSourceID = SqlDataSource1.ID
        ElseIf Not Session("SelectedSE") Is Nothing And Session("SelectedSE") <> "" Then
            TextBox1.Text = Session("SelectedSE").ToString
            ListBox1.DataSourceID = SqlDataSourceSE.ID

        End If

    End Sub