473,725 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using DropDownLists

et
How can I add a blank item to a drop down list so that it is the first item
in the list?

My drop down list is bound to a datareader, and if I add a blank item first
as follows --
ddl.Items.Add(N ew ListItem(" ", 0)) -- then binding erases the blank item.

Or maybe there is another way to solve my problem. An item in the drop down
list is automatically selected by FindByValue:

ddl.SelectedInd ex =
ddl.Items.Index Of(ddl.Items.Fi ndByValue(Sessi on("ID")))

If the above does not find a match, the SelectedIndex defaults to 0, and
hence the first item is selected. Since the first item contains a valid
value, I end up with a false result. I can't have it select the last item
if the SelectedIndex defaults to 0, because then when the first item should
be selected correctly, it ends up blank, again erroneously.

Hope that makes sense. Thanks for your help.

Apr 2 '06 #1
4 1421
I suggest you add the blank item AFTER the databinding.
You can do this by handling the DataBound event for the list,
and adding the item in that handler.

--

Riki

"et" <ea************ *@yahoo.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
How can I add a blank item to a drop down list so that it is the first
item in the list?

My drop down list is bound to a datareader, and if I add a blank item
first as follows --
ddl.Items.Add(N ew ListItem(" ", 0)) -- then binding erases the blank item.

Or maybe there is another way to solve my problem. An item in the drop
down list is automatically selected by FindByValue:

ddl.SelectedInd ex =
ddl.Items.Index Of(ddl.Items.Fi ndByValue(Sessi on("ID")))

If the above does not find a match, the SelectedIndex defaults to 0, and
hence the first item is selected. Since the first item contains a valid
value, I end up with a false result. I can't have it select the last item
if the SelectedIndex defaults to 0, because then when the first item
should be selected correctly, it ends up blank, again erroneously.

Hope that makes sense. Thanks for your help.


Apr 2 '06 #2
Are you adding this new listitem after you have bound to the datareader? If
not I believe you should.

Example:
Try
myConnection.Op en()

myDataReader = myCommand.Execu teReader(Comman dBehavior.Close Connection)

DropDownList1.D ataSource = myDataReader
DropDownList1.D ataBind()
Catch myException As Exception
Response.Write( "An error has occurred: " & myException.ToS tring())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Cl ose()
End If

DropDownList1.I tems.Insert(0, "Select an Item")
DropDownList1.S electedIndex = 0
End Try
"et" wrote:
How can I add a blank item to a drop down list so that it is the first item
in the list?

My drop down list is bound to a datareader, and if I add a blank item first
as follows --
ddl.Items.Add(N ew ListItem(" ", 0)) -- then binding erases the blank item.

Or maybe there is another way to solve my problem. An item in the drop down
list is automatically selected by FindByValue:

ddl.SelectedInd ex =
ddl.Items.Index Of(ddl.Items.Fi ndByValue(Sessi on("ID")))

If the above does not find a match, the SelectedIndex defaults to 0, and
hence the first item is selected. Since the first item contains a valid
value, I end up with a false result. I can't have it select the last item
if the SelectedIndex defaults to 0, because then when the first item should
be selected correctly, it ends up blank, again erroneously.

Hope that makes sense. Thanks for your help.


Apr 2 '06 #3
et
Thanks for your input.

I am adding the blank item after it is bound to the datareader. However, my
problem is not adding it, it adds it in just fine, but it adds it to the
bottom of the list, not the top of the list. I want it to be at the top of
the list.

"et" <ea************ *@yahoo.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
How can I add a blank item to a drop down list so that it is the first
item in the list?

My drop down list is bound to a datareader, and if I add a blank item
first as follows --
ddl.Items.Add(N ew ListItem(" ", 0)) -- then binding erases the blank item.

Or maybe there is another way to solve my problem. An item in the drop
down list is automatically selected by FindByValue:

ddl.SelectedInd ex =
ddl.Items.Index Of(ddl.Items.Fi ndByValue(Sessi on("ID")))

If the above does not find a match, the SelectedIndex defaults to 0, and
hence the first item is selected. Since the first item contains a valid
value, I end up with a false result. I can't have it select the last item
if the SelectedIndex defaults to 0, because then when the first item
should be selected correctly, it ends up blank, again erroneously.

Hope that makes sense. Thanks for your help.


Apr 2 '06 #4
Use Insert instead of Add.

Eliyahu

"et" <ea************ *@yahoo.com> wrote in message
news:O8******** *****@TK2MSFTNG P12.phx.gbl...
Thanks for your input.

I am adding the blank item after it is bound to the datareader. However,
my problem is not adding it, it adds it in just fine, but it adds it to
the bottom of the list, not the top of the list. I want it to be at the
top of the list.

"et" <ea************ *@yahoo.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
How can I add a blank item to a drop down list so that it is the first
item in the list?

My drop down list is bound to a datareader, and if I add a blank item
first as follows --
ddl.Items.Add(N ew ListItem(" ", 0)) -- then binding erases the blank
item.

Or maybe there is another way to solve my problem. An item in the drop
down list is automatically selected by FindByValue:

ddl.SelectedInd ex =
ddl.Items.Index Of(ddl.Items.Fi ndByValue(Sessi on("ID")))

If the above does not find a match, the SelectedIndex defaults to 0, and
hence the first item is selected. Since the first item contains a valid
value, I end up with a false result. I can't have it select the last
item if the SelectedIndex defaults to 0, because then when the first item
should be selected correctly, it ends up blank, again erroneously.

Hope that makes sense. Thanks for your help.



Apr 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7927
by: Marlene Harkcom | last post by:
I've got a datagrid with 5 bound columns in it as well as an edit/update/cancel column. I'm binding this to a dataset. One of the fields is always going to be one of three values. I want the user to be able to hit the edit button and have this particular column become a drop down list where the true value is selected, but they can choose one of the other two values. I've been able to add a dropdownlist column as a template column, but...
4
1339
by: | last post by:
Hi All I have a question about re-using a drop downlist box across several questions on a questionaire like form. I will include code after some explaination. I thought i could make a dropdownlist box object global and re-use it as needed if i did not destroy the object, but it doesnt seem to be working out for me.
0
1399
by: Steve Caliendo | last post by:
Hi, I'm having trouble with multiple dynamically generated dropdownlists. On my web page I have several text boxes / labels / checkboxes and dropdownlists I have no problem setting the other controls to display what I would like them to, but the dropdownlist boxes are simply not working. I can't set the selectedindex to be different for each of the controls. What I'm doing is this:
3
1204
by: Jim Bancroft | last post by:
Hi everyone, I'd like to put some DropDownLists in my DataGrid, populating each from a database query. I'm having a little trouble naming them based on the current record in the DataSet. For example, here's a snippet of what I have now in my DataGrid <ItemTemplate> <asp:DropDownList runat="server" id="lstCategories"
2
1213
by: Jay | last post by:
I'm having a weird problem in ASP.NET 1.1. I have two DropDownLists in a form. Both lists are build identically but separately. Somehow the DropDownLists are getting crossed in memory, because when I set the SelectedValue for the second list, the first list's SelectedValue becomes set to the SelectedValue of the second list. I'm also getting this exception during the render process: "A DropDownList cannot have multiple items selected."...
8
1570
by: MattB | last post by:
I have a asp.net 1.1/vb application that has a page with a bunch of dynamically added User Controls. When I add the controls, I set the UserControl.EnableViewState to true. For all my controls with TextBoxes, I can get the value back out of the controls just fine. One of my User Controls has some DropDownLists as well as a TextBox. I get a value from the TextBox on postback, but not the DropDownLists. Even if I see a value selected when...
0
970
by: ASP Developer | last post by:
I need to design a user control that will contain multiple dropdownlists. I am trying to determine the best approach to do this. I could expose every property that is necessary for all of the dropdownlists or I could expose methods that users could pass in values and specify which dropdownlist the values pertain to? So for example, SelectDropdownChoice(MyID,"Dropdownlist3") or
0
1132
by: Carlos | last post by:
Hi all, I just embedded a dropdownlist templatefield inside a gridview. When I look at the control in design mode, I see 5 dropdownlists, when I bind the data to the gridview, I only bind to 3 dropdownlists. How can the number of controls that are itemtemplates can be controlled? That is, if in fact I want to display 5 dropdownlists containing the same data ?
3
1649
by: RSH | last post by:
Hi, I am having this very bizarre occurance with 4 dropdown lists on my ASP .Net page. Each control has its own unique id. When the user selects a value from each of the dropdownlists it is written correctly to the database. HOWEVER...When setting the SelectedIndex property of each of the controls in the codebehind (.net 1.1) I get an exception "A dropdownlist can not have
3
14433
by: dianaj86 | last post by:
I have multiple dropdownlists each one filled with values from a specific column in the table. Also I have multiple textboxes corresponding to dropdownlists. For example, when I select an item from dropdownlistA, all the textboxes are filled with the first row values that contains that selected item and gives the number of rows containing this value……. In addition, I have 2 buttons one is Move Forward Button and the other is Move Previous…I am...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.