A nice addition to the ajax controls toolkit is the CascadingDropDown adapter...

The problem is that the selected value of the dropdowns was not available after a postback,
and i needed this to restore my controls back to their state after a user postback & pageredirectsavingstate
(using a persistant page state see
http://www.codeproject.com/aspnet/persistentstatepage.asp)

the trick was easy,
save the value of dropdownlists in the viewstate after postback, before redirection

ViewState("CascadingDropDown1") = Me.CascadingDropDown1.SelectedValue
ViewState("CascadingDropDown2") = Me.CascadingDropDown2.SelectedValue

 

And then, when you come back on the page, you can eventually get your lists back at page_Prerender

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

If Me.IsRestoredPageState Then

Me.CascadingDropDown1.SelectedValue = ViewState("CascadingDropDown1")
Me.CascadingDropDown2.SelectedValue = ViewState("CascadingDropDown2")

End If

End Sub

 

 

That's all Smile

Well, almost...

After restoring the cascading dropdownvalues values at page_PreRender...
If you try to read the linked dropdownlists before any postback, their selectedvalues are empty !!

E.g.:

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

If Me.IsRestoredPageState Then

Me.CascadingDropDown1.SelectedValue = ViewState("CascadingDropDown1")
Me.CascadingDropDown2.SelectedValue = ViewState("CascadingDropDown2")

' now try to use the dropdownlist.selectedvalue
Dim val as integer = Ctype(Me.dropdownlist1.selectedvalue,integer)

' the value is empty !!!!!
' but you canread the cascadingdropdownlist adapter selected value
val  = Ctype(Me.CascadingDropDown1.selectedvalue, integer) ' Value is OK

End If

End Sub

 

OK, so you think that we can just use our CascadingDropDown selectedvalue... well, NO !
if you postback now ...

' after postback

val = Ctype(Me.dropdownlist1.selectedvalue, integer) ' gets the correct value

val  = Ctype(Me.CascadingDropDown1.selectedvalue, integer) ' CRASH
' a Value is available, but at the following text format "valueField:::TextField" !!!
' If no value is selected on the dropdownlist, the cascadingdropdown.selectedvalue returns ":::"

Finally, to get the correct value from the cascading adapter:

 If Me.CascadingDropDown1.SelectedValue.Replace(":::", "").Trim <> "" Then

val = CType(Me.ccddlEvent.SelectedValue.Split(":::")(0), Integer)

End If

 

no comment.... Sealed

 

related:
http://gandhirohan.blogspot.com/2007/09/cascadingdropdown-control.html
http://forums.asp.net/t/1232819.aspx

use cascading dropdown with a database
http://www.nerdliness.com/article/2008/02/15/using-asp-net-ajax-cascadingdropdown-visual-basic-and-database