How to set value and label to a DropDownList?

Discussion in 'ASP .Net Web Controls' started by Robson Carvalho Machado, Aug 12, 2004.

  1. Dear Friends,

    Using HTML we can create a DropDownList as follows

    <select name="xxx">
    <option value="1">Brazil
    <option value="2">USA
    <option value="3">Canada
    </select>

    How can I do this with WebControls by using CodeBehind?

    I was trying the below code but could not set both value and label of each
    item.

    myDropDownList = New DropDownList
    myDropDownList.ID = Id
    myDropDownList.Items.Add("Brasil")
    myDropDownList.Items.Add("USA")
    myDropDownList.Items.Add("Canada")
    myUnits = New System.Web.UI.WebControls.Unit(402)
    myDropDownList.Width = myUnits
    myDropDownList.Attributes("style") = Style
    PlaceHolder1.Controls.Add(myDropDownList)
    Robson Carvalho Machado, Aug 12, 2004
    #1
    1. Advertising

  2. Robson Carvalho Machado

    Jos Guest

    Robson Carvalho Machado wrote:
    > Dear Friends,
    >
    > Using HTML we can create a DropDownList as follows
    >
    > <select name="xxx">
    > <option value="1">Brazil
    > <option value="2">USA
    > <option value="3">Canada
    > </select>
    >
    > How can I do this with WebControls by using CodeBehind?
    >
    > I was trying the below code but could not set both value and label
    > of each item.
    >
    > myDropDownList = New DropDownList
    > myDropDownList.ID = Id
    > myDropDownList.Items.Add("Brasil")
    > myDropDownList.Items.Add("USA")
    > myDropDownList.Items.Add("Canada")
    > myUnits = New System.Web.UI.WebControls.Unit(402)
    > myDropDownList.Width = myUnits
    > myDropDownList.Attributes("style") = Style
    > PlaceHolder1.Controls.Add(myDropDownList)


    myDropDownList.Items.Add(New ListItem("Brasil","1"))
    myDropDownList.Items.Add(New ListItem("USA","2"))
    myDropDownList.Items.Add(New ListItem("Canada","3"))

    --

    Jos
    Jos, Aug 13, 2004
    #2
    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. Replies:
    1
    Views:
    653
  2. keithb
    Replies:
    1
    Views:
    11,916
    shauns1
    Nov 1, 2006
  3. CSharpner
    Replies:
    0
    Views:
    996
    CSharpner
    Apr 9, 2007
  4. S_K
    Replies:
    5
    Views:
    751
    Eliyahu Goldin
    Nov 14, 2007
  5. Knut Krueger
    Replies:
    8
    Views:
    251
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page