Add default option to databound dropdownlist

I

IMB

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
 
G

Guest

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top