List Control, multiple choise from the code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello!

Please, could anyone tell, is it possible to set multiple items to be
selected in list control in the code? For example when the web form is loaded
three items of 5 are selected in list control already? Now I manage to set
only one item to be selected during page load, but there is need to multiple
items could be selected for the user. I appreciate very much your help!

Thanks.

Pavel
 
Hi

jus try this,

lstdropdown.SelectedIndex = -1;
for (int i=0; i<Datatable.Rows.Count;i++)

lstdropdown.Items.FindByValue(Datatable.Rows["ColumnName"].ToString()).Se
lected=true;

Regards,
Kannan
 
Peter said:
Hello!

Please, could anyone tell, is it possible to set multiple items to be
selected in list control in the code? For example when the web form is loaded
three items of 5 are selected in list control already? Now I manage to set
only one item to be selected during page load, but there is need to multiple
items could be selected for the user. I appreciate very much your help!

Thanks.

Pavel
Yes, try this statement before you start selecting the items

'Set Selection mode as Multiple
[YourListBoxID].SelectionMode = ListSelectionMode.Multiple

Full Page Example:
------------------
<%@ PAGE language="VB" runat="server" %>

<SCRIPT language="vb" runat="server">

Sub Page_Load(Source as Object, E As EventArgs)

'Add Items To List
lbxList.Items.Add("Item 1")
lbxList.Items.Add("Item 2")
lbxList.Items.Add("Item 3")
lbxList.Items.Add("Item 4")
lbxList.Items.Add("Item 5")
lbxList.Items.Add("Item 6")
lbxList.Items.Add("Item 7")
lbxList.Items.Add("Item 8")
lbxList.Items.Add("Item 9")
lbxList.Items.Add("Item 10")

lbxList.Rows = 10
'Set Selection mode as Multiple
lbxList.SelectionMode = ListSelectionMode.Multiple

Dim litmItem as ListItem

' Select every third list item
Dim i As Integer = 0

For Each litmItem In lbxList.Items
If (i + 1) MOD 3 = 0 Then
litmItem.Selected = True
End If
i += 1
Next

End Sub
</SCRIPT>

<HTML>
<HEAD>
<TITLE>Multi-Selected ListBox</TITLE>
</HEAD>
<BODY>
<FORM id="theForm" runat="server">
<ASP:listbox id="lbxList" runat="server"/>

</FORM>
</BODY>
</HTML>
 
Back
Top