• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1837
  • Last Modified:

Cascading dropdown no giving me the selected value on code behind

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

0
pikarde822
Asked:
pikarde822
  • 5
  • 3
  • 2
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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.  
0
 
pikarde822Author 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

0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
How are you invoking the webservice from you code behind?  That should only come from the IsPostback = false.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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

The Ajax Cascading DropDown control handles it.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
It still needs to be calling the web service from somewhere.  How else is it getting the data?
0
 
CtrlAltDlCommented:
Your button doesn't have a onClick event:
<asp:Button ID="btnAddUserReportValue" runat="server" Text="Add/Edit Value" OnClick="btnAddUserReportValue_click" />
0
 
pikarde822Author 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?
0
 
pikarde822Author Commented:
@ged325

The CacadingDropDown calls the webservice using the servicemethod and servicepath properties.
0
 
CtrlAltDlCommented:
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.
0
 
pikarde822Author 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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now