ASP.net 2.0 DropdownList bound to SQLDataSource and setting SelectedIndex


J

Jason Wilson

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
 
Ad

Advertisements


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

Top