gpt4 book ai didi

c# - AJAX CascadingDropDown ViewState问题

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:16 24 4
gpt4 key购买 nike

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

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

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

之前和之后:

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

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

也欢迎 C# 答案。

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

最佳答案

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

希望这会有所帮助。

关于c# - AJAX CascadingDropDown ViewState问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2818998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com