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

A

anyeone

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!
 
P

Phillip Williams

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
 
A

anyeone

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 said:
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 said:
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!
 
P

Phillip Williams

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 said:
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 said:
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 said:
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!
 
A

anyeone

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!
 
A

anyeone

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
 
P

Phillip Williams

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top