DataBound dropdownlist + extra Item

Discussion in 'ASP .Net' started by =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Jul 25, 2005.

  1. Hi guys

    I've got a dropdownlist which is being databound to an sqldatareader at
    runtime. I want to add an extra Item to the list, not from the database.

    I know I can do this with

    ddl1.Items.Add(new ListItem("text", "value"));

    works fine. However, if I put this above my DataBinding code, it doesn't
    appear. After it it appears at the end of the list fine.

    Question is, how do I databind AND get this item to the top of my list? It's
    an ALL option y'see.

    Cheers for any help!


    Dan
     
    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Jul 25, 2005
    #1
    1. Advertising

  2. ddlLocation.DataSource = Line.GetBrandingLocations

    ddlLocation.DataValueField = "BrandingLocationID"

    ddlLocation.DataTextField = "BrandingLocation"

    ddlLocation.DataBind()

    'the following line inserts at position 0

    ddlLocation.Items.Insert(0, "(Select)")

    "dhnriverside" <> wrote in message
    news:...
    > Hi guys
    >
    > I've got a dropdownlist which is being databound to an sqldatareader at
    > runtime. I want to add an extra Item to the list, not from the database.
    >
    > I know I can do this with
    >
    > ddl1.Items.Add(new ListItem("text", "value"));
    >
    > works fine. However, if I put this above my DataBinding code, it doesn't
    > appear. After it it appears at the end of the list fine.
    >
    > Question is, how do I databind AND get this item to the top of my list?
    > It's
    > an ALL option y'see.
    >
    > Cheers for any help!
    >
    >
    > Dan
     
    Simon Hazelton, Jul 25, 2005
    #2
    1. Advertising

  3. Or

    ddlLocation.Items.Insert(0, New ListItem("(Select)", value)
     
    Simon Hazelton, Jul 25, 2005
    #3
  4. Thanks SImon, worked great.


    Dan

    "Simon Hazelton" wrote:

    > ddlLocation.DataSource = Line.GetBrandingLocations
    >
    > ddlLocation.DataValueField = "BrandingLocationID"
    >
    > ddlLocation.DataTextField = "BrandingLocation"
    >
    > ddlLocation.DataBind()
    >
    > 'the following line inserts at position 0
    >
    > ddlLocation.Items.Insert(0, "(Select)")
    >
    > "dhnriverside" <> wrote in message
    > news:...
    > > Hi guys
    > >
    > > I've got a dropdownlist which is being databound to an sqldatareader at
    > > runtime. I want to add an extra Item to the list, not from the database.
    > >
    > > I know I can do this with
    > >
    > > ddl1.Items.Add(new ListItem("text", "value"));
    > >
    > > works fine. However, if I put this above my DataBinding code, it doesn't
    > > appear. After it it appears at the end of the list fine.
    > >
    > > Question is, how do I databind AND get this item to the top of my list?
    > > It's
    > > an ALL option y'see.
    > >
    > > Cheers for any help!
    > >
    > >
    > > Dan

    >
    >
    >
     
    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Jul 25, 2005
    #4
  5. Dan,

    Do the databinding first and then call

    ddl1.Items.Insert(0, new ListItem("text", "value"));

    Eliyahu

    "dhnriverside" <> wrote in message
    news:...
    > Hi guys
    >
    > I've got a dropdownlist which is being databound to an sqldatareader at
    > runtime. I want to add an extra Item to the list, not from the database.
    >
    > I know I can do this with
    >
    > ddl1.Items.Add(new ListItem("text", "value"));
    >
    > works fine. However, if I put this above my DataBinding code, it doesn't
    > appear. After it it appears at the end of the list fine.
    >
    > Question is, how do I databind AND get this item to the top of my list?

    It's
    > an ALL option y'see.
    >
    > Cheers for any help!
    >
    >
    > Dan
     
    Eliyahu Goldin, Jul 25, 2005
    #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. rodchar
    Replies:
    0
    Views:
    399
    rodchar
    Nov 26, 2007
  2. mathieu
    Replies:
    3
    Views:
    608
    Bo Persson
    Sep 4, 2009
  3. Manu
    Replies:
    0
    Views:
    177
  4. Kyle K.

    Determining item count of databound DropDownList

    Kyle K., Jun 8, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    669
    Kyle K.
    Jun 8, 2006
  5. Lagrange
    Replies:
    1
    Views:
    572
    Lagrange
    Jan 17, 2009
Loading...

Share This Page