Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   dropdown list control basic question (http://www.velocityreviews.com/forums/t514342-dropdown-list-control-basic-question.html)

Aussie Rules 06-13-2007 05:07 PM

dropdown list control basic question
 
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



chanko 06-13-2007 06:04 PM

Re: dropdown list control basic question
 
On Jun 13, 10:07 am, "Aussie Rules" <AussieRu...@nospam.nospam> wrote:
> 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


Teemu Keiski 06-13-2007 06:08 PM

Re: dropdown list control basic question
 
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.Fi ndByValue("FI"))

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

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

"Aussie Rules" <AussieRules@nospam.nospam> wrote in message
news:uxUbC2drHHA.484@TK2MSFTNGP06.phx.gbl...
> 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
>
>




dfqupmbuab 06-21-2007 11:08 PM

Re: dropdown list control basic question
 
Hello! Good Site! Thanks you! wyhcswlselwc

lphufieqln 08-16-2007 06:14 PM

Re: dropdown list control basic question
 
Hello! Good Site! Thanks you! mqkqjouukffaj


All times are GMT. The time now is 08:11 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.