根据父值设置选定的值

时间:2013-03-12 16:19:17

标签: asp.net ajaxcontroltoolkit cascadingdropdown

我有2个级联下拉菜单,1是父级,另一个/子级填充在第一个/父级下拉列表中选择一个值。

第一个填充页面加载,我可以设置这个基于上一页保存的数据库值的选定值。

对于第二个级联下拉列表,当我在页面加载时设置父ID的选定值(基于数据库值)时,它会为我填充。

但是由于第二个/子项下拉列表没有设置/加载值,直到页面加载我无法设置所选值(我尝试过Page_Load_Complete,但它也不起作用)。

我需要知道一旦我在父下拉列表中设置所选值(这样可以正常工作),就可以根据第一个值中的值填充第二个下拉列表。

这是我的aspx页面的代码。我可以设置第一个选定的值,它会填充第二个选择框(但是我无法设置所选的值,因为在我设置第一个选定值时它没有填充。

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;
            }

        }

    }

1 个答案:

答案 0 :(得分:0)

我发现了我的问题,在尝试填充第二个下拉列表时,我没有使用正确的“选定值”。