473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Add default option to databound dropdownlist

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
1 2848
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Harold | last post by:
I have read the following article http://www.4guysfromrolla.com/webtech/073101-1.shtml. I added the empty selection as the first option. When I click submit without choosing anything it does not...
9
by: Jakob Lithner | last post by:
1) I have a DataGridView with edit capability. But in some columns I want to limit the input with a DropDownList. There is no inbuilt column for DropDownLists so I intended to add one myself. I...
1
by: DJ | last post by:
I have a DropDownList in Visual Web Developer that is databound to a SQL Database. Whenever the web page is opened the DropDownList is automatically filled with the first item in the table. My...
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
0
by: Mark Micallef | last post by:
Hi, I'm having a problem using a databound dropdownlist inside a reorderlist ajax control. Here's a snippet of the code I'm using: <InsertItemTemplate> <div class="insertArea">...
1
by: Mark Micallef | last post by:
Hi, this question relates to a control in the Ajax toolkit for asp.net 2. I'm having a problem using a databound dropdownlist inside a reorderlist ajax control. Here's a snippet of the code I'm...
3
by: dizzy77 | last post by:
I have a dropdownlist databound to table column with string values "n" i "y". <asp:DropDownList ID="ddlPrivate" runat="server" SelectedValue='<%# Bind("cPrivate") %>'> ...
2
by: Cirene | last post by:
In my databound datagrid i have a databound dropdownlist, like this: <asp:DropDownList ID="ddlAvail0" runat="server" SelectedValue='<%# Bind("AvailMon") %>'> <asp:ListItem></asp:ListItem>...
5
by: nzkks | last post by:
Hi I am using these: ASP.Net 2.0, VB.Net, Visual Studio 2005, SQL Server 2005, Formview controls In a ASP.Net form I have 20 textboxes and 20 dropdownlists(ddl). All ddl(s) are databound and get...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.