문제

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 http://www.aphio.org.vt.edu/test/beforeandafter.GIF

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
도움이 되었습니까?

해결책 3

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

다른 팁

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top