473,695 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Default Selected Item in DropDownList

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 extra item
just doesn't get added to the DropDownList:

=============== =============== ===============
<script runat="server">
Sub Page_Load(..... .....)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection(" ....")
sqlDapter = New SqlDataAdapter( "SELECT CompName FROM Company",
sqlConn)

ddlCompany.Item s.Insert(0, "SELECT COMPANY")

dSet = New DataSet()
sqlDapter.Fill( dSet, "Company")

ddlCompany.Data Source = dSet.Tables("Co mpany").Default View
ddlCompany.Data Bind()
End Sub
</script>

<form runat="server">
<asp:DropDownLi st ID="ddlCompany " DataTextField=" CompName"
runat="server"/>
</form>
=============== =============== ===============

What am I doing wrong here?

Mar 2 '07 #1
4 6134
Try doing your insert after the databinding

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com
<rn**@rediffmai l.comwrote in message
news:11******** **************@ p10g2000cwp.goo glegroups.com.. .
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 extra item
just doesn't get added to the DropDownList:

=============== =============== ===============
<script runat="server">
Sub Page_Load(..... .....)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection(" ....")
sqlDapter = New SqlDataAdapter( "SELECT CompName FROM Company",
sqlConn)

ddlCompany.Item s.Insert(0, "SELECT COMPANY")

dSet = New DataSet()
sqlDapter.Fill( dSet, "Company")

ddlCompany.Data Source = dSet.Tables("Co mpany").Default View
ddlCompany.Data Bind()
End Sub
</script>

<form runat="server">
<asp:DropDownLi st ID="ddlCompany " DataTextField=" CompName"
runat="server"/>
</form>
=============== =============== ===============

What am I doing wrong here?

Mar 2 '07 #2
On Mar 2, 11:55 pm, r...@rediffmail .com wrote:
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 extra item
just doesn't get added to the DropDownList:

=============== =============== ===============
<script runat="server">
Sub Page_Load(..... .....)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection(" ....")
sqlDapter = New SqlDataAdapter( "SELECT CompName FROM Company",
sqlConn)

ddlCompany.Item s.Insert(0, "SELECT COMPANY")

dSet = New DataSet()
sqlDapter.Fill( dSet, "Company")

ddlCompany.Data Source = dSet.Tables("Co mpany").Default View
ddlCompany.Data Bind()
End Sub
</script>

<form runat="server">
<asp:DropDownLi st ID="ddlCompany " DataTextField=" CompName"
runat="server"/>
</form>
=============== =============== ===============

What am I doing wrong here?
Databindng any ASP.NET control by default clears all child controls
before that, so adding an item first then databinding clears the first
item. The solution is to first databind, then use InsertAt(0, ...) or
I think a better approach is to use the newly introduced
AppendDataBound Items property and set it to True prior to databinding.
Example:

ddlCompany.Item s.Insert(0, "SELECT COMPANY")
ddlCompany.Appe ndDataBoundItem s = True

Mar 3 '07 #3
On Mar 3, 10:03 pm, "Kevin" <Kevin.News...@ gmail.comwrote:
On Mar 2, 11:55 pm, r...@rediffmail .com wrote:


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 extra item
just doesn't get added to the DropDownList:
=============== =============== ===============
<script runat="server">
Sub Page_Load(..... .....)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter
sqlConn = New SqlConnection(" ....")
sqlDapter = New SqlDataAdapter( "SELECT CompName FROM Company",
sqlConn)
ddlCompany.Item s.Insert(0, "SELECT COMPANY")
dSet = New DataSet()
sqlDapter.Fill( dSet, "Company")
ddlCompany.Data Source = dSet.Tables("Co mpany").Default View
ddlCompany.Data Bind()
End Sub
</script>
<form runat="server">
<asp:DropDownLi st ID="ddlCompany " DataTextField=" CompName"
runat="server"/>
</form>
=============== =============== ===============
What am I doing wrong here?

Databindng any ASP.NET control by default clears all child controls
before that, so adding an item first then databinding clears the first
item. The solution is to first databind, then use InsertAt(0, ...) or
I think a better approach is to use the newly introduced
AppendDataBound Items property and set it to True prior to databinding.
Example:

ddlCompany.Item s.Insert(0, "SELECT COMPANY")
ddlCompany.Appe ndDataBoundItem s = True- Hide quoted text -

- Show quoted text -
Thanks both David & Kevin for pointing out where I was erring.

Another somewhat related question pertaining to ListBox instead of the
DropDownList,

There are 2 ListBoxes in a Web form both of which are populated with
records from a database table. There's a Button as well clicking which
the items selected in the 1st ListBox gets added to the 2nd ListBox &
the selected items in the 1st ListBox get deleted. In other words, I
want to transfer items from one ListBox to another ListBox (both
ways). Users can select multiple items in the 2 ListBoxes.

Assume that both the ListBoxes have 10 items & the last 3 items in the
1st ListBox are *HHH*, *III* & *JJJ*. Also assume that at any given
time, only 6 items are visible to users in both the ListBoxes (to view
the remaining items, users will have to scroll down the ListBoxes).

I select these 3 items in the 1st ListBox & click the Button. These 3
items get appended at the very end of the 2nd ListBox & subsequently,
these 3 items get deleted from the 1st ListBox. This means that there
are now 7 items in the 1st ListBox & 13 items in the 2nd ListBox. No
problems till this point.

The problem is after these 3 items get appended to the 2nd ListBox, I
want the last item (which is *JJJ* in this case) to get selected by
default in the 2nd ListBox after the 3 items get appended to the 2nd
ListBox (better still would be if all the 3 items get selected by
default in the 2nd ListBox). In other words, to view the just appended
items in the 2nd ListBox, I don't want users to scroll down the 2nd
ListBox to view the newly appended 3 items. Instead, the 2nd ListBox
should automatically scroll down after the 3 items get appended to the
2nd ListBox.

This is where I am getting stuck. I couldn't locate any attribute or
method of the ListBox control to select the newly appended items in
the 2nd ListBox. Can someone please give me some idea on how do I go
about it?

The HTML source of the 2nd ListBox should something like this:

<select size="6" name="ListBox2" multiple="multi ple" id="ListBox2">
<option value=".....">. ....</option>
........
<option value="HHH" selected>HHH</option>
<option value="III" selected>III</option>
<option value="JJJ" selected>JJJ</option>
</select>

Mar 3 '07 #4
On Mar 4, 12:49 am, r...@rediffmail .com wrote:
On Mar 3, 10:03 pm, "Kevin" <Kevin.News...@ gmail.comwrote:


On Mar 2, 11:55 pm, r...@rediffmail .com wrote:
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 extra item
just doesn't get added to the DropDownList:
=============== =============== ===============
<script runat="server">
Sub Page_Load(..... .....)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter
sqlConn = New SqlConnection(" ....")
sqlDapter = New SqlDataAdapter( "SELECT CompName FROM Company",
sqlConn)
ddlCompany.Item s.Insert(0, "SELECT COMPANY")
dSet = New DataSet()
sqlDapter.Fill( dSet, "Company")
ddlCompany.Data Source = dSet.Tables("Co mpany").Default View
ddlCompany.Data Bind()
End Sub
</script>
<form runat="server">
<asp:DropDownLi st ID="ddlCompany " DataTextField=" CompName"
runat="server"/>
</form>
=============== =============== ===============
What am I doing wrong here?
Databindng any ASP.NET control by default clears all child controls
before that, so adding an item first then databinding clears the first
item. The solution is to first databind, then use InsertAt(0, ...) or
I think a better approach is to use the newly introduced
AppendDataBound Items property and set it to True prior to databinding.
Example:
ddlCompany.Item s.Insert(0, "SELECT COMPANY")
ddlCompany.Appe ndDataBoundItem s = True- Hide quoted text -
- Show quoted text -

Thanks both David & Kevin for pointing out where I was erring.

Another somewhat related question pertaining to ListBox instead of the
DropDownList,

There are 2 ListBoxes in a Web form both of which are populated with
records from a database table. There's a Button as well clicking which
the items selected in the 1st ListBox gets added to the 2nd ListBox &
the selected items in the 1st ListBox get deleted. In other words, I
want to transfer items from one ListBox to another ListBox (both
ways). Users can select multiple items in the 2 ListBoxes.

Assume that both the ListBoxes have 10 items & the last 3 items in the
1st ListBox are *HHH*, *III* & *JJJ*. Also assume that at any given
time, only 6 items are visible to users in both the ListBoxes (to view
the remaining items, users will have to scroll down the ListBoxes).

I select these 3 items in the 1st ListBox & click the Button. These 3
items get appended at the very end of the 2nd ListBox & subsequently,
these 3 items get deleted from the 1st ListBox. This means that there
are now 7 items in the 1st ListBox & 13 items in the 2nd ListBox. No
problems till this point.

The problem is after these 3 items get appended to the 2nd ListBox, I
want the last item (which is *JJJ* in this case) to get selected by
default in the 2nd ListBox after the 3 items get appended to the 2nd
ListBox (better still would be if all the 3 items get selected by
default in the 2nd ListBox). In other words, to view the just appended
items in the 2nd ListBox, I don't want users to scroll down the 2nd
ListBox to view the newly appended 3 items. Instead, the 2nd ListBox
should automatically scroll down after the 3 items get appended to the
2nd ListBox.

This is where I am getting stuck. I couldn't locate any attribute or
method of the ListBox control to select the newly appended items in
the 2nd ListBox. Can someone please give me some idea on how do I go
about it?

The HTML source of the 2nd ListBox should something like this:

<select size="6" name="ListBox2" multiple="multi ple" id="ListBox2">
<option value=".....">. ....</option>
.......
<option value="HHH" selected>HHH</option>
<option value="III" selected>III</option>
<option value="JJJ" selected>JJJ</option>
</select>- Hide quoted text -

- Show quoted text -
OK...I managed to select the newly appended 3 items in the 2nd ListBox
using the following code:

=============== =============== ==========
Dim strItem As String
Dim arrSelItems As Array
Dim strSelItems As String

strSelItems = Trim(Request.Fo rm("ListBox1") )
arrSelItems = Split(strSelIte ms, ",")

For Each strItem In arrSelItems
ListBox2.Items. Add(strItem)
ListBox1.Items. Remove(strItem)
ListBox2.Items. FindByText(strI tem).Selected = True
Next
=============== =============== ==========

But the ListBox doesn't scroll down to the end automatically though
the 3 appended items get selected. Any idea how do I do this (maybe
using JavaScript)?

Mar 3 '07 #5

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

Similar topics

1
3500
by: Aaron Prohaska | last post by:
I'm having the problem with this drop down list on postback. For some reason both the ListItems get selected when I change the selected item. Using the code below I'm building the drop down list in the overriden CreateChildControls method and setting the selected item. Then when I change the item in the drop down list the list is rebuilt from viewstate, but the initial item is still selected causing the error below. I also have a number...
5
10830
by: Kris Rockwell | last post by:
Hello (again), I have gotten the dropdown list functionality to work through a few tricks (probably not the most efficient, but it works) but I am not sure how to set the default selected value. I have tried setting various values to represent the default value with little success. Essentially I am looking to have the first item in the list be the default selected item. I noticed in the properties for a dtat grid you could set a default...
2
11309
by: huzz | last post by:
How do i make a dropdownlist selected value based on the value i retrive from the database. Basically i have an edit page and like to display the default value in a dropdown list from the database. for example: if the ddl_value is 2 from the database i want the second list item to be selected by default.
10
10753
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected = true; Where endTime is a string containing "15".
2
1455
by: Brian Henry | last post by:
is there anyway to set the default selected item for a combobox's dropdownlist? I have items in the items collection, but at runtime nothing is selected by default, I'd like to have an item selected when the form shows instead of nothing. thanks
1
10392
by: Ben | last post by:
I have a formview with a few dropdownlists (software version, database version, etc). When a software version is selected, the database version dropdownlist updates itself accordingly. When in edit mode, everything works fine - the selected value in each list defaults to the value from the database record (selectedvalue=bind...). However, in insert mode (insertitem), it is not working. I pass in a value on the query string that I would...
11
5809
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
3
5278
by: Iain | last post by:
Hi All I have 2 DropDownList boxes on a page. The first (id= "Operation") is populated on PageLoad with the contents of a database table. The second id="WorkStations" will not be populated until the first has been changed The definitions are below.
5
26630
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 the data from a single objectdatasource. All textboxes and ddl(s) support null values. Textboxes are for entering numbers and ddl(s) are for selecting the unit (mm, cm, in, m3, oz, qt like these.). I am using the below code to achieve two...
0
8625
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8565
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,...
0
9113
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8977
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8841
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
8822
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...
0
5838
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
4339
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...
1
2997
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

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.