首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CascadingDropDown中预先填充数据库中的数据

在CascadingDropDown中预先填充数据库中的数据
EN

Stack Overflow用户
提问于 2011-10-27 08:17:45
回答 3查看 1.1K关注 0票数 1

我有三个CascadingDropDown在一个网页上,我使用在我的网站上的许多地方。它们包含数据库中的国家、地区和地区名称。我已经成功地将数据绑定到它们并将它们设置为级联。但是,当用户在我的站点上注册时,我想在三个CascadingDropDown中预选三个值。

注册后,我还需要在管理区域看到他们的数据,在那里我还需要预选三个CascadingDropDown,并选择用户选择的值。但我不知道该怎么做。

我的Webservice代码:

代码语言:javascript
复制
namespace ZetaSolutions.WebProjects.Web.Modules
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService()]
    public class PlaceSelection : System.Web.Services.WebService
    {
        [WebMethod]
        public CascadingDropDownNameValue[] BindCountryDropDown(string knownCategoryValues, string category)
        {
            SqlConnection conCountry = new SqlConnection(ZetaConfig.ConnectionString);
            conCountry.Open();

            SqlCommand cmdCountry = new SqlCommand("SELECT * FROM Country ORDER BY Name", conCountry);
            SqlDataAdapter daCountry = new SqlDataAdapter(cmdCountry);
            cmdCountry.ExecuteNonQuery();

            DataSet dsCountry = new DataSet();
            daCountry.Fill(dsCountry);
            conCountry.Close();

            List<CascadingDropDownNameValue> countryDetails = new List<CascadingDropDownNameValue>();

            foreach (DataRow dtRow in dsCountry.Tables[0].Rows)
            {
                string countryId = dtRow["CountryID"].ToString();
                string countryName = dtRow["Name"].ToString();

                countryDetails.Add(new CascadingDropDownNameValue(countryName, countryId));
            }

            return countryDetails.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] BindDistrictDropDown(string knownCategoryValues, string category)
        {
            int countryId;
            StringDictionary countryDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            countryId = Convert.ToInt32(countryDetails["Country"]);

            SqlConnection conDistrict = new SqlConnection(ZetaConfig.ConnectionString);
            conDistrict.Open();
            SqlCommand cmdDistrict = new SqlCommand("SELECT * FROM District WHERE CountryID=@CountryID ORDER BY Name", conDistrict);
            cmdDistrict.Parameters.AddWithValue("@CountryID", countryId);
            cmdDistrict.ExecuteNonQuery();

            SqlDataAdapter daDistrict = new SqlDataAdapter(cmdDistrict);
            DataSet dsDistrict = new DataSet();
            daDistrict.Fill(dsDistrict);
            conDistrict.Close();

            List<CascadingDropDownNameValue> districtDetails = new List<CascadingDropDownNameValue>();

            foreach (DataRow dtDistrictRow in dsDistrict.Tables[0].Rows)
            {
                string districtId = dtDistrictRow["DistrictID"].ToString();
                string districtName = dtDistrictRow["Name"].ToString();
                districtDetails.Add(new CascadingDropDownNameValue(districtName, districtId));
            }

            return districtDetails.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] BindAreaDropDown(string knownCategoryValues, string category)
        {
            int districtId;
            StringDictionary districtDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            districtId = Convert.ToInt32(districtDetails["District"]);

            SqlConnection conArea = new SqlConnection(ZetaConfig.ConnectionString);
            conArea.Open();
            SqlCommand cmdArea = new SqlCommand("SELECT * FROM Area WHERE DistrictID=@DistrictID ORDER BY Name", conArea);
            cmdArea.Parameters.AddWithValue("@DistrictID ", districtId);
            cmdArea.ExecuteNonQuery();

            SqlDataAdapter daArea = new SqlDataAdapter(cmdArea);
            DataSet dsArea = new DataSet();
            daArea.Fill(dsArea);
            conArea.Close();

            List<CascadingDropDownNameValue> areaDetails = new List<CascadingDropDownNameValue>();

            foreach (DataRow dtAreaRow in dsArea.Tables[0].Rows)
            {
                string areaId = dtAreaRow["AreaID"].ToString();
                string areaName = dtAreaRow["Name"].ToString();
                areaDetails.Add(new CascadingDropDownNameValue(areaName, areaId));
            }

            return areaDetails.ToArray();
        }
    }
}

我的aspx代码:

代码语言:javascript
复制
<table>
    <tr>
        <td>
            Country:
        </td>
        <td>
            <asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList>
            <ajaxToolkit:CascadingDropDown ID="CountryCascading" runat="server" Category="Country" TargetControlID="ddlCountry" LoadingText="Loading Countries..." PromptText="Select Country" ServiceMethod="BindCountryDropDown" ServicePath="PlaceSelection.asmx">
            </ajaxToolkit:CascadingDropDown>
        </td>
    </tr>
    <tr>
        <td>
            District:
        </td>
        <td>
            <asp:DropDownList ID="ddlDistrict" runat="server"></asp:DropDownList>
            <ajaxToolkit:CascadingDropDown ID="DistrictCascading" runat="server" Category="District" TargetControlID="ddlDistrict" ParentControlID="ddlCountry" LoadingText="Loading Districts..." PromptText="Select District" ServiceMethod="BindDistrictDropDown" ServicePath="PlaceSelection.asmx">
            </ajaxToolkit:CascadingDropDown>
        </td>
    </tr>
    <tr>
        <td>
            Area:
        </td>
        <td>
            <asp:DropDownList ID="ddlArea" runat="server"></asp:DropDownList>
            <ajaxToolkit:CascadingDropDown ID="AreaCascading" runat="server" Category="Area" TargetControlID="ddlArea" ParentControlID="ddlDistrict" LoadingText="Loading Areas..." PromptText="select Areas" ServiceMethod="BindAreaDropDown" ServicePath="PlaceSelection.asmx">
            </ajaxToolkit:CascadingDropDown>
        </td>
    </tr>
</table> 

有人能告诉我怎么解决我的问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-10-27 08:20:31

您需要读取的值,因为您想从您的‘管理’系统中选择,然后使用;

代码语言:javascript
复制
DropDownList.SelectedValue = "value";

在Page_Load上使用回发支票。

请参阅:SelectedValue性质

票数 1
EN

Stack Overflow用户

发布于 2014-01-17 05:49:31

谢谢,这段代码是为我工作的。

代码语言:javascript
复制
    CascadingDropdownID1.SelectedValue = dsdataset.Table(0).Rows(0)(0).ToString()
    CascadingDropdownID2.SelectedValue = dsdataset.Table(0).Rows(0)(1).ToString()
    CascadingDropdownID3.SelectedValue = dsdataset.Table(0).Rows(0)(2).ToString()
票数 0
EN

Stack Overflow用户

发布于 2011-10-27 17:44:49

我知道答案了。ChrisBint,SelectedValue属性不应属于DropDown本身。它应该是CascadingDropDown的。所以现在我的代码是:

代码语言:javascript
复制
CountryCascading.SelectedValue = countryId.ToString();
DistrictCascading.SelectedValue = districtId.ToString();
AreaCascading.SelectedValue = areaId.ToString();

正如ChrisBint所说,我在if (!Page.IsPostBack)块之外编写了这段代码。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7913304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档