Hi Timm,
I have placed the same code inside this sample of a DetailsView from the
Quickstart tutorials
http://66.129.71.130/QuickStartv20/u...lsEdit_cs.aspx
and it still worked. Can you post the code the you have not working?
Here it is the complete code that I tried:
<b>Choose a state:</b>
<asp:DropDownList ID="DropDownList1" DataSourceID="SqlDataSource2"
AutoPostBack="true"
DataTextField="state" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedInde xChanged" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
SelectCommand="SELECT DISTINCT [state] FROM [authors]"
ConnectionString="<%$ ConnectionStrings:Pubs %>" />
<br />
<br />
<table>
<tr>
<td valign="top">
<asp:GridView ID="GridView1" AllowSorting="True"
AllowPaging="True" runat="server"
DataSourceID="SqlDataSource1" DataKeyNames="au_id"
AutoGenerateColumns="False" Width="427px"
OnSelectedIndexChanged="GridView1_SelectedIndexCha nged"
OnSorted="GridView1_Sorted" OnPageIndexChanged="GridView1_PageIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="au_id" HeaderText="au_id"
ReadOnly="True" SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname"
SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname"
SortExpression="au_fname" />
<asp:BoundField DataField="state" HeaderText="state"
SortExpression="state" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]
WHERE ([state] = @state)"
ConnectionString="<%$ ConnectionStrings:Pubs %>">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="state"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</td>
<td valign="top">
<asp:DetailsView AutoGenerateRows="False" DataKeyNames="au_id"
OnItemUpdated="DetailsView1_ItemUpdated" DataSourceID="SqlDataSource3"
HeaderText="Author Details" ID="DetailsView1" runat="server"
Width="275px">
<Fields>
<asp:BoundField DataField="au_id" HeaderText="au_id"
ReadOnly="True" SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname"
SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname"
SortExpression="au_fname" />
<asp:BoundField DataField="phone" HeaderText="phone"
SortExpression="phone" />
<asp:BoundField DataField="address" HeaderText="address"
SortExpression="address" />
<asp:BoundField DataField="city" HeaderText="city"
SortExpression="city" />
<asp:BoundField DataField="state" HeaderText="state"
SortExpression="state" />
<asp:BoundField DataField="zip" HeaderText="zip"
SortExpression="zip" />
<asp:TemplateField >
<ItemTemplate>
<asp:Label runat=server ID="lblRegion" Text='<%#
Eval("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<table>
<tr>
<td>Region</td>
<td>
<asp:DropDownList ID="ddlRegions" runat=server
DataSourceID ="SqlRegions" DataTextField="RegionDescription"
DataValueField="RegionID"
AutoPostBack="True"></asp:DropDownList>
<asp:SqlDataSource ID="SqlRegions" runat="server"
ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT RegionID,
RegionDescription FROM Region">
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>Territory</td>
<td>
<asp:DropDownList ID="ddlTerritories2"
runat=server DataSourceID ="SqlTerritories"
DataTextField="TerritoryDescription"
DataValueField="TerritoryID"></asp:DropDownList>
<asp:SqlDataSource ID="SqlTerritories"
runat="server"
ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT TerritoryID,
TerritoryDescription FROM Territories WHERE (RegionID = @RegionID)">
<SelectParameters>
<asp:ControlParameter Name="RegionID"
ControlID="ddlRegions" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="contract" HeaderText="contract"
SortExpression="contract" />
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:Pubs %>"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone],
[address], [city], [state], [zip], [contract] FROM [authors] WHERE ([au_id] =
@au_id)"
UpdateCommand="UPDATE [authors] SET [au_lname] = @au_lname,
[au_fname] = @au_fname, [phone] = @phone, [address] = @address, [city] =
@city, [state] = @state, [zip] = @zip, [contract] = @contract WHERE [au_id] =
@original_au_id" DeleteCommand="DELETE FROM [authors] WHERE [au_id] =
@original_au_id">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="au_id"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="au_lname" Type="String" />
<asp:Parameter Name="au_fname" Type="String" />
<asp:Parameter Name="phone" Type="String" />
<asp:Parameter Name="address" Type="String" />
<asp:Parameter Name="city" Type="String" />
<asp:Parameter Name="state" Type="String" />
<asp:Parameter Name="zip" Type="String" />
<asp:Parameter Name="contract" Type="Boolean" />
<asp:Parameter Name="original_au_id" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
--
HTH,
Phillip Williams
http://www.societopia.net http://www.webswapp.com
"Timm" wrote:
But how can the ControlID of the SelectParameter refer to a DropDownList in a
TemplateField of the DetailsView?
"Phillip Williams" wrote:
I have tried the following using SqlDataSource against the NorthWind database
and it worked fine:
<table>
<tr>
<td>Region</td>
<td>
<asp:DropDownList ID="ddlRegions" runat=server DataSourceID
="SqlRegions" DataTextField="RegionDescription"
DataValueField="RegionID" AutoPostBack="True"></asp:DropDownList>
<asp:SqlDataSource ID="SqlRegions" runat="server"
ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT RegionID, RegionDescription FROM
Region">
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>Territory</td>
<td>
<asp:DropDownList ID="ddlTerritories2" runat=server DataSourceID
="SqlTerritories" DataTextField="TerritoryDescription"
DataValueField="TerritoryID"></asp:DropDownList>
<asp:SqlDataSource ID="SqlTerritories" runat="server"
ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT TerritoryID, TerritoryDescription FROM
Territories WHERE (RegionID = @RegionID)">
<SelectParameters>
<asp:ControlParameter Name="RegionID" ControlID="ddlRegions"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Timm" wrote:
Thanks Phillip, but I have tried something similar already and couldn't get
it to work, perhaps because my data sources are SqlDataSource (it might have
been helpful if I'd mentioned that in the first place!). Also note, DDL1 and
DDL2 are controls in TemplateFields within a DetailsView.
"Phillip Williams" wrote:
> Correction: Look in the Binding to a Visual Studio DataSet (instead of the
> Data Access layer) in the Quick Start tutorials that I gave its link.
>
> "Phillip Williams" wrote:
>
> > Yes you can. I just tested it on my PC. If you use Binding to DataAccess
> > Layer http://66.129.71.130/QuickStartv20/a...jects.aspx#dal
> >
> > Then you would have a parameterized query for selecting the items of the
> > second dropdown list. Something like this:
> >
> > <!-- this the first dropdown list --->
> > <asp:DropDownList ID="DropDownList1" runat="server"
> > DataSourceID="ObjectDataSource1" DataTextField="CategoryName"
> > DataValueField="CategoryID" AutoPostBack="True">
> > </asp:DropDownList>
> > <!-- this is the dependent dropdown list --->
> > <asp:DropDownList ID="DropDownList2" runat="server"
> > DataSourceID="ObjectDataSource2" DataTextField="Description"
> > DataValueField="CategoryID" AutoPostBack="True">
> > </asp:DropDownList>
> > <!-- this is the objectDataSource for the first dropdownlist --->
> > <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
> > TypeName="DataSetTableAdapters.CategoriesTableAdap ter"
> > SelectMethod="getMyFirstList"></asp:ObjectDataSource>
> > <!-- this is the objectDataSource for the second dropdownlist. It receives
> > a parameter from the first list --->
> > <asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
> > TypeName="DataSetTableAdapters.CategoriesTableAdap ter"
> > SelectMethod="getMySecondList">
> > <SelectParameters>
> > <asp:ControlParameter ControlID="DropDownList1" Name="CategoryID"
> > PropertyName="SelectedValue"
> > Type="String" />
> > </SelectParameters>
> > </asp:ObjectDataSource>
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Timm" wrote:
> >
> > > I have an ASP.NET 2.0 page with two DropDownLists. I am using declarative
> > > data binding wherever possible and trying to minimize the use of code. The
> > > list of values in DropDownList DDL2 should be (filtered) dependent upon the
> > > selection in DDL1. I think this inevitably needs some code, but I'd be happy
> > > to be told otherwise!
> > > I have some code to handle OnSelectedIndexChanged for DDL1 that sets the
> > > FilterExpression associated with the DataSourceID of DDL2. My problem is that
> > > after the code executes, I get an InvalidOperationException error that says
> > > "Databinding methods such as Eval(), XPath(), and Bind() can only be used in
> > > the context of a databound control."
> > > How can I avoid this?