473,385 Members | 1,727 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,385 software developers and data experts.

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.Items.Insert(0, "SELECT COMPANY")

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

ddlCompany.DataSource = dSet.Tables("Company").DefaultView
ddlCompany.DataBind()
End Sub
</script>

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

What am I doing wrong here?

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

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com
<rn**@rediffmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.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.Items.Insert(0, "SELECT COMPANY")

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

ddlCompany.DataSource = dSet.Tables("Company").DefaultView
ddlCompany.DataBind()
End Sub
</script>

<form runat="server">
<asp:DropDownList 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.Items.Insert(0, "SELECT COMPANY")

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

ddlCompany.DataSource = dSet.Tables("Company").DefaultView
ddlCompany.DataBind()
End Sub
</script>

<form runat="server">
<asp:DropDownList 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
AppendDataBoundItems property and set it to True prior to databinding.
Example:

ddlCompany.Items.Insert(0, "SELECT COMPANY")
ddlCompany.AppendDataBoundItems = 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.Items.Insert(0, "SELECT COMPANY")
dSet = New DataSet()
sqlDapter.Fill(dSet, "Company")
ddlCompany.DataSource = dSet.Tables("Company").DefaultView
ddlCompany.DataBind()
End Sub
</script>
<form runat="server">
<asp:DropDownList 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
AppendDataBoundItems property and set it to True prior to databinding.
Example:

ddlCompany.Items.Insert(0, "SELECT COMPANY")
ddlCompany.AppendDataBoundItems = 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="multiple" 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.Items.Insert(0, "SELECT COMPANY")
dSet = New DataSet()
sqlDapter.Fill(dSet, "Company")
ddlCompany.DataSource = dSet.Tables("Company").DefaultView
ddlCompany.DataBind()
End Sub
</script>
<form runat="server">
<asp:DropDownList 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
AppendDataBoundItems property and set it to True prior to databinding.
Example:
ddlCompany.Items.Insert(0, "SELECT COMPANY")
ddlCompany.AppendDataBoundItems = 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="multiple" 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.Form("ListBox1"))
arrSelItems = Split(strSelItems, ",")

For Each strItem In arrSelItems
ListBox2.Items.Add(strItem)
ListBox1.Items.Remove(strItem)
ListBox2.Items.FindByText(strItem).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
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...
5
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....
2
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...
10
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 =...
2
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...
1
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...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
3
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.