I am populating a ListBox with different city names which are being retrieved from a SQL Server DB table. The AutoPostBack property is set to true for this ListBox which invokes a Sub named SelectEName in the OnSelectedIndexChanged event. This is what I have done:
Sub Page_Load(obj As Object,ea As EventArgs)
If Not(Page.IsPostBack) Then
'populate the ListBox & then bind it
....................................
lname.DataBind()
End If
End Sub
Sub SelectEName(obj As Object,ea As EventArgs)
Dim strLName
strLName=lname.SelectedItem.Value
Response.Write(strLName)
End Sub
<form runat="server">
<asp:ListBox id="lname" AutoPostBack="true" DataTextField="LName" OnSelectedIndexChanged="SelectEName" multiple runat="server"/>
........................
........................
</form>
Please note that I want to give my users the option of selecting more than one city name from the ListBox. That's the reason why I have included multiple within the ListBox. Now when a user selects a city name, then Response.Write(strLName) prints the city name selected by the user on the web page, as expected. The problem comes when a user selects more than one city name. First of all, since the OnSelectedIndexChanged event is being used, the user is not allowed to select more than one city name i.e. as soon as he selects one city name, the Form gets POSTed. Secondly, even if the user manages to select more than one city name by clicking the mouse on the 1st city name & without releasing the mouse click selecting another 2-3 city names, then also Response.Write(strLName) prints the 1st city name only that he had selected. For e.g. assume that the cities given in the ListItem are City1, City2, City3, City4 & City5. The user clicks his mouse on City1 & without releasing the mouse click, moves his mouse down & selects, say, City2 & City3. Under such circumstances, though the user has selected City1, City2 & City3 from the ListItem,
still Response.Write(strLName) prints City1 only on the web page. How do I overcome the above 2 problems?
Thanks,
Arpan
Sub Page_Load(obj As Object,ea As EventArgs)
If Not(Page.IsPostBack) Then
'populate the ListBox & then bind it
....................................
lname.DataBind()
End If
End Sub
Sub SelectEName(obj As Object,ea As EventArgs)
Dim strLName
strLName=lname.SelectedItem.Value
Response.Write(strLName)
End Sub
<form runat="server">
<asp:ListBox id="lname" AutoPostBack="true" DataTextField="LName" OnSelectedIndexChanged="SelectEName" multiple runat="server"/>
........................
........................
</form>
Please note that I want to give my users the option of selecting more than one city name from the ListBox. That's the reason why I have included multiple within the ListBox. Now when a user selects a city name, then Response.Write(strLName) prints the city name selected by the user on the web page, as expected. The problem comes when a user selects more than one city name. First of all, since the OnSelectedIndexChanged event is being used, the user is not allowed to select more than one city name i.e. as soon as he selects one city name, the Form gets POSTed. Secondly, even if the user manages to select more than one city name by clicking the mouse on the 1st city name & without releasing the mouse click selecting another 2-3 city names, then also Response.Write(strLName) prints the 1st city name only that he had selected. For e.g. assume that the cities given in the ListItem are City1, City2, City3, City4 & City5. The user clicks his mouse on City1 & without releasing the mouse click, moves his mouse down & selects, say, City2 & City3. Under such circumstances, though the user has selected City1, City2 & City3 from the ListItem,
still Response.Write(strLName) prints City1 only on the web page. How do I overcome the above 2 problems?
Thanks,
Arpan