Saving SelectedValue of CascadingDropDown in DetailsView into database

Hi,

I’ve been wrestling with this problem for a few days, and searching like crazy, but not really finding any solution.

First, the scenario:
I have a form for our staff to list an event in a calendar system. In the form, there is a DDL for their Department, and a CasscadingDropDown that provides a list of staff members (the staff list is filtered by Department, as chosen in the first DDL). When the form gets submitted, I need to store the selected values of both DDL’s (DepartmentID and StaffID, respectively) into the database.

So, I have a DetailsView, and am using the InsertItemTemplate. Now, I can get both DDL’s populated correctly (with the cascade working correctly), and I used custom DDL’s derived from the standard DDL class to avoid any Postback/EventValidation problems. But I can’t seem to get the values of the user’s selected items so I can save them back into the database!

I’ve tried many different approaches, but all I seem to get returned is a value of “”.

Here’s what I have so far…

EditWS.aspx

 <%@ Page Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="EditWS.aspx.vb" Inherits="Services_Workshops_EditWS" title="" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

...
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    
    <asp:ObjectDataSource ID="WSDetailsDataSource" runat="server" OldValuesParameterFormatString="{0}"
        SelectMethod="GetWorkshopByWorkshopID" TypeName="WorkshopsBLL" 
        UpdateMethod="UpdateWorkshop" InsertMethod="AddWorkshop" 
        DeleteMethod="DeleteWorkshop">
        <UpdateParameters>...</UpdateParameters>
        <SelectParameters>...</SelectParameters>
        <DeleteParameters>...</DeleteParameters>
        <InsertParameters>...</InsertParameters>
    </asp:ObjectDataSource>
   
    <asp:DetailsView ID="WSDetailsView" runat="server" AutoGenerateRows="False" 
        DataKeyNames="WorkshopID" DataSourceID="WSDetailsDataSource" 
        DefaultMode="Edit" GridLines="None">
        <Fields>
            ...
            <asp:TemplateField HeaderText="Department:" SortExpression="DepartmentID">
                <InsertItemTemplate>
                    <asp:PlaceHolder ID="ph1" runat="server" />
                </InsertItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="ESC Contact:" SortExpression="ESCContact">
                <InsertItemTemplate>
                    <asp:PlaceHolder ID="ph2" runat="server" />
                </InsertItemTemplate>
            </asp:TemplateField>
           ... 
        </Fields>
    </asp:DetailsView>

Code Behind (EditWS.aspx.vb)

Imports System.Web
Imports System.Web.Services
Imports System.Data
Imports AjaxControlToolkit
Imports CustomControls

Partial Class Services_Workshops_EditWS
    Inherits System.Web.UI.Page

    Private nvddl1 As NoValidationDropDownList
    Private nvddl2 As NoValidationDropDownList
    Private cdd1 As AjaxControlToolkit.CascadingDropDown
    Private cdd2 As AjaxControlToolkit.CascadingDropDown


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim ph1 As PlaceHolder = CType(WSDetailsView.FindControl("ph1"), PlaceHolder)
        Dim ph2 As PlaceHolder = CType(WSDetailsView.FindControl("ph2"), PlaceHolder)

        nvddl1 = New NoValidationDropDownList()
        nvddl1.ID = "DepartmentList"
        nvddl1.Width = "415"
        ph1.Controls.Add(nvddl1)

        cdd1 = New CascadingDropDown()
        cdd1.ID = "cdd1"
        cdd1.TargetControlID = "DepartmentList"
        cdd1.Category = "DepartmentName"
        cdd1.ServicePath = "~/ESCContacts.asmx"
        cdd1.ServiceMethod = "GetESCDepartments"
        cdd1.ContextKey = ""
        cdd1.LoadingText = "Updating"
        cdd1.PromptText = "Select a Department"

        ph1.Controls.Add(cdd1)


        nvddl2 = New NoValidationDropDownList()
        nvddl2.ID = "ESCContactList"
        nvddl2.Width = "415"
        ph2.Controls.Add(nvddl2)

        cdd2 = New CascadingDropDown()
        cdd2.ID = "cdd2"
        cdd2.TargetControlID = "ESCContactList"
        cdd2.ParentControlID = "DepartmentList"
        cdd2.Category = "ESCContact"
        cdd2.ServicePath = "~/ESCContacts.asmx"
        cdd2.ServiceMethod = "GetESCContacts"
        cdd2.ContextKey = ""
        cdd2.PromptText = "Select an Employee"
        cdd2.LoadingText = "Updating"

        ph2.Controls.Add(cdd2)
    End Sub

My Web Service

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports AjaxControlToolkit
Imports System.Data
'Imports System.Data.SqlClient
'Imports System.Configuration

<WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Script.Services.ScriptService()> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class ESCContacts
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function GetESCContacts(ByVal knownCategoryValues As String, ByVal category As String, ByVal contextKey As String) As CascadingDropDownNameValue()

        Dim Param As StringDictionary
        Param = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
        Dim ParamInt As Integer = Convert.ToInt32(Param("DepartmentName"))


        Dim StaffAPI As New StaffBLL()
        Dim FacilitatorsTable As ESC.ESC_StaffDataTable = StaffAPI.GetActiveFacillitatorsByDepartmentID(ParamInt)
        Dim ContactList As New List(Of CascadingDropDownNameValue)

        For Each dr As DataRow In FacilitatorsTable
            If dr("StaffID").ToString() = contextKey Then
                ContactList.Add(New CascadingDropDownNameValue(DirectCast((dr("FName") & " " & dr("LName")), String), dr("StaffID").ToString(), True))
            Else
                ContactList.Add(New CascadingDropDownNameValue(DirectCast((dr("FName") & " " & dr("LName")), String), dr("StaffID").ToString(), False))
            End If
        Next
        Return ContactList.ToArray
    End Function

    <WebMethod()> _
    Public Function GetESCDepartments(ByVal knownCategoryValues As String, ByVal category As String, ByVal contextKey As String) As CascadingDropDownNameValue()

        Dim DepartmentsAPI As New DepartmentsBLL()
        Dim FacilitatorsTable As ESC.ESC_DepartmentsDataTable = DepartmentsAPI.GetDepartments
        Dim DepartmentList As New List(Of CascadingDropDownNameValue)

        For Each dr As DataRow In FacilitatorsTable
            If dr("DepartmentID").ToString() = contextKey Then
                DepartmentList.Add(New CascadingDropDownNameValue(DirectCast(dr("DepartmentName"), String), dr("DepartmentID").ToString(), True))
            Else
                DepartmentList.Add(New CascadingDropDownNameValue(DirectCast(dr("DepartmentName"), String), dr("DepartmentID").ToString(), False))
            End If
        Next
        Return DepartmentList.ToArray

    End Function
End Class

So how can I get at the value of the selected items?

I’ve things like tried this…

Protected Sub WSDetailsView_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles WSDetailsView.ItemInserting

Dim DepartmentList As DropDownList = DirectCast(WSDetailsView.FindControl("DepartmentList"), DropDownList)
e.Values("departmentID") = DepartmentList.SelectedValue

End Sub

… but all I get returned is “”.

Any ideas?

It seems like all of the examples I can find online discuss how to populate the dropdowns, but stop there, which has been frustrating to say the least. If anyone knows of any good examples that show how to get the selected value from the cascadingdropdown so that I can bind the data when the form is submitted, I’d be eternally grateful!!

Thanks for taking the time to read this.

try

e.Values(“DDL1”) = DirectCast(DirectCast(sender, DetailsView).FindControl(“DDL1”), DropDownList).SelectedValue

did you enable viewstate WITH DDL, and also during postback your page load event cannot re populate the ddl, then you will not get any selected value.

DDL is the biggest headache in asp.net

BTW, I converted my c# code to vb using converter, hope you get the point