Carry Forward Multiple Selections In A ListBox

Results 1 to 2 of 2

Thread: Carry Forward Multiple Selections In A ListBox

  1. #1
    Join Date
    Dec 1969

    Default Carry Forward Multiple Selections In A ListBox

    I am populating a ListBox with different city names which are being retrieved from a SQL Server DB table. The AutoPostBack<BR>property is set to true for this ListBox which invokes a Sub named SelectEName in the OnSelectedIndexChanged event. This is what I have done:<BR><BR>Sub Page_Load(obj As Object,ea As EventArgs)<BR> If Not(Page.IsPostBack) Then<BR> &#039;populate the ListBox & then bind it<BR> ....................................<BR> lname.DataBind()<BR> End If<BR>End Sub<BR>Sub SelectEName(obj As Object,ea As EventArgs)<BR> Dim strLName<BR> strLName=lname.SelectedItem.Value<BR> Response.Write(strLName)<BR>End Sub<BR>&#060;form runat="server"&#062;<BR>&#060;asp:ListBox id="lname" AutoPostBack="true" DataTextField="LName" OnSelectedIndexChanged="SelectEName" multiple runat="server"/&#062;<BR>........................<BR>............ ............<BR>&#060;/form&#062;<BR><BR>Please note that I want to give my users the option of selecting more than one city name from the ListBox......that&#039;s the reason<BR>why I have included multiple within the ListBox. Now when a user selects a city name, then Response.Write(strLName) prints the<BR>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<BR>of all, since the OnSelectedIndexChanged event is being used, the user is not allowed to select more than one city name i.e. as<BR>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?<BR><BR>Thanks,<BR><BR>Arpan

  2. #2
    Join Date
    Dec 1969

    Default RE: Carry Forward Multiple Selections In A ListBox

    You want to lose the OnSelectedIndexChanged. You can&#039;t be in two places at once. Either you want the person to be able hold down their [Ctrl} key and click multiple items OR you want to do something with the selected index.<BR><BR>Second, to get all of the values you need iterate throught the members. Right now all you are doing is writing out strLName. You&#039;re partway there but first go through ALL of the list&#039;s members...<BR><BR>Dim itmCity As ListItem<BR>&#039;Dim strCityList As String<BR>For Each itmCity in lblCity.Items<BR>If itmCity.Selected Then<BR>strCityList = strCityList & itmCity.Value & ","<BR>End If<BR>Next<BR><BR>cheers,<BR><BR>Cameron

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts