问题:如何在回发后维护两个下拉列表的内容(来自查询)和选定的值?
源代码:从这个链接下载我的源代码(链接现在工作)。只需添加对AjaxControlToolkit的引用即可
用户操作:从每个下拉列表中选择一个值。单击Submit。
后回发: StatesDrop:(选定值),CitiesDrop“选择一个城市”
前后:
alt文本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 ClassDefault.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发布于 2010-05-21 13:54:45
我放弃了CascadingDropDown,转而使用常规回发和UpdatePanel。
发布于 2010-05-20 11:18:31
因为依赖下拉列表的项是在客户端填充的。服务器没有意识到这一点。您必须在每个回发上填充依赖项下拉列表。因此,在您的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-12 13:29:53
要在回发中维护下拉列表的内容,请确保加载代码背后控件的逻辑位于if语句检查中,以查看是否为回发。例如..。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Load Controls
}
}保存控件中的数据将由viewstate完成。
https://stackoverflow.com/questions/2818998
复制相似问题