首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AJAX CascadingDropDown ViewState问题

AJAX CascadingDropDown ViewState问题
EN

Stack Overflow用户
提问于 2010-05-12 13:05:55
回答 3查看 2.7K关注 0票数 2

问题:如何在回发后维护两个下拉列表的内容(来自查询)和选定的值?

源代码:从这个链接下载我的源代码(链接现在工作)。只需添加对AjaxControlToolkit的引用即可

用户操作:从每个下拉列表中选择一个值。单击Submit。

后回发: StatesDrop:(选定值),CitiesDrop“选择一个城市”

前后:

alt文本http://www.aphio.org.vt.edu/test/beforeandafter.GIF

我认为,当第一个下拉列表获得其选定的值时,第二个下拉列表就会刷新,从而失去其选定的值。

C#的答案也欢迎。

Default.aspx

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-05-21 13:54:45

我放弃了CascadingDropDown,转而使用常规回发和UpdatePanel。

票数 0
EN

Stack Overflow用户

发布于 2010-05-20 11:18:31

因为依赖下拉列表的项是在客户端填充的。服务器没有意识到这一点。您必须在每个回发上填充依赖项下拉列表。因此,在您的page_load中编写以下代码。

代码语言:javascript
复制
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];
}

希望这能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2010-05-12 13:29:53

要在回发中维护下拉列表的内容,请确保加载代码背后控件的逻辑位于if语句检查中,以查看是否为回发。例如..。

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Load Controls
        }
    }

保存控件中的数据将由viewstate完成。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2818998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档