AJAX CascadingDropDown ViewState Problem

Posted by Steven on Stack Overflow See other posts from Stack Overflow or by Steven
Published on 2010-05-12T13:05:55Z Indexed on 2010/05/17 18:40 UTC
Read the original article Hit count: 368

Filed under:
|
|
|
|

Question: How do I maintain both the contents (from queries) and selected value of both dropdowns after postback?

Source Code: Download my source code from this link (link now works). Just add a reference to your AjaxControlToolkit

User Action: Select a value from each dropdown. Click Submit.
After Postback: StatesDrop: (Selected value), CitiesDrop "Select a City"

Before and after:

alt text

I believe that when the first dropdown gets its selected value, the second dropdown refreshes and therefore loses its selected value.

C# answers also welcome.

Default.aspx

Active States<br /><asp:DropDownList ID="StatesDrop" runat="server" /><br />
Active Cities<br /><asp:DropDownList ID="CitiesDrop" runat="server" /><br />

<ajax:CascadingDropDown ID="StatesCasc" TargetControlID="StatesDrop"
        ServicePath="WebService1.asmx" ServiceMethod="GetActiveStates"
        Category="States" runat="server"
        PromptText="Select a State" PromptValue="?"  />

<ajax:CascadingDropDown ID="CitiesCasc" TargetControlID="CitiesDrop"
        ServicePath="WebService1.asmx" ServiceMethod="GetActiveCities"
        Category="Cities" runat="server" ParentControlID="StatesDrop"
        PromptText="Select a City"  PromptValue="?"  />

WebService1.asmx.vb

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Services
Imports AjaxControlToolkit
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding _
    (ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class WebService1: Inherits System.Web.Services.WebService

   <WebMethod()> _
   Public Function GetActiveStates (ByVal knownCategoryValues As String, _
        ByVal category As String) As CascadingDropDownNameValue()
        Dim values As New List(Of CascadingDropDownNameValue)()
        'Fill values array'
        Return values.ToArray()
    End Function

    <WebMethod()> _
    Public Function GetActiveCities (ByVal knownCategoryValues As String, _
        ByVal category As String) As CascadingDropDownNameValue()
        Dim values As New List(Of CascadingDropDownNameValue)()
        Dim kv As StringDictionary = _
         CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
        Dim SelState As String = ""
        If kv.ContainsKey("State") Then SelState = kv("State")
        'Fill values array'
        Return values.ToArray()
    End Function
End Class

Default.aspx.vb

Imports System.Web.Services
Imports System.Web.Script.Services
Imports AjaxControlToolkit

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Submit_Click(ByVal sender As Object, _
                               ByVal e As EventArgs) Handles SubmitBtn.Click
        ResultsGrid.DataBind()
    End Sub
End Class

© Stack Overflow or respective owner

Related posts about AJAX

Related posts about vb.net