We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Cascading dropdown no giving me the selected value on code behind

Medium Priority
1,879 Views
Last Modified: 2012-05-06
Ive got my cascading drop downs working great, however when i try to get the selected value of one of the CDD's in the codebehind it gives me a blank value.
Code Behind:
============================
    Protected Sub btnAddUserReportValue_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddUserReportValue.Click
 
        Dim UserID As String = drpAddUserName.SelectedValue  'I added a breakpoint and this string is blank
        Dim ReportID As String = drpAddReportName.SelectedValue 'I added a breakpoint and this string is blank
        Dim ReportValue As String = txtAddReportValue.Text
        Dim SQL As String
 
        SQL = "SELECT PageAccessID From AWS_PageAccess WHERE WebPageID=" & ReportID & " AND UserID=" & UserID & ""
        Dim PageAccessID As String = DataAccess.ExecuteScalar(SQL)
 
        SQL = "SELECT PageAccessID From ORD_UserValues WHERE PageAccessID=" & PageAccessID
        If DataAccess.ExecuteScalar(SQL) Then
            SQL = "UPDATE ORD_UserValues SET reportValue='" & ReportValue & "' WHERE PageAccessID=" & PageAccessID
        Else
            SQL = "INSERT INTO ORD_UserValues (PageAccessID, ReportValue) VALUES ('" & PageAccessID & "', '" & ReportValue & "')"
        End If
 
        DataAccess.ExecuteNonQuery(SQL)
 
    End Sub
 
uservalues.ascx
==========================================================
 
<asp:Panel ID="pnlAddUserReportValue" runat="server" GroupingText="Add/Edit: User Report Value" BackColor="white">
	    <br />
	    <cc1:CascadingDropDown ID="cddAddUserName" runat="server" 
	    Category="User" 
	    TargetControlID="drpAddUserName"
        ServiceMethod="GetUsers" 
        ServicePath="~/CDD.asmx" 
        PromptText="Please select a User" />
	             
        <cc1:CascadingDropDown ID="cddAddReportName" runat="server"
        Category="Report" 
        TargetControlID="drpAddReportName"
        ParentControlID="drpAddUserName" 
        ServiceMethod="GetUserReports" 
        ServicePath="~/CDD.asmx"
        PromptText="Please select a Report" />
     
	    <table cellpadding="5">
		    <tr>
			    <td style="text-align:left;">
                    <asp:Label ID="lblAddUserName" runat="server" Text="User Name"></asp:Label><br />
                    <asp:DropDownList ID="drpAddUserName" runat="server">
                    </asp:DropDownList>
                </td>
                <td style="text-align:left;">
                    <asp:Label ID="lblAddReportName" runat="server" Text="Report Name"></asp:Label><br />
                    <asp:DropDownList ID="drpAddReportName" runat="server">
                    </asp:DropDownList>
                </td>
                <td style="text-align:left;">
                    <asp:Label ID="lblAddReportValue" runat="server" Text="Report Value"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtAddReportValue" runat="server"></asp:TextBox>
                </td>
                <td style="text-align:left;"><br /></td>
            </tr>
            <tr>
                <td colspan="3" style="text-align:right;"><asp:Button ID="btnAddUserReportValue" runat="server" Text="Add/Edit Value" /></td>
            </tr>
	    </table>
	    <br />
    </asp:Panel>

Open in new window

Comment
Watch Question

Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
If you're calling a databind you need to make sure it's only happening on the not PostBack part.

If you call the databind you will lose your reference to the selected value.  

Author

Commented:
Negative, the drop Down's are being populated via a webservice (CDD.asmx) is there something that needs to be added there?

Maybe the web service is recreating the dd's on postback, therefore losing selected value?

attached is the code for the web service...
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports AjaxControlToolkit
Imports System.Data
Imports System.Data.SqlClient
 
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<System.Web.Script.Services.ScriptService()> _
Public Class CDD
    Inherits System.Web.Services.WebService
 
    <WebMethod()> _
    Public Function GetUsers(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
 
        Dim values As New System.Collections.Generic.List(Of AjaxControlToolkit.CascadingDropDownNameValue)
 
        Dim ds As DataSet = HttpContext.Current.Cache("User")
 
        If ds Is Nothing Then
            Dim SQL As String = "SELECT [UserID], [FirstName], [LastName], [Login] FROM [AWS_User]"
            Dim conn As New SqlConnection("Data Source=FL-JAX-OPDASH;Initial Catalog=ORD_DEV;User Id=ORDUser;Password=acosta123")
            Dim comm As New SqlCommand(SQL, conn)
            Dim ad As New SqlDataAdapter(comm)
            ds = New DataSet
            ad.Fill(ds)
            '---save the dataset into cache---
            HttpContext.Current.Cache.Insert("User", ds)
        Else
            ds = CType(HttpContext.Current.Cache("User"), DataSet)
        End If
 
        For Each row As DataRow In ds.Tables(0).Rows
            values.Add(New CascadingDropDownNameValue(CType(row("Login"), String) & " - " & CType(row("FirstName"), String) & " " & CType(row("LastName"), String), CType(row("UserID"), String)))
        Next
 
        Return values.ToArray
    End Function
 
    <WebMethod()> _
    Public Function GetUserReports(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
 
        Dim kv As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
 
        If Not (kv.ContainsKey("User")) Then
            Return Nothing
        End If
 
        Dim values As New System.Collections.Generic.List(Of AjaxControlToolkit.CascadingDropDownNameValue)
 
        Dim SQL As String = "Select WP.[WebPageID] as WPID, WP.[Description] as PageName FROM AWS_PageAccess PA LEFT JOIN AWS_WebPage WP ON PA.WebPageId=WP.WebPageID LEFT JOIN AWS_User USR ON PA.UserId=USR.UserId Where PA.UserId='" & kv("User") & "' AND PA.Active=0"
        Dim conn As New SqlConnection("Data Source=FL-JAX-OPDASH;Initial Catalog=ORD_DEV;User Id=ORDUser;Password=acosta123")
        Dim comm As New SqlCommand(SQL, conn)
        Dim ad As New SqlDataAdapter(comm)
        Dim ds As New DataSet
        ad.Fill(ds)
 
        For Each row As DataRow In ds.Tables(0).Rows
            If Left(row("PAGENAME"), 5) <> "Admin" And Left(row("PAGENAME"), 4) <> "Menu" Then
                values.Add(New CascadingDropDownNameValue( _
                    CType(row("PAGENAME"), String), CType(row("WPID"), String)))
            End If
        Next
        Return values.ToArray
    End Function
 
End Class

Open in new window

Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
How are you invoking the webservice from you code behind?  That should only come from the IsPostback = false.

Author

Commented:
Im not invoking it from the code behind....

The Ajax Cascading DropDown control handles it.
Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
It still needs to be calling the web service from somewhere.  How else is it getting the data?
Your button doesn't have a onClick event:
<asp:Button ID="btnAddUserReportValue" runat="server" Text="Add/Edit Value" OnClick="btnAddUserReportValue_click" />

Author

Commented:
I have a handles clause in the code behind. everything posts back just fine, its the selected values of the cascading drop downs that are not being passed.

Can somone with some knowledge on the CDD control help me out?

Author

Commented:
@ged325

The CacadingDropDown calls the webservice using the servicemethod and servicepath properties.
Since all the events for CDD are client-side (unless you add autopostback) then you'll never know the previous drop-down values.  You could probably create and save a session variable in your webservice then refer to those variables in your button click event.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
It just so happens that CDD has a Selected Value property that allows me to pass it the Selected Value so my webservice can interperet it for me.

CtrlAltDl you are the winner.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.