Add default option to databound dropdownlist

Discussion in 'ASP .Net' started by IMB, Apr 5, 2006.

  1. IMB

    IMB Guest

    I'm using VS2005 and am working with a ASP.NET 2.0 web site and I'm coding
    in VB.NET.

    I understand that I can add an item to a databound dropdownlist and make it
    the default selection using the following code:

    MyDropDownList.Items.Insert(0, "My default item")
    MyDropDownList.SelectedIndex = 0

    The problem that I am experiencing is *where* to put the above code! If I
    place it in the Page_Load sub, the code is not applied, so I'm wondering
    where I need to put the code in order for it to be applied after the
    databinding has occured.

    If, for example, I attach the code to a linkbutton, then it does exactly
    what I expect, but understandably, I want the default option to be shown in
    the dropdownlist when the page is first rendered.

    I have played around with the various Page Events, but to no avail. If it's
    any help, I'm using 'code behind'.

    I'd very much appreciate any advice.

    Thanks in advance,
    IMB
     
    IMB, Apr 5, 2006
    #1
    1. Advertising

  2. You could put it in the OnDataBound event for the DropDownList, this will do
    it once the data has been bound and the values populated to the list.

    You could also do it declaritvely by adding the following to the
    DropDownList control:

    <asp:ListItem Selected="True" Text="SomeText"
    Value="SomeValue"></asp:ListItem>

    And then setting the AppendDataBoundItems property of the DropdownList to
    true.

    "IMB" wrote:

    > I'm using VS2005 and am working with a ASP.NET 2.0 web site and I'm coding
    > in VB.NET.
    >
    > I understand that I can add an item to a databound dropdownlist and make it
    > the default selection using the following code:
    >
    > MyDropDownList.Items.Insert(0, "My default item")
    > MyDropDownList.SelectedIndex = 0
    >
    > The problem that I am experiencing is *where* to put the above code! If I
    > place it in the Page_Load sub, the code is not applied, so I'm wondering
    > where I need to put the code in order for it to be applied after the
    > databinding has occured.
    >
    > If, for example, I attach the code to a linkbutton, then it does exactly
    > what I expect, but understandably, I want the default option to be shown in
    > the dropdownlist when the page is first rendered.
    >
    > I have played around with the various Page Events, but to no avail. If it's
    > any help, I'm using 'code behind'.
    >
    > I'd very much appreciate any advice.
    >
    > Thanks in advance,
    > IMB
    >
    >
    >
     
    =?Utf-8?B?Y2xpY2tvbg==?=, Apr 5, 2006
    #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. jm
    Replies:
    0
    Views:
    632
  2. Simon Harvey
    Replies:
    2
    Views:
    415
    Christopher Reed
    Jan 18, 2006
  3. rodchar
    Replies:
    0
    Views:
    397
    rodchar
    Nov 26, 2007
  4. Manu
    Replies:
    0
    Views:
    172
  5. Lagrange
    Replies:
    1
    Views:
    569
    Lagrange
    Jan 17, 2009
Loading...

Share This Page