By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,197 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,197 IT Pros & Developers. It's quick & easy.

Add default option to databound dropdownlist

P: n/a
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
Apr 5 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Apr 5 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.