dropdown list control basic question

Discussion in 'ASP .Net' started by Aussie Rules, Jun 13, 2007.

  1. Aussie Rules

    Aussie Rules Guest

    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
     
    Aussie Rules, Jun 13, 2007
    #1
    1. Advertising

  2. Aussie Rules

    chanko Guest

    On Jun 13, 10:07 am, "Aussie Rules" <> 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
     
    chanko, Jun 13, 2007
    #2
    1. Advertising

  3. Aussie Rules

    Teemu Keiski Guest

    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

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

    "Aussie Rules" <> wrote in message
    news:...
    > 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
    >
    >
     
    Teemu Keiski, Jun 13, 2007
    #3
  4. Aussie Rules

    dfqupmbuab Guest

    Hello! Good Site! Thanks you! wyhcswlselwc
     
    dfqupmbuab, Jun 22, 2007
    #4
  5. Aussie Rules

    lphufieqln Guest

    Hello! Good Site! Thanks you! mqkqjouukffaj
     
    lphufieqln, Aug 16, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFubnkgQ2hvaGFu?=

    Question: Dropdown List Control

    =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Mar 18, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    465
    Karl Seguin
    Mar 19, 2005
  2. vishnu
    Replies:
    1
    Views:
    1,232
    Patrick.O.Ige
    Mar 25, 2006
  3. Mike
    Replies:
    1
    Views:
    825
  4. acadam
    Replies:
    0
    Views:
    768
    acadam
    Dec 27, 2006
  5. M. Craig
    Replies:
    1
    Views:
    760
    Victor Garcia Aprea [MVP]
    Jan 9, 2004
Loading...

Share This Page