Home All Groups Group Topic Archive Search About

InsertItemTemplate dependant dropdown list problem

Author
6 Feb 2006 2:27 PM
dhizzy
am trying to create a drop down list that is dependent on another
dropdown list value inside a formview control.


I have seen this site
http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx



but I am unable to get it working.  The only things that I have
different is that the displayed text in both list is a text value and
the value is an integer and also I am using the insertitemtemplate.  I
have tried debugging

Here is a code snippet:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ServerID"
         DataSourceID="DS_Servers"
OnItemUpdating="FormView1_ItemUpdating">
         <InsertItemTemplate>
         <asp:SqlDataSource ID="DS_Rack" runat="server"
                  ConnectionString="<%$
ConnectionStrings:ConnectionString1 %>"
                  SelectCommand="SELECT [RackID], [Rack] FROM
                  [cRack]"></asp:SqlDataSource>
        <asp:SqlDataSource ID="DS_Cabinet" runat="server"
                  ConnectionString="<%$
ConnectionStrings:ConnectionString1 %>"
                  SelectCommand="SELECT [CabinetID], [Cabinet] FROM
                  [cCabinet] WHERE ([RackID] = @RackID)">
                  <SelectParameters>
                        <asp:ControlParameter ControlID="ddRack"
Name="RackID"
                         PropertyName="SelectedValue" Type="Int32" />
                  </SelectParameters>
        </asp:SqlDataSource>
        <table>
            <tr align="left">
                <td>Rack Location</td>
                <td>
                    <asp:DropDownList ID="ddRack" runat="server"
                            AutoPostBack="True" SelectedValue='<%#
Bind("RackID") %>'
                            DataSourceID="DS_Rack" DataTextField="Rack"

                            DataValueField="RackID"></asp:DropDownList>

                            -
                            <asp:DropDownList ID="ddCabinet"
runat="server"
                             SelectedValue='<%# Eval("CabinetID") %>'
DataSourceID="DS_Cabinet"
                             DataTextField="Cabinet"
DataValueField="CabinetID"
                             OnDataBound="ddCabinet_DataBound">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:LinkButton ID="InsertButton" runat="server"
                            CausesValidation="True"
CommandName="Insert"
                            Text="Insert"></asp:LinkButton>
                    <asp:LinkButton ID="InsertCancelButton"
                            runat="server" CausesValidation="False"
CommandName="Cancel"
                            Text="Cancel"></asp:LinkButton>
                </td>
            </tr>
        </table>
    </InsertItemTemplate>
</asp:FormView>


    Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal
e As FormViewUpdateEventArgs)
        Dim Rack As Integer = CType(CType(sender,
FormView).FindControl("ddCabinet"), DropDownList).SelectedIndex
        e.NewValues("Rack") = Rack
        e.Cancel = False
    End Sub
    Protected Sub ddCabinet_DataBound(ByVal sender As Object, ByVal e
As EventArgs)
        Dim ddl As DropDownList = CType(sender, DropDownList)
        Dim frmV As FormView = CType(ddl.NamingContainer, FormView)
        If Not frmV.DataItem Is Nothing Then
            Dim RackID As Int32 = CType(frmV.DataItem,
Data.DataRowView)("Rack")
            ddl.ClearSelection()
            Dim li As ListItem = ddl.Items.FindByValue(RackID)
            If Not li Is Nothing Then li.Selected = True
        End If
    End Sub


Any help would be greatly appreciated.

Author
6 Feb 2006 3:02 PM
dhizzy
The error that I get is

'ddCabinet' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value
Author
6 Feb 2006 3:19 PM
Phillip Williams
If I understand your code correctly, then you are attempting to set the
Cabinet dropdownlist based on the selection of the Rack dropdownlist.  I
think your code has a lot of errors though:

1-  In the FormView1_ItemUpdating you took the value for the
ddCabinet.SelectedIndex and saved it in the Rack?!  You need to correct that.

2-  Remove the SelectedValue='<%# Eval("CabinetID") %>' because you should
be doing that during the ddCabinet_DataBound method

3-  Your coding of the ddCabinet_DataBound is incorrect.  Your code is
setting the ddCabinet selection to the RackID (which explains the error that
you got that the selected value does not exist).  You should have looked in
that code for the CabinetID instead of the RackID.  The purpose of this
method is to replace the function of the Eval by looking up the value on the
datatable record and setting the dropdownlist to that value if it is found.

4-  Once the user selected a value for the ddCabinet you need to save it. 
You should then add a line in the FormView1_ItemUpdating, like this:
    Dim CabinetID As Integer = CType(CType(sender,
        FormView).FindControl("ddCabinet"), DropDownList).SelectedIndex
         e.NewValues("CabinetID") = CabinetID

Show quoteHide quote
"dhizzy" wrote:

>  am trying to create a drop down list that is dependent on another
> dropdown list value inside a formview control.
>
>
> I have seen this site
> http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx
>
>
>
> but I am unable to get it working.  The only things that I have
> different is that the displayed text in both list is a text value and
> the value is an integer and also I am using the insertitemtemplate.  I
> have tried debugging
>
> Here is a code snippet:
>
> <asp:FormView ID="FormView1" runat="server" DataKeyNames="ServerID"
>          DataSourceID="DS_Servers"
> OnItemUpdating="FormView1_ItemUpdating">
>          <InsertItemTemplate>
>          <asp:SqlDataSource ID="DS_Rack" runat="server"
>                   ConnectionString="<%$
> ConnectionStrings:ConnectionString1 %>"
>                   SelectCommand="SELECT [RackID], [Rack] FROM
>                   [cRack]"></asp:SqlDataSource>
>         <asp:SqlDataSource ID="DS_Cabinet" runat="server"
>                   ConnectionString="<%$
> ConnectionStrings:ConnectionString1 %>"
>                   SelectCommand="SELECT [CabinetID], [Cabinet] FROM
>                   [cCabinet] WHERE ([RackID] = @RackID)">
>                   <SelectParameters>
>                         <asp:ControlParameter ControlID="ddRack"
> Name="RackID"
>                          PropertyName="SelectedValue" Type="Int32" />
>                   </SelectParameters>
>         </asp:SqlDataSource>
>         <table>
>             <tr align="left">
>                 <td>Rack Location</td>
>                 <td>
>                     <asp:DropDownList ID="ddRack" runat="server"
>                             AutoPostBack="True" SelectedValue='<%#
> Bind("RackID") %>'
>                             DataSourceID="DS_Rack" DataTextField="Rack"
>
>                             DataValueField="RackID"></asp:DropDownList>
>
>                             -
>                             <asp:DropDownList ID="ddCabinet"
> runat="server"
>                              SelectedValue='<%# Eval("CabinetID") %>'
> DataSourceID="DS_Cabinet"
>                              DataTextField="Cabinet"
> DataValueField="CabinetID"
>                              OnDataBound="ddCabinet_DataBound">
>                     </asp:DropDownList>
>                 </td>
>             </tr>
>             <tr>
>                 <td>
>                     <asp:LinkButton ID="InsertButton" runat="server"
>                             CausesValidation="True"
> CommandName="Insert"
>                             Text="Insert"></asp:LinkButton>
>                     <asp:LinkButton ID="InsertCancelButton"
>                             runat="server" CausesValidation="False"
> CommandName="Cancel"
>                             Text="Cancel"></asp:LinkButton>
>                 </td>
>             </tr>
>         </table>
>     </InsertItemTemplate>
> </asp:FormView>
>
>
>     Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal
> e As FormViewUpdateEventArgs)
>         Dim Rack As Integer = CType(CType(sender,
> FormView).FindControl("ddCabinet"), DropDownList).SelectedIndex
>         e.NewValues("Rack") = Rack
>         e.Cancel = False
>     End Sub
>     Protected Sub ddCabinet_DataBound(ByVal sender As Object, ByVal e
> As EventArgs)
>         Dim ddl As DropDownList = CType(sender, DropDownList)
>         Dim frmV As FormView = CType(ddl.NamingContainer, FormView)
>         If Not frmV.DataItem Is Nothing Then
>             Dim RackID As Int32 = CType(frmV.DataItem,
> Data.DataRowView)("Rack")
>             ddl.ClearSelection()
>             Dim li As ListItem = ddl.Items.FindByValue(RackID)
>             If Not li Is Nothing Then li.Selected = True
>         End If
>     End Sub
>
>
> Any help would be greatly appreciated.
>
>
Author
23 Feb 2006 5:45 PM
Uanto
Try using a string for the RackID... my code works asd is something like this

      Dim strVendorID As String = CType(frmV.DataItem,
DataRowView)("VendorID").ToString
      ddl.ClearSelection()
      Dim li As ListItem = ddl.Items.FindByValue(strVendorID)


Show quoteHide quote
"dhizzy" wrote:

>  am trying to create a drop down list that is dependent on another
> dropdown list value inside a formview control.
>
>
> I have seen this site
> http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx
>
>
>
> but I am unable to get it working.  The only things that I have
> different is that the displayed text in both list is a text value and
> the value is an integer and also I am using the insertitemtemplate.  I
> have tried debugging
>
> Here is a code snippet:
>
> <asp:FormView ID="FormView1" runat="server" DataKeyNames="ServerID"
>          DataSourceID="DS_Servers"
> OnItemUpdating="FormView1_ItemUpdating">
>          <InsertItemTemplate>
>          <asp:SqlDataSource ID="DS_Rack" runat="server"
>                   ConnectionString="<%$
> ConnectionStrings:ConnectionString1 %>"
>                   SelectCommand="SELECT [RackID], [Rack] FROM
>                   [cRack]"></asp:SqlDataSource>
>         <asp:SqlDataSource ID="DS_Cabinet" runat="server"
>                   ConnectionString="<%$
> ConnectionStrings:ConnectionString1 %>"
>                   SelectCommand="SELECT [CabinetID], [Cabinet] FROM
>                   [cCabinet] WHERE ([RackID] = @RackID)">
>                   <SelectParameters>
>                         <asp:ControlParameter ControlID="ddRack"
> Name="RackID"
>                          PropertyName="SelectedValue" Type="Int32" />
>                   </SelectParameters>
>         </asp:SqlDataSource>
>         <table>
>             <tr align="left">
>                 <td>Rack Location</td>
>                 <td>
>                     <asp:DropDownList ID="ddRack" runat="server"
>                             AutoPostBack="True" SelectedValue='<%#
> Bind("RackID") %>'
>                             DataSourceID="DS_Rack" DataTextField="Rack"
>
>                             DataValueField="RackID"></asp:DropDownList>
>
>                             -
>                             <asp:DropDownList ID="ddCabinet"
> runat="server"
>                              SelectedValue='<%# Eval("CabinetID") %>'
> DataSourceID="DS_Cabinet"
>                              DataTextField="Cabinet"
> DataValueField="CabinetID"
>                              OnDataBound="ddCabinet_DataBound">
>                     </asp:DropDownList>
>                 </td>
>             </tr>
>             <tr>
>                 <td>
>                     <asp:LinkButton ID="InsertButton" runat="server"
>                             CausesValidation="True"
> CommandName="Insert"
>                             Text="Insert"></asp:LinkButton>
>                     <asp:LinkButton ID="InsertCancelButton"
>                             runat="server" CausesValidation="False"
> CommandName="Cancel"
>                             Text="Cancel"></asp:LinkButton>
>                 </td>
>             </tr>
>         </table>
>     </InsertItemTemplate>
> </asp:FormView>
>
>
>     Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal
> e As FormViewUpdateEventArgs)
>         Dim Rack As Integer = CType(CType(sender,
> FormView).FindControl("ddCabinet"), DropDownList).SelectedIndex
>         e.NewValues("Rack") = Rack
>         e.Cancel = False
>     End Sub
>     Protected Sub ddCabinet_DataBound(ByVal sender As Object, ByVal e
> As EventArgs)
>         Dim ddl As DropDownList = CType(sender, DropDownList)
>         Dim frmV As FormView = CType(ddl.NamingContainer, FormView)
>         If Not frmV.DataItem Is Nothing Then
>             Dim RackID As Int32 = CType(frmV.DataItem,
> Data.DataRowView)("Rack")
>             ddl.ClearSelection()
>             Dim li As ListItem = ddl.Items.FindByValue(RackID)
>             If Not li Is Nothing Then li.Selected = True
>         End If
>     End Sub
>
>
> Any help would be greatly appreciated.
>
>