469,589 Members | 2,222 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,589 developers. It's quick & easy.

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 7054
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Tim::.. | last post: by
4 posts views Thread by P. Yanzick | last post: by
1 post views Thread by clickon | last post: by
3 posts views Thread by gsauns | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.