473,385 Members | 1,333 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.

ASP.net 2.0 DropdownList bound to SQLDataSource and setting SelectedIndex

I have two dropdownlists that are bound to the same datasource and I
have a couple of questions:

1) Because they are bound to the same datasource, I am assuming that
they only make 1 round trip to the DB server -- is that correct?

2) On a user's click of a button, one of the actions in the button
OnClick event is to set the selectedindex of the 2nd DDL to that of the
1st. I get a runtime error here that says that the given index is not
in the list of items. Is this because the button event is firing
before the listitems are populated? If so how do I get the list items
to populate 1st? Also the list items will probably not change in the
term of a user session, is there a way to prevent the page from
retreiving the listitems on each postback?

Here is my relevant code:

<asp:SqlDataSource ID="dsICD9" runat="server"
ConnectionString="<%$...%>"
SelectCommand="...">
</asp:SqlDataSource>

<asp:DropDownList AppendDataBoundItems="true" ID="ddl1"
runat="server" DataSourceID="dsICD9"
onkeypress="return KeySortDropDownList_onkeypress(this,false)"
DataTextField="description" DataValueField="ICD9">

<asp:ListItem Selected="True"></asp:ListItem>
</asp:DropDownList>

<asp:DropDownList AppendDataBoundItems="true" ID="ddl2"
runat="server" DataSourceID="dsICD9"
onkeypress="return KeySortDropDownList_onkeypress(this,false)"
DataTextField="description" DataValueField="ICD9">

<asp:ListItem Selected="True"></asp:ListItem>
</asp:DropDownList>

Protected Sub AddCPT_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
...
ddl2.SelectedIndex = ddl1.SelectedIndex <-----Here's where I get
the runtime error
...
End Sub

Sep 13 '06 #1
1 7192
Hi Jason,

Your code seems to work against my database. Here's what I used:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub AddCPT_Click _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
ddl2.SelectedIndex = ddl1.SelectedIndex
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:sqldatasource id="dsICD9" runat="server" connectionstring="<%$
ConnectionStrings:authorsConnectionString %>" selectcommand="SELECT
[FirstName], [AuthorID] FROM [Authors]"></asp:sqldatasource>
<asp:dropdownlist id="ddl1" runat="server"
appenddatabounditems="true" datasourceid="dsICD9" datatextfield="FirstName"
datavaluefield="AuthorID" >
<asp:listitem selected="True"></asp:listitem>
</asp:dropdownlist>
<asp:dropdownlist id="ddl2" runat="server"
appenddatabounditems="true" datasourceid="dsICD9" datatextfield="FirstName"
datavaluefield="AuthorID" >
<asp:listitem selected="True"></asp:listitem>
</asp:dropdownlist>
<asp:button id="AddCPT" runat="server" text="Button"
onclick="AddCPT_Click" />
</div>
</form>
</body>
</html>
"Jason Wilson" <wi*****@ausrad.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>I have two dropdownlists that are bound to the same datasource and I
have a couple of questions:

1) Because they are bound to the same datasource, I am assuming that
they only make 1 round trip to the DB server -- is that correct?

2) On a user's click of a button, one of the actions in the button
OnClick event is to set the selectedindex of the 2nd DDL to that of the
1st. I get a runtime error here that says that the given index is not
in the list of items. Is this because the button event is firing
before the listitems are populated? If so how do I get the list items
to populate 1st? Also the list items will probably not change in the
term of a user session, is there a way to prevent the page from
retreiving the listitems on each postback?

Here is my relevant code:

<asp:SqlDataSource ID="dsICD9" runat="server"
ConnectionString="<%$...%>"
SelectCommand="...">
</asp:SqlDataSource>

<asp:DropDownList AppendDataBoundItems="true" ID="ddl1"
runat="server" DataSourceID="dsICD9"
onkeypress="return KeySortDropDownList_onkeypress(this,false)"
DataTextField="description" DataValueField="ICD9">

<asp:ListItem Selected="True"></asp:ListItem>
</asp:DropDownList>

<asp:DropDownList AppendDataBoundItems="true" ID="ddl2"
runat="server" DataSourceID="dsICD9"
onkeypress="return KeySortDropDownList_onkeypress(this,false)"
DataTextField="description" DataValueField="ICD9">

<asp:ListItem Selected="True"></asp:ListItem>
</asp:DropDownList>

Protected Sub AddCPT_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
...
ddl2.SelectedIndex = ddl1.SelectedIndex <-----Here's where I get
the runtime error
...
End Sub

Sep 14 '06 #2

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

Similar topics

3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
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 =...
4
by: P. Yanzick | last post by:
Hello, I've been playing with master/detail views as well as editing in the gridview, and I ran across a strange problem that I am not exactly sure where to go to try to solve. I have 2...
6
by: Dabbler | last post by:
I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to an SqlDataSource, then have a value from a boundfield in the row be passed as the keyfield for select where clause. Im...
2
by: Dabbler | last post by:
In my Registrant FormView I have a DropDownList which loads data from a secondary SqlDataSource "sdsOfficeParks". I need the user to select an office park but save the selected value in the...
1
by: clickon | last post by:
Forget about the controlParameter for the moment, for testing purposes i have created the following Markup: <asp:Table ID="tblSelectRoute" runat="server" CssClass="asp-table"> <asp:TableRow>...
3
by: gsauns | last post by:
Hello, I have an ASP.NET app with a DropDownList on the page, which is bound to values from a table. I have a Repeater control on the page whose displayed data is dependent on the DropDownList...
1
by: ken_wilsonii | last post by:
I ran into this problem it ended up being a combination of this that needed to be done. I believe this will work for a bound list to. The whole problem stems from the two-way binding which has...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.