Default Value in on Databound Drop Down list

Discussion in 'ASP .Net Web Controls' started by Randy Galliano, Nov 5, 2007.

  1. Hello,

    I am populating a drop down list control with a table from a database.
    I would like the control to display a value such as --- select --- when
    it first comes up, instead of any values from the table. Is there a way
    to do this without putting the value --- Select --- in the actual table?

    Regards,

    Randy.
     
    Randy Galliano, Nov 5, 2007
    #1
    1. Advertising

  2. Yes, after calling the Databind() method, use the use the Items.Insert()
    method. Here is an example:

    MyDropDown.DataBind()
    MyDropDown.Items.Insert(0,"--- Select ---")

    This inserts an extra ListItem before index 0, therefore changing what was
    previously index 0 into index 1, index 1 into index 2, etc. Since this is
    only 1 extra line of code, it is very little extra work, and makes it the
    first ListItem regardless of what is in the database. Hopefully this helps.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Randy Galliano" <> wrote in message
    news:ukWS3p%...
    > Hello,
    >
    > I am populating a drop down list control with a table from a database. I
    > would like the control to display a value such as --- select --- when it
    > first comes up, instead of any values from the table. Is there a way to
    > do this without putting the value --- Select --- in the actual table?
    >
    > Regards,
    >
    > Randy.
     
    Nathan Sokalski, Nov 6, 2007
    #2
    1. Advertising

  3. Thank you so much. That worked very well. I put it in the Page_Load
    event so I also added a check for ispostback.

    Regards,

    Randy.


    Nathan Sokalski wrote:
    > Yes, after calling the Databind() method, use the use the Items.Insert()
    > method. Here is an example:
    >
    > MyDropDown.DataBind()
    > MyDropDown.Items.Insert(0,"--- Select ---")
    >
    > This inserts an extra ListItem before index 0, therefore changing what was
    > previously index 0 into index 1, index 1 into index 2, etc. Since this is
    > only 1 extra line of code, it is very little extra work, and makes it the
    > first ListItem regardless of what is in the database. Hopefully this helps.
     
    Randy Galliano, Nov 7, 2007
    #3
    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. SirPoonga
    Replies:
    2
    Views:
    806
    Ben Strackany
    Jan 7, 2005
  2. thebison

    DataBound Drop Down List

    thebison, Apr 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,859
    antelopepiledriver
    Jul 16, 2007
  3. weiwei
    Replies:
    0
    Views:
    1,052
    weiwei
    Jan 5, 2007
  4. mc
    Replies:
    1
    Views:
    111
    Walter Wang [MSFT]
    Jun 26, 2006
  5. Replies:
    5
    Views:
    304
Loading...

Share This Page