国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET Ajax級聯(lián)DropDownList實現(xiàn)代碼

2020-01-18 01:27:02
字體:
來源:轉載
供稿:網(wǎng)友

了解級聯(lián)DDL
那么考慮以下幾種常見情景:
? 用戶注冊時需要選擇國家、省、市、地區(qū)等。
? 用戶購買產(chǎn)品時選擇產(chǎn)品類別、產(chǎn)品名稱、產(chǎn)品型號。
以上的例子有一些共同特點:
? 上一級(如?。┻x擇后下一級(如市)才可以選擇。
? 下一級的內容由上一級的內容決定。
像這樣的一組DropDownList就是級聯(lián)DDL.常見的解決方法是將帶有層次的數(shù)據(jù)寫入XML,然后設置DropDownList的AutoPostBack屬性為"True"開啟自動回調,最后處理SelectedIndexChanged事件。這樣不僅十分麻煩,過多的頁面刷新會給用戶帶來反感。那么如何實現(xiàn)無刷新的級聯(lián)DropDownList呢?
開始
一、 創(chuàng)建XML數(shù)據(jù)文件
比如,我想做用戶注冊時的省、市的級聯(lián)DDL, 那么首先建立以下XML文件。
復制代碼 代碼如下:

<?xmlversion="1.0"encoding="utf-8"?>
<CityServiceSource>
<areaname="中國">
<provinceID="1"provinceID="110000"name="北京市">
<cityCityID="110100"name="市轄區(qū)">
<PieceareaPieceareaID="110101"name="東城區(qū)" />
<PieceareaPieceareaID="110102"name="西城區(qū)" />
<PieceareaPieceareaID="110103"name="崇文區(qū)" />
<PieceareaPieceareaID="110104"name="宣武區(qū)" />
<PieceareaPieceareaID="110105"name="朝陽區(qū)" />
<PieceareaPieceareaID="110106"name="豐臺區(qū)" />
<PieceareaPieceareaID="110107"name="石景山區(qū)" />
<PieceareaPieceareaID="110108"name="海淀區(qū)" />
<PieceareaPieceareaID="110109"name="門頭溝區(qū)" />
<PieceareaPieceareaID="110111"name="房山區(qū)" />
<PieceareaPieceareaID="110112"name="通州區(qū)" />
<PieceareaPieceareaID="110113"name="順義區(qū)" />
<PieceareaPieceareaID="110114"name="昌平區(qū)" />
<PieceareaPieceareaID="110115"name="大興區(qū)" />
<PieceareaPieceareaID="110116"name="懷柔區(qū)" />
<PieceareaPieceareaID="110117"name="平谷區(qū)" />
</city>
<cityCityID="110200"name="縣">
<PieceareaPieceareaID="110228"name="密云縣" />
<PieceareaPieceareaID="110229"name="延慶縣" />
</city>
</province>
</area>
<areaname="英國">
</area>
<areaname="美國">
</area>
<areaname="日本">
</area>
</CityServiceSource>

二、 創(chuàng)建web service
創(chuàng)建web service(如CityService.asmx)
復制代碼 代碼如下:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
publicclassCityService : System.Web.Services.WebService
{
privatestaticXmlDocument _document; // 用來讀取XML數(shù)據(jù)
privatestaticobject _lock = newobject();// 多線程并發(fā)處理
publicstaticXmlDocument Document
{
get
{
lock (_lock)
{
if (_document == null)
{
_document = newXmlDocument();
_document.Load(HttpContext.Current.Server.MapPath("~/App_Data/CityServiceSource.xml"));
}
}
return _document;
}
}
publicstaticstring[] Hierarchy
{
get
{
returnnewstring[] { "area", "province"};// XML數(shù)據(jù)的層次
}
}
[WebMethod] //一會兒控件會自動調用的web method.這個函數(shù)不根據(jù)具體情況改變。
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
{
StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category);
}
}

三、創(chuàng)建DLL控件
如果沒有安裝Ajax Control Toolkit去下載并安裝(http://asp.net)。
創(chuàng)建三個標準的DropDownList(默認命名為DropDownList1、DropDownList2、DropDownList3).
然后在Ajax Control Toolkit中拖拽出三個CascadingDropDown控件,注意一個Extender只能對于一個標準控件。 
復制代碼 代碼如下:

<ajaxToolkit:CascadingDropDownID="CascadingDropDown1"runat="server"
ServiceMethod="GetDropDownContents"
ServicePath="~/webservices/cityservice.asmx"TargetControlID="DropDownList1"
Category="area"LoadingText="正在讀取..."PromptText="請選擇國家">
</ajaxToolkit:CascadingDropDown>
<ajaxToolkit:CascadingDropDownID="CascadingDropDown2"runat="server"
ParentControlID="DropDownList1"ServiceMethod="GetDropDownContentsPageMethod"
TargetControlID="DropDownList2"Category="province"LoadingText="正在讀取..."
PromptText="請選擇省">
</ajaxToolkit:CascadingDropDown>
<ajaxToolkit:CascadingDropDownID="CascadingDropDown3"runat="server"
ParentControlID="DropDownList2"ServiceMethod="GetDropDownContents"
ServicePath="~/webservices/cityservice.asmx"TargetControlID="DropDownList3"
Category="city"LoadingText="正在讀取..."PromptText="請選擇城市">
</ajaxToolkit:CascadingDropDown>
<asp:UpdatePanelID="UpdatePanel1"runat="server"UpdateMode="Conditional"RenderMode="inline">
<Triggers>
<asp:AsyncPostBackTriggerControlID="DropDownList3"EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>

在”.cs”文件中創(chuàng)建web method.
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
publicstaticCascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category)
{
returnnewCityService().GetDropDownContents(knownCategoryValues, category);
}
下面分別對CascadingDropDown的各個屬性進行說明。
ServiceMethod="GetDropDownContents" 調用的web method
ServicePath="~/webservices/cityservice.asmx" web service地址
TargetControlID="DropDownList1" 與其綁定的DropDownList控件的ID
Category="area" 該級聯(lián)DDL的層次
LoadingText="正在讀取..." 加載時顯示的文字
PromptText="請選擇國家"> 未選擇時顯示的文字
可以說Ajax在UE(User Experience)帶來了革命性的變化。異步的刷新模式大大改進了傳統(tǒng)“一步一刷新”的尷尬局面。由于本人修為尚淺,如有錯誤歡迎批評指證。
by Kim
2008/12/11
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 揭西县| 黔东| 淳化县| 枣阳市| 遵化市| 镇康县| 桂阳县| 淮北市| 曲沃县| 安康市| 磴口县| 尤溪县| 阿尔山市| 秦皇岛市| 东丰县| 新绛县| 开阳县| 伊金霍洛旗| 云浮市| 平凉市| 丽水市| 两当县| 哈尔滨市| 油尖旺区| 枞阳县| 宝应县| 开化县| 宁陵县| 靖安县| 余姚市| 新乐市| 彩票| 聂拉木县| 会东县| 弋阳县| 旌德县| 茂名市| 和静县| 博客| 昌吉市| 古交市|