Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Web Controls (http://www.velocityreviews.com/forums/f63-asp-net-web-controls.html)
-   -   Bound dropdownlist in .NET 2.0 -- picking the selected value? (http://www.velocityreviews.com/forums/t777448-bound-dropdownlist-in-net-2-0-picking-the-selected-value.html)

anyeone 12-19-2005 10:35 PM

Bound dropdownlist in .NET 2.0 -- picking the selected value?
 
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 12-19-2005 11:29 PM

RE: Bound dropdownlist in .NET 2.0 -- picking the selected value?
 
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/...idView_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 12-19-2005 11:43 PM

RE: Bound dropdownlist in .NET 2.0 -- picking the selected value?
 
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.FindByValu e(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/...idView_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 12-19-2005 11:54 PM

RE: Bound dropdownlist in .NET 2.0 -- picking the selected value?
 
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/...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.FindByValu e(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/...idView_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 12-20-2005 12:52 AM

RE: Bound dropdownlist in .NET 2.0 -- picking the selected value?
 
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/...FormView1.aspx
>
> If that example does not answer your question, please do try another time.
>


anyeone 12-20-2005 03:41 PM

RE: Bound dropdownlist in .NET 2.0 -- picking the selected value?
 
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 12-21-2005 04:25 PM

RE: Bound dropdownlist in .NET 2.0 -- picking the selected value?
 
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



All times are GMT. The time now is 10:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.