ASP.net 2.0 DropdownList bound to SQLDataSource and setting SelectedIndex

Discussion in 'ASP .Net Web Controls' started by Jason Wilson, Sep 13, 2006.

  1. Jason Wilson

    Jason Wilson Guest

    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
    Jason Wilson, Sep 13, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. K Bryan
    Replies:
    4
    Views:
    2,445
    Andrew de la Harpe
    Apr 15, 2004
  2. keithb
    Replies:
    1
    Views:
    11,910
    shauns1
    Nov 1, 2006
  3. Jason Wilson
    Replies:
    1
    Views:
    13,263
    Ken Cox [Microsoft MVP]
    Sep 14, 2006
  4. S_K
    Replies:
    5
    Views:
    749
    Eliyahu Goldin
    Nov 14, 2007
  5. wrecker
    Replies:
    1
    Views:
    168
    Wardeaux
    Feb 13, 2004
Loading...

Share This Page