2

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

源代码:从此链接下载我的源代码(链接现在有效)。只需添加对 AjaxControlToolkit 的引用

User Action:从每个下拉列表中选择一个值。单击提交。
回发后: StatesDrop:(选定值),CitiesDrop“选择城市”

之前和之后:

替代文字 http://www.aphio.org.vt.edu/test/beforeandafter.GIF

我相信当第一个下拉列表获得其选定值时,第二个下拉列表会刷新并因此丢失其选定值。

也欢迎 C# 答案。

默认.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

默认.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
4

3 回答 3

1

由于依赖下拉列表的项目是在客户端填充的。服务器对此一无所知。您必须在每个回帖上填充相关下拉列表的项目。因此,在您的 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];
}

希望这会有所帮助。

于 2010-05-20T11:18:31.850 回答
0

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

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

保存来自控件的数据将由视图状态完成。

于 2010-05-12T13:29:53.007 回答
0

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

于 2010-05-21T13:54:45.700 回答