AJAX CascadingDropDown ViewState issue
Question: How do I save both the content (from queries) and the selected value of both dropdowns after postback?
Source Code: Download my source code from link ( link now works >). Just add a link to your AjaxControlToolkit
User Action: Select a value from each dropdown list. Click Submit.
After postback StatesDrop
:: (Selected value), CitiesDrop
"Select city"
Before and after:
alt text http://www.aphio.org.vt.edu/test/beforeandafter.GIF
I believe that when the first dropdown gets its selected value, the second dropdown is updated and hence loses the selected value.
C # answers are 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
a source to share
Since the items of the dependent dropdown are populated on the client side. The server is not aware of this. You have to fill in the items of the dependent dropdown list on each column. So write the 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 helps.
a source to share
To preserve the contents of the dropdownlists after a postback, make sure that the logic that loads the controls in your code is in an if statement that checks if it's a postback or not. For instance...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Load Controls
}
}
Saving data from controls will be done using the viewstate.
a source to share