Hi Timm,
Here is how you make it work:
1- create 2 separate SqlDataSource objects; one for insert and one for edit,
e.g. instead of SqlProductType, place SqlProductTypeI and SqlProductTypeE
2- place the SqlDataSource objects within their respective templates, i.e.
the SqlProductTypeI within the Insert template and the SqlProductTypeE within
the Edit template
3- adjust the references accordingly
Here is my modification of your code that worked on my PC (I had to create
test tables to work with your data)
<asp:DetailsView ID="View1" runat="Server" DataSourceID="Sql1"
DataKeyNames="ID"
DefaultMode="ReadOnly" AutoGenerateRows="false"
EmptyDataText=" No record found "
AutoGenerateInsertButton="true" AutoGenerateEditButton="true">
<Fields>
<asp:BoundField DataField="ID" HeaderText="Ref."
SortExpression="ID" InsertVisible="false"
ReadOnly="true" />
<asp:TemplateField HeaderText="Product Ref.">
<InsertItemTemplate>
<asp:DropDownList ID="DDL1I" runat="server"
DataSourceID="SqlProductTypeI" DataTextField="ProductRef"
DataValueField="ProductRef"
AutoPostBack="true"
AppendDataBoundItems="true" />
<asp:SqlDataSource runat="server"
ID="SqlProductTypeI" SelectCommand="SELECT NULL AS [ProductRef] UNION SELECT
[ProductRef]
FROM [ProductType] Order by [ProductRef]"
ConnectionString="<%$ ConnectionStrings:Test %>">
</asp:SqlDataSource>
</InsertItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDL1E" runat="server"
DataSourceID="SqlProductTypeE" DataTextField="ProductRef"
DataValueField="ProductRef"
AutoPostBack="true"
AppendDataBoundItems="true" />
<asp:SqlDataSource runat="server"
ID="SqlProductTypeE" SelectCommand="SELECT NULL AS [ProductRef] UNION SELECT
[ProductRef]
FROM [ProductType] Order by [ProductRef]"
ConnectionString="<%$ ConnectionStrings:Test %>">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ProductRef" runat="Server"
Text='<%# Bind("ProductRef") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dependent Type">
<InsertItemTemplate>
<asp:DropDownList ID="DDL2I" runat="server"
DataSourceID="SqlDependentTypeI" DataTextField="ID"
DataValueField="ID" />
<asp:SqlDataSource runat="server"
ID="SqlDependentTypeI" SelectCommand="SELECT NULL AS [ProductRef], '' AS [ID]
UNION SELECT
[ProductRef], [ID] FROM [DependentType]
WHERE [ProductRef]=@ProductRef" ConnectionString="<%$ ConnectionStrings:Test
%>">
<SelectParameters>
<asp:ControlParameter ControlID="DDL1I"
Name="ProductRef" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</InsertItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDL2E" runat="server"
DataSourceID="SqlDependentTypeE" DataTextField="ID"
DataValueField="ID" />
<asp:SqlDataSource runat="server"
ID="SqlDependentTypeE" SelectCommand="SELECT NULL AS [ProductRef], '' AS [ID]
UNION SELECT
[ProductRef], [ID] FROM [DependentType]
WHERE [ProductRef]=@ProductRef" ConnectionString="<%$ ConnectionStrings:Test
%>">
<SelectParameters>
<asp:ControlParameter ControlID="DDL1E"
Name="ProductRef" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="DependentType" runat="Server"
Text='<%# Bind("DependentType") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DependentDetails"
HeaderText="Dependent Details" SortExpression="DependentDetails" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource runat="server" ID="Sql1"
SelectCommand="SELECT [ID], [ProductRef], [DependentType],
[DependentDetails] FROM [MainTable] ORDER BY [ID] DESC"
UpdateCommand="UPDATE [MainTable] SET [ProductRef]=@ProductRef,
[DependentType]=@DependentType, [DependentDetails]=@DependentDetails WHERE
[ID] = @Original_ID"
InsertCommand="INSERT INTO [MainTable] ([ProductRef],
[DependentType], [DependentDetails]) SELECT @ProductRef, @DependentType,
@DependentDetails"
ConnectionString="<%$ ConnectionStrings:Test %>"
OldValuesParameterFormatString="Original_{0}">
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="ProductRef"
ConvertEmptyStringToNull="true" />
<asp:Parameter Name="DependentType"
ConvertEmptyStringToNull="true" />
<asp:Parameter Name="DependentDetails"
ConvertEmptyStringToNull="true" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductRef"
ConvertEmptyStringToNull="true" />
<asp:Parameter Name="DependentType"
ConvertEmptyStringToNull="true" />
<asp:Parameter Name="DependentDetails"
ConvertEmptyStringToNull="true" />
</InsertParameters>
</asp:SqlDataSource>
--
HTH,
Phillip Williams
http://www.societopia.net http://www.webswapp.com
"Timm" wrote:
[color=blue]
> Many thanks again for your time and assistance Phillip. Please find below a
> simplified version of my page, as requested. Your recommendation for a
> ControlParameter based solution would be better than this one (based on
> SelectedIndexChanged), but I still can't find a way to reference DDL1I or
> DDL1E in the ControlParameter.
>
> <%@ Page Language="VB" AutoEventWireup="false" %>
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head id="Head1" runat="server"><title>Dependent DropDownLists</title></head>
> <script runat="server" >
> Protected Sub ProductRef_SelectedIndexChanged(ByVal sender As Object,
> ByVal e As System.EventArgs)
> SqlProductType.FilterExpression = "[ProductRef] IS NULL OR
> [ProductRef]='" & sender.SelectedValue.ToString & "'"
> End Sub
> </script>
> <body>
> <form id="form1" runat="server">
> <asp:DetailsView ID="View1" Runat="Server" DataSourceID="Sql1"
> DataKeyNames="ID" DefaultMode="ReadOnly" AutoGenerateRows="false"
> EmptyDataText=" No record found " AutoGenerateInsertButton="true"
> AutoGenerateEditButton="true" >
> <Fields>
> <asp:BoundField DataField="ID" HeaderText="Ref."
> SortExpression="ID" InsertVisible="false" ReadOnly="true" />
> <asp:TemplateField HeaderText="Product Ref." >
> <InsertItemTemplate>
> <asp:DropDownList ID="DDL1I" Runat="server"
> DataSourceID="SqlProductType" DataTextField="ProductRef"
> DataValueField="ProductRef" SelectedValue='<%# Bind("ProductRef") %>'
> OnSelectedIndexChanged="ProductRef_SelectedIndexCh anged"
> AutoPostBack="true" AppendDataBoundItems="true" />
> </InsertItemTemplate>
> <EditItemTemplate>
> <asp:DropDownList ID="DDL1E" Runat="server"
> DataSourceID="SqlProductType" DataTextField="ProductRef"
> DataValueField="ProductRef" SelectedValue='<%# Bind("ProductRef") %>'
> OnSelectedIndexChanged="ProductRef_SelectedIndexCh anged"
> AutoPostBack="true" AppendDataBoundItems="true" />
> </EditItemTemplate>
> <ItemTemplate>
> <asp:Label ID="ProductRef" Runat="Server" Text='<%# Bind("ProductRef")
> %>' />
> </ItemTemplate>
> </asp:TemplateField>
> <asp:TemplateField HeaderText="Dependent Type" >
> <InsertItemTemplate>
> <asp:DropDownList ID="DDL2I" Runat="server"
> DataSourceID="SqlDependentType" DataTextField="ID" DataValueField="ID"
> SelectedValue='<%# Bind("DependentType") %>' />
> </InsertItemTemplate>
> <EditItemTemplate>
> <asp:DropDownList ID="DDL2E" Runat="server"
> DataSourceID="SqlDependentType" DataTextField="ID" DataValueField="ID"
> SelectedValue='<%# Bind("DependentType") %>' />
> </EditItemTemplate>
> <ItemTemplate>
> <asp:Label ID="DependentType" Runat="Server" Text='<%#
> Bind("DependentType") %>' />
> </ItemTemplate>
> </asp:TemplateField>
> <asp:BoundField DataField="DependentDetails"
> HeaderText="Dependent Details" SortExpression="DependentDetails" />
> </Fields>
> </asp:DetailsView>
> <asp:SqlDataSource Runat="server" ID="Sql1"
> SelectCommand="SELECT [ID], [ProductRef], [DependentType],
> [DependentDetails] FROM [MainTable] ORDER BY [ID] DESC"
> UpdateCommand="UPDATE [MainTable] SET [ProductRef]=@ProductRef,
> [DependentType]=@DependentType, [DependentDetails]=@DependentDetails WHERE
> [ID] = @Original_ID"
> InsertCommand="INSERT INTO [MainTable] ([ProductRef],
> [DependentType], [DependentDetails]) SELECT @ProductRef, @DependentType,
> @DependentDetails"
> ConnectionString="<%$ ConnectionStrings:Test %>"
> OldValuesParameterFormatString="Original_{0}" >
> <UpdateParameters>
> <asp:Parameter Name="ID" Type="Int32" />
> <asp:Parameter Name="ProductRef" ConvertEmptyStringToNull="true"
> />
> <asp:Parameter Name="DependentType"
> ConvertEmptyStringToNull="true" />
> <asp:Parameter Name="DependentDetails"
> ConvertEmptyStringToNull="true" />
> </UpdateParameters>
> <InsertParameters>
> <asp:Parameter Name="ProductRef" ConvertEmptyStringToNull="true"
> />
> <asp:Parameter Name="DependentType"
> ConvertEmptyStringToNull="true" />
> <asp:Parameter Name="DependentDetails"
> ConvertEmptyStringToNull="true" />
> </InsertParameters>
> </asp:SqlDataSource>
> <asp:SqlDataSource Runat="server" ID="SqlProductType"
> SelectCommand="SELECT NULL AS [ProductRef] UNION SELECT [ProductRef]
> FROM [ProductType] ORDER BY [ProductRef]"
> ConnectionString="<%$ ConnectionStrings:Test %>">
> </asp:SqlDataSource>
> <asp:SqlDataSource Runat="server" ID="SqlDependentType"
> SelectCommand="SELECT NULL AS [ProductRef], '' AS [ID] UNION SELECT
> [ProductRef], [ID] FROM [DependentType] ORDER BY [ProductRef]"
> ConnectionString="<%$ ConnectionStrings:Test %>">
> </asp:SqlDataSource>
> </form>
> </body>
> </html>
>
>
> "Phillip Williams" wrote:
>[color=green]
> > 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:
> >[color=darkred]
> > > 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>[/color][/color][/color]