By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

Default Selected Item in DropDownList

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.