我有 2 个级联下拉列表,1 个是父级,一旦您在第一个/父级下拉列表中选择一个值,另一个/子级就会填充。
第一个在页面加载时填充,我可以根据保存在前一页上的数据库值来设置这个选择的值。
对于第二个级联下拉列表,当我在页面加载时为父 ID 设置所选值(基于数据库值)时,它会为我填充。
但是由于第二个/子下拉列表在页面加载之前没有设置/加载值,所以我无法设置选定的值(我在 Page_Load_Complete 上尝试过,但它在那里也不起作用)。
我需要知道如何在父下拉列表中设置选定的值(这工作正常),根据第一个下拉列表中的值填充第二个下拉列表。
这是我的aspx页面代码。我可以设置第一个选定的值,它会填充第二个选择框(但我不能设置选定的值,因为在我设置第一个选定的值时它没有被填充。
页面
<asp:Label ID="lblAffPartCat" Text="<%$ Resources:share,lblAffPartCat %>" runat="server"></asp:Label>
<asp:DropDownList ID="ddlPartCat" runat="server"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="ddlPartCat"
Category="BasePart" PromptText="<%$ Resources:share,lblSelPartCat %>" LoadingText="[Loading Part Cat...]"
ServicePath="PAIntExtPart.asmx" ServiceMethod="BindPartCat"
ContextKey="" UseContextKey="True"/>
<asp:Label ID="lblAffBasePart" Text="<%$ Resources:share,lblAffBasePart %>" runat="server"></asp:Label>
<asp:DropDownList ID="ddlBasePart" runat="server" ></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="ddlBasePart_CascadingDropDown" runat="server" Category="BasePart"
TargetControlID="ddlBasePart" ParentControlID= "ddlPartCat" PromptText="<%$ Resources:share,lblSelBasePart %>"
LoadingText="Loading Base Parts.."
ServicePath="PAIntExtPart.asmx"
ServiceMethod="BindBasePart"
ContextKey="" UseContextKey="True" />
填充下拉列表的 asmx.cs 页面:
using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Data;
using System.Collections.Specialized;
using AjaxControlToolkit;
using Hotline.DataAccess;
/// <summary>
/// Summary description for PAIntExtPart
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService()]
public class PAIntExtPart : System.Web.Services.WebService
{
string _SiteLocation = MiscFunctions.getCurrentSiteLocation();
/// <summary>
/// WebMethod to Populate Part Category Dropdown
/// </summary>
[WebMethod]
public CascadingDropDownNameValue[] BindPartCat(string knownCategoryValues, string category, string contextKey)
{
DataTable dsPartCat = null;
// string passed for contextKey is FormType and Language split by ":"
string[] arrcontextKey = contextKey.Split(':');
string FormType = arrcontextKey[0].ToString();
int LanguageID = Int32.Parse(arrcontextKey[1].ToString());
string PartCatValue = arrcontextKey[2].ToString();
try
{
dsPartCat = HarDB.getPartCat(_SiteLocation, LanguageID, FormType);
//create list and add items in it by looping through dataset table
List<CascadingDropDownNameValue> PartCatdetails = new List<CascadingDropDownNameValue>();
foreach (DataRow dtrow in dsPartCat.Rows)
{
string PartCatID = dtrow["PartCatID"].ToString();
string PartCat = dtrow["PartCat"].ToString();
PartCatdetails.Add(new CascadingDropDownNameValue(PartCat, PartCatID));
}
if (PartCatValue.Trim() != "")
{
//SelectedValue = PartCatValue;
}
return PartCatdetails.ToArray();
}
catch (Exception ex)
{
Server.Transfer("Errorpage.aspx?function=getAttachInfo+Error=" + Server.UrlEncode(ex.Message));
return null;
}
}
/// <summary>
/// WebMethod to Populate Base Part Dropdown
/// </summary>
[WebMethod]
public CascadingDropDownNameValue[] BindBasePart(string knownCategoryValues, string category, string contextKey)
{
string PartCatID;
//int LanguageID = Int32.Parse(contextKey);
string[] arrcontextKey = contextKey.Split(':');
string FormType = arrcontextKey[0].ToString();
int LanguageID = Int32.Parse(arrcontextKey[1].ToString());
string BasePartValue = arrcontextKey[2].ToString();
//This method will return a StringDictionary containing the name/value pairs of the currently selected values
StringDictionary PartCatdetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
PartCatID = PartCatdetails["BasePart"];
DataTable dsBasePart = null;
try
{
dsBasePart = HarDB.getBasePart(_SiteLocation, LanguageID, PartCatID, FormType);
//create list and add items in it by looping through dataset table
List<CascadingDropDownNameValue> BasePartdetails = new List<CascadingDropDownNameValue>();
foreach (DataRow dtrow in dsBasePart.Rows)
{
string BasePartID = dtrow["BasePartNumID"].ToString();
string BasePart = dtrow["BasePartNum"].ToString();
BasePartdetails.Add(new CascadingDropDownNameValue(BasePart, BasePartID));
}
if (BasePartValue.Trim() != "")
{
//SelectedValue = PartCatValue;
}
return BasePartdetails.ToArray();
}
catch (Exception ex)
{
Server.Transfer("Errorpage.aspx?function=getAttachInfo+Error=" + Server.UrlEncode(ex.Message));
return null;
}
}
}