Bound dropdownlist in .NET 2.0 -- picking the selected value?

Discussion in 'ASP .Net Web Controls' started by anyeone, Dec 19, 2005.

  1. anyeone

    anyeone Guest

    I'm finding articles all over the place saying how to bind a dropdownlist to
    an objectdatasource or sqldatasource in .NET 2.0/VS2005 but nothing that says
    how to get the selected value from another datasource.

    Here's the scenario.
    I'm essentially loading an order for a client. There is a dropdownlist
    (ddlClient) with a list of clients (populated by a sqldatasource). Each
    ListItem's value is the clientID.

    <asp:SqlDataSource ID="sqlClientSearch" runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    SelectCommand="proc_ClientLookup" SelectCommandType="StoredProcedure"
    ProviderName="System.Data.SqlClient">
    <SelectParameters />
    </asp:SqlDataSource>

    I need to load the Order object (from another datasource) and then select
    the clientID on the Order in ddlClient. I tried doing this in the code
    behind but when the Page_Load() runs, the binding of the ddlClient hasn't
    occurred yet so it contains no ListItems (and thus
    ddlClient.Items.FindByValue(clientID) doesn't work).

    So I'm presuming I need to create a second data source in the aspx page that
    loads the individual order record like so:
    <asp:SqlDataSource ID="sqlOrderSearch" runat="server" ConnectionString="<%$
    ConnectionStrings:MyConnectionString %>"
    SelectCommand="proc_OrderLookup" SelectCommandType="StoredProcedure"
    ProviderName="System.Data.SqlClient">
    <SelectParameters> <asp:QueryStringParameter Name="OrderID"
    QueryStringField="OrderID" Type="Int32" />
    </asp:SqlDataSource>

    But what I can't figure out is how to tell the dropdownlist ddlClient to
    use the clientID field in the sqlOrderSearch datasource as the selected value.

    Can someone please assist?

    Thanks in advance!
    anyeone, Dec 19, 2005
    #1
    1. Advertising

  2. Your description is not clear. But to get the selected value of another
    datasource you use the SelectedValue of the object it is bound to. For
    example if you bind an objectdatasource to a dropdownlist then the selected
    value can be used in the select parameter of another objectdatasource like
    this:
    <SelectParameters>
    <asp:ControlParameter ControlID="ddlClients" Name="ClientID"
    PropertyName="SelectedValue"
    Type="String" />
    </SelectParameters>
    If the databound object is GridView then the SelectedDataKey value can be
    retrieved like this:
    <asp:ControlParameter ControlID="ChildGridView1" Name="ClientId"
    PropertyName="SelectedDataKey.Values[0]" Type="Int32" />

    Look at this sample and let me know if that answers your question:
    http://www.webswapp.com/CodeSamples/aspnet20/GridView_2c.aspx

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "anyeone" wrote:

    > I'm finding articles all over the place saying how to bind a dropdownlist to
    > an objectdatasource or sqldatasource in .NET 2.0/VS2005 but nothing that says
    > how to get the selected value from another datasource.
    >
    > Here's the scenario.
    > I'm essentially loading an order for a client. There is a dropdownlist
    > (ddlClient) with a list of clients (populated by a sqldatasource). Each
    > ListItem's value is the clientID.
    >
    > <asp:SqlDataSource ID="sqlClientSearch" runat="server"
    > ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    > SelectCommand="proc_ClientLookup" SelectCommandType="StoredProcedure"
    > ProviderName="System.Data.SqlClient">
    > <SelectParameters />
    > </asp:SqlDataSource>
    >
    > I need to load the Order object (from another datasource) and then select
    > the clientID on the Order in ddlClient. I tried doing this in the code
    > behind but when the Page_Load() runs, the binding of the ddlClient hasn't
    > occurred yet so it contains no ListItems (and thus
    > ddlClient.Items.FindByValue(clientID) doesn't work).
    >
    > So I'm presuming I need to create a second data source in the aspx page that
    > loads the individual order record like so:
    > <asp:SqlDataSource ID="sqlOrderSearch" runat="server" ConnectionString="<%$
    > ConnectionStrings:MyConnectionString %>"
    > SelectCommand="proc_OrderLookup" SelectCommandType="StoredProcedure"
    > ProviderName="System.Data.SqlClient">
    > <SelectParameters> <asp:QueryStringParameter Name="OrderID"
    > QueryStringField="OrderID" Type="Int32" />
    > </asp:SqlDataSource>
    >
    > But what I can't figure out is how to tell the dropdownlist ddlClient to
    > use the clientID field in the sqlOrderSearch datasource as the selected value.
    >
    > Can someone please assist?
    >
    > Thanks in advance!
    Phillip Williams, Dec 19, 2005
    #2
    1. Advertising

  3. anyeone

    anyeone Guest

    Sorry, I must not have explained well because you aren't describing my
    scenario. , you're describing a totally different problem. I am not trying
    to filter on anything - I'm trying to preselect values already in the
    database.

    The page is a "Modify Order" typepage. It loads an Order from the database.
    Then it binds the Order data to fields on the form, one of which is the
    client dropdown.

    The client dropdownlist is populated with a list of values from the database
    (i.e. the list of clients from the Clients table). But only one of those
    values applies to this order. The Clients table doesn't know which one it is
    - it has to look at the Order object to pick the correct value.

    This is a pretty basic functionality that just about every site needs to be
    able to do, so I'm surprised at the lack of documentation on how to do it.

    An analogous case would be loading a customer profile that has the
    customer's state selected from a list of states. The states come from a
    states table, but to preselect the right one it has to look at the customer
    object.

    I hope this is clearer.

    I need to *pick* the selected value of this list based on another object,
    the Order.

    The order has a bunch of properties like OrderID, Product, and ClientID.

    I need the dropdown list of clients to preselect the ClientID from the order.

    The Order is not used in any bound controls.

    In the "olden days" I would just use the following to select the ddl's value:
    ddlClient.SelectedIndex =
    ddlClient.Items.IndexOf(ddlClient.Items.FindByValue(clientID);

    But this doesn't work since the binding to the list occurs AFTER all the
    code behind code runs.


    "Phillip Williams" wrote:

    > Your description is not clear. But to get the selected value of another
    > datasource you use the SelectedValue of the object it is bound to. For
    > example if you bind an objectdatasource to a dropdownlist then the selected
    > value can be used in the select parameter of another objectdatasource like
    > this:
    > <SelectParameters>
    > <asp:ControlParameter ControlID="ddlClients" Name="ClientID"
    > PropertyName="SelectedValue"
    > Type="String" />
    > </SelectParameters>
    > If the databound object is GridView then the SelectedDataKey value can be
    > retrieved like this:
    > <asp:ControlParameter ControlID="ChildGridView1" Name="ClientId"
    > PropertyName="SelectedDataKey.Values[0]" Type="Int32" />
    >
    > Look at this sample and let me know if that answers your question:
    > http://www.webswapp.com/CodeSamples/aspnet20/GridView_2c.aspx
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "anyeone" wrote:
    >
    > > I'm finding articles all over the place saying how to bind a dropdownlist to
    > > an objectdatasource or sqldatasource in .NET 2.0/VS2005 but nothing that says
    > > how to get the selected value from another datasource.
    > >
    > > Here's the scenario.
    > > I'm essentially loading an order for a client. There is a dropdownlist
    > > (ddlClient) with a list of clients (populated by a sqldatasource). Each
    > > ListItem's value is the clientID.
    > >
    > > <asp:SqlDataSource ID="sqlClientSearch" runat="server"
    > > ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    > > SelectCommand="proc_ClientLookup" SelectCommandType="StoredProcedure"
    > > ProviderName="System.Data.SqlClient">
    > > <SelectParameters />
    > > </asp:SqlDataSource>
    > >
    > > I need to load the Order object (from another datasource) and then select
    > > the clientID on the Order in ddlClient. I tried doing this in the code
    > > behind but when the Page_Load() runs, the binding of the ddlClient hasn't
    > > occurred yet so it contains no ListItems (and thus
    > > ddlClient.Items.FindByValue(clientID) doesn't work).
    > >
    > > So I'm presuming I need to create a second data source in the aspx page that
    > > loads the individual order record like so:
    > > <asp:SqlDataSource ID="sqlOrderSearch" runat="server" ConnectionString="<%$
    > > ConnectionStrings:MyConnectionString %>"
    > > SelectCommand="proc_OrderLookup" SelectCommandType="StoredProcedure"
    > > ProviderName="System.Data.SqlClient">
    > > <SelectParameters> <asp:QueryStringParameter Name="OrderID"
    > > QueryStringField="OrderID" Type="Int32" />
    > > </asp:SqlDataSource>
    > >
    > > But what I can't figure out is how to tell the dropdownlist ddlClient to
    > > use the clientID field in the sqlOrderSearch datasource as the selected value.
    > >
    > > Can someone please assist?
    > >
    > > Thanks in advance!
    anyeone, Dec 19, 2005
    #3
  4. Ok, If I understood you correctly (second attempt) look at my website and you
    would find this example doing exactly what you are trying to do:
    http://www.webswapp.com/CodeSamples/aspnet20/FormView1.aspx

    If that example does not answer your question, please do try another time.

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "anyeone" wrote:

    > Sorry, I must not have explained well because you aren't describing my
    > scenario. , you're describing a totally different problem. I am not trying
    > to filter on anything - I'm trying to preselect values already in the
    > database.
    >
    > The page is a "Modify Order" typepage. It loads an Order from the database.
    > Then it binds the Order data to fields on the form, one of which is the
    > client dropdown.
    >
    > The client dropdownlist is populated with a list of values from the database
    > (i.e. the list of clients from the Clients table). But only one of those
    > values applies to this order. The Clients table doesn't know which one it is
    > - it has to look at the Order object to pick the correct value.
    >
    > This is a pretty basic functionality that just about every site needs to be
    > able to do, so I'm surprised at the lack of documentation on how to do it.
    >
    > An analogous case would be loading a customer profile that has the
    > customer's state selected from a list of states. The states come from a
    > states table, but to preselect the right one it has to look at the customer
    > object.
    >
    > I hope this is clearer.
    >
    > I need to *pick* the selected value of this list based on another object,
    > the Order.
    >
    > The order has a bunch of properties like OrderID, Product, and ClientID.
    >
    > I need the dropdown list of clients to preselect the ClientID from the order.
    >
    > The Order is not used in any bound controls.
    >
    > In the "olden days" I would just use the following to select the ddl's value:
    > ddlClient.SelectedIndex =
    > ddlClient.Items.IndexOf(ddlClient.Items.FindByValue(clientID);
    >
    > But this doesn't work since the binding to the list occurs AFTER all the
    > code behind code runs.
    >
    >
    > "Phillip Williams" wrote:
    >
    > > Your description is not clear. But to get the selected value of another
    > > datasource you use the SelectedValue of the object it is bound to. For
    > > example if you bind an objectdatasource to a dropdownlist then the selected
    > > value can be used in the select parameter of another objectdatasource like
    > > this:
    > > <SelectParameters>
    > > <asp:ControlParameter ControlID="ddlClients" Name="ClientID"
    > > PropertyName="SelectedValue"
    > > Type="String" />
    > > </SelectParameters>
    > > If the databound object is GridView then the SelectedDataKey value can be
    > > retrieved like this:
    > > <asp:ControlParameter ControlID="ChildGridView1" Name="ClientId"
    > > PropertyName="SelectedDataKey.Values[0]" Type="Int32" />
    > >
    > > Look at this sample and let me know if that answers your question:
    > > http://www.webswapp.com/CodeSamples/aspnet20/GridView_2c.aspx
    > >
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "anyeone" wrote:
    > >
    > > > I'm finding articles all over the place saying how to bind a dropdownlist to
    > > > an objectdatasource or sqldatasource in .NET 2.0/VS2005 but nothing that says
    > > > how to get the selected value from another datasource.
    > > >
    > > > Here's the scenario.
    > > > I'm essentially loading an order for a client. There is a dropdownlist
    > > > (ddlClient) with a list of clients (populated by a sqldatasource). Each
    > > > ListItem's value is the clientID.
    > > >
    > > > <asp:SqlDataSource ID="sqlClientSearch" runat="server"
    > > > ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    > > > SelectCommand="proc_ClientLookup" SelectCommandType="StoredProcedure"
    > > > ProviderName="System.Data.SqlClient">
    > > > <SelectParameters />
    > > > </asp:SqlDataSource>
    > > >
    > > > I need to load the Order object (from another datasource) and then select
    > > > the clientID on the Order in ddlClient. I tried doing this in the code
    > > > behind but when the Page_Load() runs, the binding of the ddlClient hasn't
    > > > occurred yet so it contains no ListItems (and thus
    > > > ddlClient.Items.FindByValue(clientID) doesn't work).
    > > >
    > > > So I'm presuming I need to create a second data source in the aspx page that
    > > > loads the individual order record like so:
    > > > <asp:SqlDataSource ID="sqlOrderSearch" runat="server" ConnectionString="<%$
    > > > ConnectionStrings:MyConnectionString %>"
    > > > SelectCommand="proc_OrderLookup" SelectCommandType="StoredProcedure"
    > > > ProviderName="System.Data.SqlClient">
    > > > <SelectParameters> <asp:QueryStringParameter Name="OrderID"
    > > > QueryStringField="OrderID" Type="Int32" />
    > > > </asp:SqlDataSource>
    > > >
    > > > But what I can't figure out is how to tell the dropdownlist ddlClient to
    > > > use the clientID field in the sqlOrderSearch datasource as the selected value.
    > > >
    > > > Can someone please assist?
    > > >
    > > > Thanks in advance!
    Phillip Williams, Dec 19, 2005
    #4
  5. anyeone

    anyeone Guest

    That example is mostly there - except I don't want to use a formview to
    update the database. I want to use code-behind to commit changes because
    there is some pretty complex validation and differential behavior based on
    various field types etc. Also, my pages' form is always in "edit" mode, I
    don't want the user to have to click to enter edit mode.

    So is there a way to do this without displaying the dropdown list within a
    formview? It seems when I declare my dropdownlist within the FormView, I
    can't see it from the code-behind at all.

    Thanks for the help!


    "Phillip Williams" wrote:

    > Ok, If I understood you correctly (second attempt) look at my website and you
    > would find this example doing exactly what you are trying to do:
    > http://www.webswapp.com/CodeSamples/aspnet20/FormView1.aspx
    >
    > If that example does not answer your question, please do try another time.
    >
    anyeone, Dec 20, 2005
    #5
  6. anyeone

    anyeone Guest

    For anyone else reading this, I did find an alternate solution that is
    actually pretty close to what I was trying anyway. Not sure why it didn't
    occur to me earlier, since I've actually done this before. *sigh* Long day I
    suppose.

    Anyway. The binding to the dropdownlist occurs later in the lifecycle than
    the Page_Load - so the trick was finding where exactly it did occur and
    putting my code to select the proper value in an event that fires afterward.

    First I tried OnPreRender -- too early.

    Then I tried OnPreRenderComplete -- bingo.

    So, the moral of this story is - if you need to access the contents of a
    databound control from your code behind, you have to wait til after they are
    rendered and put that code in the OnPreRenderComplete (or in a method called
    from inside it) rather than triggering it from the Page_Load.

    Thanks again Phillip for your help, the example you sent will be useful for
    some other things I'm working on.

    Cheers,
    Anye
    anyeone, Dec 20, 2005
    #6
  7. You are welcome, Anye. And thanks for sharing the findings of your solution.
    I am glad you found one that works for your particular scenario.
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "anyeone" wrote:

    > For anyone else reading this, I did find an alternate solution that is
    > actually pretty close to what I was trying anyway. Not sure why it didn't
    > occur to me earlier, since I've actually done this before. *sigh* Long day I
    > suppose.
    >
    > Anyway. The binding to the dropdownlist occurs later in the lifecycle than
    > the Page_Load - so the trick was finding where exactly it did occur and
    > putting my code to select the proper value in an event that fires afterward.
    >
    > First I tried OnPreRender -- too early.
    >
    > Then I tried OnPreRenderComplete -- bingo.
    >
    > So, the moral of this story is - if you need to access the contents of a
    > databound control from your code behind, you have to wait til after they are
    > rendered and put that code in the OnPreRenderComplete (or in a method called
    > from inside it) rather than triggering it from the Page_Load.
    >
    > Thanks again Phillip for your help, the example you sent will be useful for
    > some other things I'm working on.
    >
    > Cheers,
    > Anye
    Phillip Williams, Dec 21, 2005
    #7
    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. Phillip Vong
    Replies:
    0
    Views:
    402
    Phillip Vong
    Jul 27, 2006
  2. Iain
    Replies:
    3
    Views:
    912
  3. Nathan Sokalski
    Replies:
    0
    Views:
    592
    Nathan Sokalski
    Oct 5, 2008
  4. Jorge
    Replies:
    1
    Views:
    159
    Mike Moore [MSFT]
    Nov 13, 2003
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    278
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page