Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have cascading drop down boxes that appear to be working - BUT when I try to bind the displayed text I somehow bind the ID associated with the displayed text.

I don't know if the problem is in my webservice or in the way I bind the data. If anyone has any ideas I would really appreciate it.

My Web Method:

WebMethod(); _
Public Function GetProcedure(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
       Dim strConnection As String = ConfigurationManager.ConnectionStrings("WPSConnectionString").ConnectionString
       Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
       Dim strProcQuery As String = "SELECT  [Procedure], ProcID from Proc_Procedure where SubCategoryID = @SubCategoryID"
       Dim cmdFetchProc As SqlCommand = New SqlCommand(strProcQuery, sqlConn)

       Dim dtrProc As SqlDataReader
       Dim kvProc As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)

       Dim SubCategoryId As Integer

       If Not kvProc.ContainsKey("SubCategory") Or Not Int32.TryParse(kvProc("SubCategory"), SubCategoryId) Then
           Return Nothing
       End If

       cmdFetchProc.Parameters.AddWithValue("@SubCategoryID", SubCategoryId)

       Dim myProc As New List(Of CascadingDropDownNameValue)

       sqlConn.Open()
       dtrProc = cmdFetchProc.ExecuteReader

       While dtrProc.Read()
           Dim strProcName As String = dtrProc("Procedure").ToString
           Dim strProcId As Integer = dtrProc("ProcID")

           myProc.Add(New CascadingDropDownNameValue(strProcName, strProcId))
       End While

       Return myProc.ToArray
   End Function



My data binding:
<asp:DropDownList ID="ddlprocedure" runat="server"
     SelectedValue='<%# bind("Procedure") %>' DataTextField="Procedure" DataValueField="ProcID">
     </asp:DropDownList>
     <asp:CascadingDropDown ID="ddlprocedure_cdd" runat="server"
     TargetControlID="ddlprocedure"
     Category="Procedure"
     PromptText="Select Procedure"
     LoadingText="Please wait..."
     ServicePath="~/ProcedureService.asmx"
     ServiceMethod="GetProcedure"
     ParentcontrolID="ddlSubCategory">
     </asp:CascadingDropDown>

     </InsertItemTemplate>
     <ItemTemplate>
     <asp:Label ID="Label1" runat="server" Text='<%# Bind("Procedure") %>'></asp:Label>
Posted

1 solution

I figured out a work-around for my issue.

I bound the data from my cascading drop down extender instead of the drop down control itself. This data appeared as the ID followed by ":::" followed by the text value (ex. 13:::sometext)

To deal with that, I created an onindexchange sub on the drop down box that uses indexof and substring to pull out the text I want - and then sets the final value to a session variable.

Then I modified my insert and update parameter fields for my detailsview to use a session parameter instead of a regular parameter for the affected field.

Nobody is going to call it elegant, but it gets the job done.

   Private Sub proc_trim(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlvar As CascadingDropDown = CType(DetailsView1.FindControl("ddlprocedure_cdd"), CascadingDropDown)
        Dim sbad As String = ddlvar.SelectedValue
        If sbad.IndexOf(":::") < 0 Then
            Session("clean_proc") = sbad
        Else
            Dim i As Integer = sbad.IndexOf(":::")
            Dim smid As String = sbad.Substring(i)
            Dim sneed As String = smid.Substring(3)
            Session("clean_proc") = sneed
        End If
    End Sub


.....

<asp:DropDownList ID="ddlprocedure" runat="server" OnSelectedIndexChanged="proc_trim" 
DataTextField="Procedure" DataValueField="Procedure">
</asp:DropDownList>
<asp:CascadingDropDown ID="ddlprocedure_cdd" runat="server" 
           TargetControlID="ddlprocedure"
           Category="Procedure"
           PromptText="Select Procedure"
           LoadingText="Please wait..."
           ServicePath="~/ProcedureService.asmx"
           ServiceMethod="GetProcedure"
           ParentcontrolID="ddlSubCategory">      
</asp:CascadingDropDown>

.....

<InsertParameters>
<asp:SessionParameter Name="procedure" SessionField="clean_proc" Type="String" />
</InsertParameters>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900