dropdown list control basic question

A

Aussie Rules

Hi,

I have a drop down that I have populated. I am use to using winforms with
the items.add function to add objects to the drop downs, but on web forms it
seems you can only add text string.

How do i then force the drop down to be a specific item in the drop down
(such as setting a default value)


If its possible to add more than just a text string, is it possible to have
an index. For example I want to have a list of countriues in the drop down.
When the user selected a country, say 'France', I am able to access the
index of that item, which may be the 'FR'

Thanks all
 
C

chanko

Hi,

I have a drop down that I have populated. I am use to using winforms with
the items.add function to add objects to the drop downs, but on web forms it
seems you can only add text string.

How do i then force the drop down to be a specific item in the drop down
(such as setting a default value)

If its possible to add more than just a text string, is it possible to have
an index. For example I want to have a list of countriues in the drop down.
When the user selected a country, say 'France', I am able to access the
index of that item, which may be the 'FR'

Thanks all

Hello!

You can add ListItems to a drop down on webforms
these objects can be created with a display string, and a key string

so if you wanted to add countries, it could be something like:

ddlCountries.Items.Add( new ListItem("Frances", "FR") );

and when wanting to know the value of which item is selected in your
drop down you can just access the SelectedValue property of the
control

string selected = ddlCountries.SelectedValue;

also, in case you wanted to select a specific item via code behind,
you can with the SelectedIndex property

ddlCountries.SelectedIndex =
ddlCountries.Items.IndexOf( ddlCountries.Items.FindByValue( "FR" ) );


hope this is what you were looking for
good luck
 
T

Teemu Keiski

With web forms you can have a text and a value associated with a list item
in the DropDownList

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="France" Value="FR" />
<asp:ListItem Text="Finland" Value="FI" Selected="True" />
</asp:DropDownList>

Adding in code could be:

DropDownList1.Items.Add(New ListItem("France", "FR"))
DropDownList1.Items.Add(New ListItem("Finland", "FI"))

'Setting Finland to be a default selection
DropDownList1.Items.FindByValue("FI").Selected = True

'Or optionally to ensure only one item is always selected
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("FI"))

Then accessing what's selected can be done via SelectedIndex, SelectedItem
or SelectedValueproperties or via Items collection
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top