Default Selected Item in DropDownList

R

rn5a

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?
 
K

Kevin

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
 
R

rn5a

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>
 
R

rn5a

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)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DropDownList DataGrid 1
Show "No Records Found" Message 1
Insert New Record 3
DB Value in Label 1
DataBind In ItemDataBound Event 1
DataGrid.Columns(Index) 3
SqlDataAdapter & Label 0
Display Records Instantly 0

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top