472,809 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 software developers and data experts.

use GridView boundfield value for DropDownList select where clause

I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to
an SqlDataSource, then have a value from a boundfield in the row be passed as
the keyfield for select where clause. Im trying to load the ddl with a list
of dates from another table keyed on GridView row field that only apply to
this row.

Any suggestions would be appreciated.

Apr 3 '06 #1
6 8188
I have done it in this demo using an ObjectDataSource; where one of the
properties in the databound business class is a collection of the list items
specific to each row.

http://www.webswapp.com/codesamples/...w/default.aspx

If you want to do it using SQLDataSource, you will have to either intervene
during the RowUpdated event to trigger another SQL query that returns a list
of item that you bind to the dropdownlist or you can add the field that
determines the listitems selection process in the GridView. DataKeyNames then
set a SqlDataSource within the ItemTemplate with a parameter referencing that
datakey, e.g.

<asp:ControlParameter ControlID="GridView1" Name="CategoryID"
PropertyName="SelectedDataKey.Values[1]" Type="Int32" />

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Dabbler" wrote:
I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to
an SqlDataSource, then have a value from a boundfield in the row be passed as
the keyfield for select where clause. Im trying to load the ddl with a list
of dates from another table keyed on GridView row field that only apply to
this row.

Any suggestions would be appreciated.

Apr 3 '06 #2
Thanks Phillip, I studied the example with ObjectDataSource but was hoping I
could implement this just using SqlDataSource.

I tried your selectedkeys.values[1] but still don't get any data in the ddl.
Here's my code:
<asp:GridView ID="RegistrantsGridView" runat="server"
DataSourceID="RegistrantsSqlDataSource"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
CssClass="waGridView"
DataKeyNames="RegistrantId" ... >
<Columns>
<asp:BoundField DataField="RegistrantId" HeaderText="Id"
InsertVisible="False" ReadOnly="True" SortExpression="RegistrantId" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="RideDates" runat="server"
DataSourceId="RideDatesSqlDataSource">
</asp:DropDownList>
<asp:SqlDataSource ID="RideDatesSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:myDB %>"
ProviderName="System.Data.SqlClient"
SelectCommand="GetRegRideDates" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="RegistrantsGridView"
Name="RegistrantId"
PropertyName="SelectedDataKey.Values[1]" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</TemplateField>

"Phillip Williams" wrote:
I have done it in this demo using an ObjectDataSource; where one of the
properties in the databound business class is a collection of the list items
specific to each row.

http://www.webswapp.com/codesamples/...w/default.aspx

If you want to do it using SQLDataSource, you will have to either intervene
during the RowUpdated event to trigger another SQL query that returns a list
of item that you bind to the dropdownlist or you can add the field that
determines the listitems selection process in the GridView. DataKeyNames then
set a SqlDataSource within the ItemTemplate with a parameter referencing that
datakey, e.g.

<asp:ControlParameter ControlID="GridView1" Name="CategoryID"
PropertyName="SelectedDataKey.Values[1]" Type="Int32" />

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Dabbler" wrote:
I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to
an SqlDataSource, then have a value from a boundfield in the row be passed as
the keyfield for select where clause. Im trying to load the ddl with a list
of dates from another table keyed on GridView row field that only apply to
this row.

Any suggestions would be appreciated.

Apr 3 '06 #3
You are right. I just tried it and it does not work. I forgot that while the
GridViewRow is being created there are no selected rows yet to have values in
the SelectedDataKey.

However I tried this modification and it worked:

<asp:TemplateField>
<ItemTemplate>
<asp:Label id="lblRegistrantId" runat="Server"
Text='<%#Bind("RegistrantId")%' cssClass="hidden"></asp:Label>
<asp:DropDownList ID="RideDates" runat="server"
DataSourceId="RideDatesSqlDataSource">
</asp:DropDownList>
<asp:SqlDataSource ID="RideDatesSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:myDB %>"
ProviderName="System.Data.SqlClient"
SelectCommand="GetRegRideDates" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="lblRegistrantId"
Name="RegistrantId"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</TemplateField>
where the style class definition would be:
..hidde {display:none;}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Dabbler" wrote:
Thanks Phillip, I studied the example with ObjectDataSource but was hoping I
could implement this just using SqlDataSource.

I tried your selectedkeys.values[1] but still don't get any data in the ddl.
Here's my code:
<asp:GridView ID="RegistrantsGridView" runat="server"
DataSourceID="RegistrantsSqlDataSource"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
CssClass="waGridView"
DataKeyNames="RegistrantId" ... >
<Columns>
<asp:BoundField DataField="RegistrantId" HeaderText="Id"
InsertVisible="False" ReadOnly="True" SortExpression="RegistrantId" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="RideDates" runat="server"
DataSourceId="RideDatesSqlDataSource">
</asp:DropDownList>
<asp:SqlDataSource ID="RideDatesSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:myDB %>"
ProviderName="System.Data.SqlClient"
SelectCommand="GetRegRideDates" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="RegistrantsGridView"
Name="RegistrantId"
PropertyName="SelectedDataKey.Values[1]" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</TemplateField>

"Phillip Williams" wrote:
I have done it in this demo using an ObjectDataSource; where one of the
properties in the databound business class is a collection of the list items
specific to each row.

http://www.webswapp.com/codesamples/...w/default.aspx

If you want to do it using SQLDataSource, you will have to either intervene
during the RowUpdated event to trigger another SQL query that returns a list
of item that you bind to the dropdownlist or you can add the field that
determines the listitems selection process in the GridView. DataKeyNames then
set a SqlDataSource within the ItemTemplate with a parameter referencing that
datakey, e.g.

<asp:ControlParameter ControlID="GridView1" Name="CategoryID"
PropertyName="SelectedDataKey.Values[1]" Type="Int32" />

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Dabbler" wrote:
I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to
an SqlDataSource, then have a value from a boundfield in the row be passed as
the keyfield for select where clause. Im trying to load the ddl with a list
of dates from another table keyed on GridView row field that only apply to
this row.

Any suggestions would be appreciated.

Apr 3 '06 #4

Ok, I got it to work with your suggestion to load the ddl in code instead of
using SqlDataSource. I used RowDataBound event, so I could pull the key value
from a cell using e.Row.Cells[1].Text. I think the RowUpdatedEvent doesn't
trigger while the Gridview is loading, which is what i needed to load the
initial ddl values.

Thanks for the tip!

"Phillip Williams" wrote:
I have done it in this demo using an ObjectDataSource; where one of the
properties in the databound business class is a collection of the list items
specific to each row.

http://www.webswapp.com/codesamples/...w/default.aspx

If you want to do it using SQLDataSource, you will have to either intervene
during the RowUpdated event to trigger another SQL query that returns a list
of item that you bind to the dropdownlist or you can add the field that
determines the listitems selection process in the GridView. DataKeyNames then
set a SqlDataSource within the ItemTemplate with a parameter referencing that
datakey, e.g.

<asp:ControlParameter ControlID="GridView1" Name="CategoryID"
PropertyName="SelectedDataKey.Values[1]" Type="Int32" />

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Dabbler" wrote:
I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to
an SqlDataSource, then have a value from a boundfield in the row be passed as
the keyfield for select where clause. Im trying to load the ddl with a list
of dates from another table keyed on GridView row field that only apply to
this row.

Any suggestions would be appreciated.

Apr 3 '06 #5
Interesting that this worked for you. The only difference between this and my
last failed attempt using SqlDataSource is that you are using a label control
in an item template, whereas I was using the RegistrantId in a visible
BoundField. I'm guessing that the BoundFields have mangeled Id names instead
of the original Id names.

"Phillip Williams" wrote:
You are right. I just tried it and it does not work. I forgot that while the
GridViewRow is being created there are no selected rows yet to have values in
the SelectedDataKey.

However I tried this modification and it worked:

<asp:TemplateField>
<ItemTemplate>
<asp:Label id="lblRegistrantId" runat="Server"
Text='<%#Bind("RegistrantId")%' cssClass="hidden"></asp:Label>
<asp:DropDownList ID="RideDates" runat="server"
DataSourceId="RideDatesSqlDataSource">
</asp:DropDownList>
<asp:SqlDataSource ID="RideDatesSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:myDB %>"
ProviderName="System.Data.SqlClient"
SelectCommand="GetRegRideDates" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="lblRegistrantId"
Name="RegistrantId"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</TemplateField>
where the style class definition would be:
.hidde {display:none;}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Dabbler" wrote:
Thanks Phillip, I studied the example with ObjectDataSource but was hoping I
could implement this just using SqlDataSource.

I tried your selectedkeys.values[1] but still don't get any data in the ddl.
Here's my code:
<asp:GridView ID="RegistrantsGridView" runat="server"
DataSourceID="RegistrantsSqlDataSource"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
CssClass="waGridView"
DataKeyNames="RegistrantId" ... >
<Columns>
<asp:BoundField DataField="RegistrantId" HeaderText="Id"
InsertVisible="False" ReadOnly="True" SortExpression="RegistrantId" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="RideDates" runat="server"
DataSourceId="RideDatesSqlDataSource">
</asp:DropDownList>
<asp:SqlDataSource ID="RideDatesSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:myDB %>"
ProviderName="System.Data.SqlClient"
SelectCommand="GetRegRideDates" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="RegistrantsGridView"
Name="RegistrantId"
PropertyName="SelectedDataKey.Values[1]" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</TemplateField>

"Phillip Williams" wrote:
I have done it in this demo using an ObjectDataSource; where one of the
properties in the databound business class is a collection of the list items
specific to each row.

http://www.webswapp.com/codesamples/...w/default.aspx

If you want to do it using SQLDataSource, you will have to either intervene
during the RowUpdated event to trigger another SQL query that returns a list
of item that you bind to the dropdownlist or you can add the field that
determines the listitems selection process in the GridView. DataKeyNames then
set a SqlDataSource within the ItemTemplate with a parameter referencing that
datakey, e.g.

<asp:ControlParameter ControlID="GridView1" Name="CategoryID"
PropertyName="SelectedDataKey.Values[1]" Type="Int32" />

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

> I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to
> an SqlDataSource, then have a value from a boundfield in the row be passed as
> the keyfield for select where clause. Im trying to load the ddl with a list
> of dates from another table keyed on GridView row field that only apply to
> this row.
>
> Any suggestions would be appreciated.
>

Apr 3 '06 #6
Yes, DataBound fields do not have an ID property. If you right-mouse click
on the browser to view the rendered HTML you will see that the bound field
rendered the content of your the databound field within a TableCell.
Therefore you could not refer to it. However by specifying a templateField
in which I added a server control (the label) containing the RegistrantId, I
managed to refer to that control by its ID.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Dabbler" wrote:
Interesting that this worked for you. The only difference between this and my
last failed attempt using SqlDataSource is that you are using a label control
in an item template, whereas I was using the RegistrantId in a visible
BoundField. I'm guessing that the BoundFields have mangeled Id names instead
of the original Id names.

"Phillip Williams" wrote:
You are right. I just tried it and it does not work. I forgot that while the
GridViewRow is being created there are no selected rows yet to have values in
the SelectedDataKey.

However I tried this modification and it worked:

<asp:TemplateField>
<ItemTemplate>
<asp:Label id="lblRegistrantId" runat="Server"
Text='<%#Bind("RegistrantId")%' cssClass="hidden"></asp:Label>
<asp:DropDownList ID="RideDates" runat="server"
DataSourceId="RideDatesSqlDataSource">
</asp:DropDownList>
<asp:SqlDataSource ID="RideDatesSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:myDB %>"
ProviderName="System.Data.SqlClient"
SelectCommand="GetRegRideDates" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="lblRegistrantId"
Name="RegistrantId"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</TemplateField>
where the style class definition would be:
.hidde {display:none;}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Dabbler" wrote:
Thanks Phillip, I studied the example with ObjectDataSource but was hoping I
could implement this just using SqlDataSource.

I tried your selectedkeys.values[1] but still don't get any data in the ddl.
Here's my code:
<asp:GridView ID="RegistrantsGridView" runat="server"
DataSourceID="RegistrantsSqlDataSource"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
CssClass="waGridView"
DataKeyNames="RegistrantId" ... >
<Columns>
<asp:BoundField DataField="RegistrantId" HeaderText="Id"
InsertVisible="False" ReadOnly="True" SortExpression="RegistrantId" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="RideDates" runat="server"
DataSourceId="RideDatesSqlDataSource">
</asp:DropDownList>
<asp:SqlDataSource ID="RideDatesSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:myDB %>"
ProviderName="System.Data.SqlClient"
SelectCommand="GetRegRideDates" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="RegistrantsGridView"
Name="RegistrantId"
PropertyName="SelectedDataKey.Values[1]" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</TemplateField>

"Phillip Williams" wrote:

> I have done it in this demo using an ObjectDataSource; where one of the
> properties in the databound business class is a collection of the list items
> specific to each row.
>
> http://www.webswapp.com/codesamples/...w/default.aspx
>
> If you want to do it using SQLDataSource, you will have to either intervene
> during the RowUpdated event to trigger another SQL query that returns a list
> of item that you bind to the dropdownlist or you can add the field that
> determines the listitems selection process in the GridView. DataKeyNames then
> set a SqlDataSource within the ItemTemplate with a parameter referencing that
> datakey, e.g.
>
> <asp:ControlParameter ControlID="GridView1" Name="CategoryID"
> PropertyName="SelectedDataKey.Values[1]" Type="Int32" />
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Dabbler" wrote:
>
> > I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to
> > an SqlDataSource, then have a value from a boundfield in the row be passed as
> > the keyfield for select where clause. Im trying to load the ddl with a list
> > of dates from another table keyed on GridView row field that only apply to
> > this row.
> >
> > Any suggestions would be appreciated.
> >

Apr 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
1
by: krian | last post by:
Hi, How can I change the styles of the DropDownList(Select box)..? and also I want to place an image in the place of DropDownButton of Select Box .. How can i done these in DHTML..?. I would...
8
by: Jean | last post by:
Hello all, I have the following data, that was queried and sorted to columns PROBLEM_ID and then by STATUSDATE (ascending): STATUS_ID STATUSDATE PROBLEM_ID --------- ---------- ...
2
by: Lone Wolf | last post by:
We have a recordset with the following structure id (int), mls (varchar) What we need to do is within a single sql call we need to show all records where the mls = '' and all unique records...
0
by: R.A.M. | last post by:
Hello, Could you help me please to define GridView associated to DropDownList. My DropDownList "Publishers" should show publishers' names from SQL Server table Publishers. My GridView...
2
by: cephelo | last post by:
I have no problems outputting the attribute value when the node is in context, for example, @id when an <status> node is in context. However, I am having trouble outputting it in a <xsl:value-of...
5
by: No bother | last post by:
I am using 5.0.26-NT on Windows 2000. I have need to use a reference in the outer from clause in a subquery in the select clause. Consider the following example: Select (select b.baitID from...
5
by: paragpdoke | last post by:
Hello Everyone. I'm new to XSL and couldn't get something to work. Sample XML excerpt: <root> <ObjectTemplate> <NodeType1></NodeType1> <NodeType2></NodeType2> </ObjectTemplate> <Object>
1
by: loix | last post by:
I'm trying to Insert 300.000 rows (or thereabouts) into a table (via QMF) using the following Select clause: # Insert into table_name ( Select row1, row2, row..x from table_name2 ...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.