views:

457

answers:

3

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/")&gt; _
<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
A: 

To maintain the content of the dropdownlists on a postback, make sure the logic that loads the controls in your codebehind is in an if statement checking to see if it's a postback or not. For example...

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Load Controls
        }
    }

Keeping the data from the controls will be done by the viewstate.

Dan H
The controls are part of the aspx code. I don't need to load anything in the page_load routine.
Steven
A: 

Since the items of the dependent drop down list are populated on the client side. Server is unaware about it. You have to populate items of dependent drop down list on each post back. So write following code in your page_load.

if(!IsPostBack) {
 //Some logic
}
else {
    //populate child drop down list on the base of selected value of parent drop down. 
// you can set the selected value of child control by getting the selected value from Request //object for example write following code to set the value of child control

childControl.SelectedValue = Request[childControl.UniqueID];
}

hope this will help.

Muhammad Kamran
Please provide a modified version of my source code. If it works, I'll accept.
Steven
**Doesn't work.** The client code (which resets the child dropdown) is called after page_load and even after page_unload. Also, in form_load (and unload) both dropdowns only have 1 Item, which is the `SelectedValue` of each.
Steven
+1  A: 

I scrapped the CascadingDropDown and instead used regular postbacks and an UpdatePanel.

Steven