how to get HtmlSelect's multiple item value?

  • Thread starter Thread starter David
  • Start date Start date
D

David

I want to get the Sel_142's multiple item's value,
and put them in an array.

if ( !Sel_142.Multiple )
vo.CA142 = Sel_142.Items[Sel_142.SelectedIndex].Value;
else
{
Sel_142.Items.....
}

thanks for your kind advice.

David.
 
the ListBox.SelectedItems Property contains aListBox.SelectedObjectCollection
containing the currently selected items in the control.

Peter

Remarks
 
pardon me, the "Sel_142" is a System.Web.UI.HtmlControls.HtmlSelect,
just add "run at server" tag...
so I can not find the ListBox.SelectedItems Property on it.

I modify my code like this:

if ( Sel_142.Items.Count > 1 )
{
string CA142 = "" ;
for ( int i=0; i<=Sel_142.Items.Count - 1; i++ )
{
if (Sel_142.Items.Selected)
{
if ( CA142 == "" )
CA142 = Sel_142.Items.Value ;
else
CA142s += Sel_142.Items.Value + "," ;
}
}
}

but when I excute Sel_142.Items.Selected,
it always get first selected item,
and others selected item always skip "if (Sel_142.Items.Selected)"
I don't know why I can not get all selected item's value...><

David.
 
Sorry,
didn't see the "MULTIPLE".

Try this:

string CA142 = "" ;
string CA142s=String.Empty ;
if ( Sel_142.Items.Count > 1 )
{
for ( int i=0; i<Sel_142.Items.Count; i++ )
{
if (Sel_142.Items.Selected)
{
if ( CA142 == "" )
CA142 = Sel_142.Items.Value ;
else
CA142 += Sel_142.Items.Value + "," ;
}
}
CA142=CA142.Substring(0,CA142.LastIndexOf(",") );
}

--Peter
 
sorry, I still got 1 selected item value.
I think the point is "if (Sel_142.Items.Selected)"
because I always only got 1 selected item value,
and others it will skip.

thx, David.
 
David,
I have no idea what you are doing to get these results, the code I just
provided returns a concatenated, comma-delimited list of all the selected
item VALUES
(e.g., <OPTION VALUE="1" SELECTED>One</OPTION> )
and it chops off the extra comma at the end. Take another look at the
example code I provided you.
Peter
 
Back
Top