首页 >> 编程知识
asp.net AJAX中的CascadingDropDown控件使用心得、在Web.config中注册用户控件和自定义控件的优越性
asp.net AJAX中的CascadingDropDown控件使用心得
asp.net AJAX中的CascadingDropDown控件使用心得
<!--下拉列表控件-->
<asp:DropDownList ID="ddlRootClass" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList>
<!--对应的CascadingDropDown控件-->
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" LoadingText="加载中" PromptText="请选择"
ServiceMethod="ClientTypeRootList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlRootClass" Category="RootClientType">
</cc1:CascadingDropDown>
<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" LoadingText="加载中" PromptText="请选择"
ServiceMethod="ClientTypeSubList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx"
TargetControlID="ddlSubClass" Category="SubClientType" ParentControlID="ddlRootClass">
</cc1:CascadingDropDown>
<!--下拉列表控件--> <asp:DropDownList ID="ddlRootClass" runat="server"></asp:DropDownList> <asp:DropDownList ID="ddlSubClass" runat="server"></asp:DropDownList> <!--对应的CascadingDropDown控件--> <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" LoadingText="加载中" PromptText="请选择" ServiceMethod="ClientTypeRootList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx" TargetControlID="ddlRootClass" Category="RootClientType"> </cc1:CascadingDropDown> <cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" LoadingText="加载中" PromptText="请选择" ServiceMethod="ClientTypeSubList" ServicePath="/Common/ClientTypeCascadingDropDown.asmx" TargetControlID="ddlSubClass" Category="SubClientType" ParentControlID="ddlRootClass"> </cc1:CascadingDropDown>
注意CascadingDropDownr控件中的Category设置,Category主要就是为你CascadingDropDownr控件对应的下拉列表控件选定的值取个名字,好区分是下拉列表的值,所以这个得取的不一样。ServiceMethod主要就是对应WebSerivce的方法了,指明当前CascadingDropDown控件使用哪个WebSerivce中的方法,其它的么就不细说了。
再来看WebService的代码
view plaincopy to clipboardprint?
/// <summary>
/// ClientType Ajax下拉列表数据服务(注意代码中的[]是全角,使用的时候替换成半角的)
/// </summary>
[WebService(Namespace = "http:tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService] //<-这段必须得存在
public class ClientTypeCascadingDropDown : System.Web.Services.WebService
{
[WebMethod]
//一级客户类别相关的WebService方法
public CascadingDropDownNameValue[] ClientTypeRootList(string knownCategoryValues,string category) //<-除了ClientTypeRootList这个方法名可变动,其它不能变动
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-这段也得有,呵呵
//这里就可以放你的数据库相关代码,比如把一级客户类别从数据库取出来然后存放在一个数组中
//因为这里是一级客户的下拉列表,所以不用去管那个category的值
//Model.ClientType是我建的一个实体类,其中有ClientTypeName,ClientTypeID,ParentClientTypeID几个属性
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//当然你也可以使用DataSet、DataTabel等,在foreach那边把列表需要的值填充进去就好
//下以部分是下拉列表填充代码
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
foreach (Model.ClientType model in models)
{
values.Add(new CascadingDropDownNameValue(model.ClientTypeName,model.ClientTypeID.ToString()));
}
return values.ToArray();
}
[WebMethod]
//二级客户类别相关的WebService方法
public CascadingDropDownNameValue[] ClientTypeSubList(string knownCategoryValues, string category) //<-除了ClientTypeRootList这个方法名可变动,其它不能变动
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-这段也得有,呵呵
//二级客户的下拉列表,得取得一级的category的值,以下代码是判断上级列表的category值,存在或不是空的话把值赋给parentClientTypeID
//RootClientType是一级CascadingDropDown设置的category属性名称
int parentClientTypeID;
if (!kv.ContainsKey("RootClientType") || !Int32.TryParse(kv["RootClientType"], out parentClientTypeID))
{
return null;
}
//这里就可以放你的数据库相关代码,比如把一级客户类别从数据库取出来然后存放在一个数组中
//Model.ClientType是我建的一个实体类,其中有ClientTypeName,ClientTypeID,ParentClientTypeID几个属性
//Model.ClientType model = new Model.ClientType();
//Model.ClientType[] models = new Model.ClientType[];
//当然你也可以使用DataSet、DataTabel等,在foreach那边把列表需要的值填充进去就好
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
foreach (Model.ClientType model in models)
{
values.Add(new CascadingDropDownNameValue(model.ClientTypeName, model.ClientTypeID.ToString()));
}
return values.ToArray();
}
}
/// <summary> /// ClientType Ajax下拉列表数据服务(注意代码中的[]是全角,使用的时候替换成半角的) /// </summary> [WebService(Namespace = "http:tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] //<-这段必须得存在 public class ClientTypeCascadingDropDown : System.Web.Services.WebService { [WebMethod] //一级客户类别相关的WebService方法 public CascadingDropDownNameValue[] ClientTypeRootList(string knownCategoryValues,string category) //<-除了ClientTypeRootList这个方法名可变动,其它不能变动 { StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-这段也得有,呵呵 //这里就可以放你的数据库相关代码,比如把一级客户类别从数据库取出来然后存放在一个数组中 //因为这里是一级客户的下拉列表,所以不用去管那个category的值 //Model.ClientType是我建的一个实体类,其中有ClientTypeName,ClientTypeID,ParentClientTypeID几个属性 //Model.ClientType model = new Model.ClientType(); //Model.ClientType[] models = new Model.ClientType[]; //当然你也可以使用DataSet、DataTabel等,在foreach那边把列表需要的值填充进去就好 //下以部分是下拉列表填充代码 List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); foreach (Model.ClientType model in models) { values.Add(new CascadingDropDownNameValue(model.ClientTypeName,model.ClientTypeID.ToString())); } return values.ToArray(); } [WebMethod] //二级客户类别相关的WebService方法 public CascadingDropDownNameValue[] ClientTypeSubList(string knownCategoryValues, string category) //<-除了ClientTypeRootList这个方法名可变动,其它不能变动 { StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //<-这段也得有,呵呵 //二级客户的下拉列表,得取得一级的category的值,以下代码是判断上级列表的category值,存在或不是空的话把值赋给parentClientTypeID //RootClientType是一级CascadingDropDown设置的category属性名称 int parentClientTypeID; if (!kv.ContainsKey("RootClientType") || !Int32.TryParse(kv["RootClientType"], out parentClientTypeID)) { return null; } //这里就可以放你的数据库相关代码,比如把一级客户类别从数据库取出来然后存放在一个数组中 //Model.ClientType是我建的一个实体类,其中有ClientTypeName,ClientTypeID,ParentClientTypeID几个属性 //Model.ClientType model = new Model.ClientType(); //Model.ClientType[] models = new Model.ClientType[]; //当然你也可以使用DataSet、DataTabel等,在foreach那边把列表需要的值填充进去就好 List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); foreach (Model.ClientType model in models) { values.Add(new CascadingDropDownNameValue(model.ClientTypeName, model.ClientTypeID.ToString())); } return values.ToArray(); } }
基本上一个CascadingDropDown控件就会应对一个Webserivce的方法,如果再有第三个,第四个CascadingDropDown,按ClientTypeSubList为第三个,第四个CascadingDropDown添加对应WebService方法
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Collections.Specialized;
using AjaxControlToolkit;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// SNWebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
//该源码下载自www.51aspx.com(51aspx.com)
public class SNWebService : System.Web.Services.WebService
{
public SNWebService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
/// <summary>
/// 获取省份数据
/// </summary>
/// <param name="knownCategoryValues"></param>
/// <param name="category"></param>
/// <returns></returns>
[WebMethod]
public CascadingDropDownNameValue[] GetProvinceContents(string knownCategoryValues, string category)
{
List<CascadingDropDownNameValue> provinceList = new List<CascadingDropDownNameValue>();
string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
string strSql = "Select * From Province";
SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);
SqlDataReader dtrProvince = sqlCmd.ExecuteReader();
while (dtrProvince.Read())
{
provinceList.Add(new CascadingDropDownNameValue(dtrProvince["Name"].ToString(),dtrProvince["Code"].ToString()));
}
dtrProvince.Close();
sqlConn.Close();
return provinceList.ToArray();
}
/// <summary>
/// 获取市数据
/// </summary>
/// <param name="knownCategoryValues"></param>
/// <param name="category"></param>
/// <returns></returns>
[WebMethod]
public CascadingDropDownNameValue[] GetCityContents(string knownCategoryValues, string category)
{
StringDictionary provinceList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
string strSql = "Select * From city Where ProvinceId='" + provinceList["Province"] + "'";
SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);
SqlDataReader dtrCity = sqlCmd.ExecuteReader();
List<CascadingDropDownNameValue> cityList = new List<CascadingDropDownNameValue>();
while (dtrCity.Read())
{
cityList.Add(new CascadingDropDownNameValue(dtrCity["Name"].ToString(), dtrCity["code"].ToString()));
}
dtrCity.Close();
return cityList.ToArray();
}
/// <summary>
/// 获取乡镇数据
/// </summary>
/// <param name="knownCategoryValues"></param>
/// <param name="category"></param>
/// <returns></returns>
[WebMethod]
public CascadingDropDownNameValue[] GetViliageContents(string knownCategoryValues, string category)
{
StringDictionary cityList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
string strSql = "Select * From area Where CityId='" + cityList["City"] + "'";
SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);
SqlDataReader dtrViliage = sqlCmd.ExecuteReader();
List<CascadingDropDownNameValue> viliageList = new List<CascadingDropDownNameValue>();
while (dtrViliage.Read())
{
viliageList.Add(new CascadingDropDownNameValue(dtrViliage["Name"].ToString(), dtrViliage["id"].ToString()));
}
dtrViliage.Close();
return viliageList.ToArray();
}
}
在Web.config中注册用户控件和自定义控件的优越性
在ASP.NET 的早先版本里,开发人员通过在页面的顶部添加 <%@ Register %> 指令来引入和使用自定义服务器控件和用户控件时,象这样:
<%@ Register TagPrefix="scott" TagName="header" Src="Controls/Header.ascx" %>
<%@ Register TagPrefix="scott" TagName="footer" Src="Controls/Footer.ascx" %>
<%@ Register TagPrefix="ControlVendor" Assembly="ControlVendor" %>
<html>
<form id="form1" runat="server">
<scott:header ID="MyHeader" runat="server" />
</form>
</body>
</html>
注意到上面的前两个注册指令是用来注册用户控件的(是在.ascx文件里实现的),最后这个是用来注册编译进一个程序集 .dll 文件里的自定义控件的。注册完后,开发人员可以在页面的任何地方用设定好的 tagprefix (标识前缀)和标识符号名( tagname)来声明这些控件。
这行之有效,但管理起来会很痛苦,当你要在你的网站的许多页面上使用控件的话,尤其是,假如你移动了.ascx 文件,需要更新所有的注册声明的话。
解决方案:
ASP.NET 2.0 使得控件声明极其干净而且管理起来极其容易。不用在你的页面上重复这些声明,只要在你的应用的web.config 文件的新的 pages->controls 部分声明一次即可:
<?xml version="1.0"?>
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="scottgu" src="~/Controls/Header.ascx" tagName="header"/>
<add tagPrefix="scottgu" src="~/Controls/Footer.ascx" tagName="footer"/>
<add tagPrefix="ControlVendor" assembly="ControlVendorAssembly"/>
</controls>
</pages>
</system.web>
</configuration>
不知者无罪麽? 抓紧学拉.
asp.net AJAX中的CascadingDropDown控件使用心得、在Web.config中注册用户控件和自定义控件的优越性(本文完毕)
下一篇:ASP.NET性能调优总结
上一篇:ASP.Net2.0的集合操作 --- List?