List Control, multiple choise from the code

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
 
K

Kannan Meiappan

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
 
N

N. Demos

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top