Reputation: 485
I'm trying to put a searchbox, a dropdownlist and lostbox control on my asp master page. Once a value is selected from the dropdownlist, the value passed to listbox;once some word is typed into the searchbox and enter is hit, the closest value is passed to the listbox.
My asp code for the control is below:
searchbox:
<asp:TextBox ID="TextBox1" runat="server" ToolTip="Enter Company" Width="120px">
dropdownlist:
<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>
listbox:
<asp:ListBox ID="ListBox1" runat="server" DataTextField="company" DataValueField="id" Rows="1" Width="160px">
<asp:ListItem Text="--Null--" Value="" selected="true" />
</asp:ListBox>
data source from the searchbox is:
<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>
datasource from dropdownlist is
<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>
and my vb code is below:
--Store session variable
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
--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 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
-----on change
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
What I want to achieve is after I make a selection (say company A), when I refresh page, company A is still selected. Now it works well if company A is got through search box. But if I select company A from dropdownlist, then store the session variable, then refresh the page, it comes out company A is still selected in dropdownlist, but "--Null--" appears in listbox. I wonder what's the problem?
Thanks for any advice!
Upvotes: 1
Views: 104
Reputation: 485
I finally realized that my page_load has a problem. The following is the correct page_load code
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
Upvotes: 1