CascadingDropDown控件
添加一个WebService.asmx ?添加标签:[System.Web.Script]
?
Get { ??? Lock(objectLock)//在lock里是一个临界区,在这个临界区只能有一个线程,要有其它线程调用必须等这个线程释放才可以.由private static XmlDocument xmlDoc;进行线程程操作 { ?????? xmlDoc.Load(HttpContext.Current.Server.MapPath(“~/App_Data/XML.xml”)// 加载文件 } return xmlDoc; }
?
public static string[] Hierarch//用于返回交节点和子节点// { ?????? get{return? new string[] {“province”,”city”};}
?
} public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knowCategoryValues,string category) //province name=”江苏,values值就是江苏,category 就是province {//StringDictionary需要using System.Collections.Specialized ??? StringDictionary knownCataegoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParaseKnowCategoryValuesString(knowCategoryValues);//将value值转换成StringDictionary类的一个 }
?
三个DropDownList 中的CascadingDropDown1,2,3(CascadingDropDown) Category:对应着的1,3分别填province,city,county LoadingText:正在加载 ParentControlID:第一个CascadingDropDown1不用写,这里实现一个连动效果,当选择省的时候,第二个就会出现相应的市,第三个出现县 ??? 所以第二个与第三个分别选择DropDownList1,DropDownList2 PromptText分别真请选择省,请选择市,请选择县 ServiceMethod:都填写GetDropDownContents ServicePath:都填写 WebService.asmx
?
更UpdatePanel属性的Triggers(Collection)添加,更改ControlID为DropDownList3,EventName为SelectedIndexChange这个是触发事件
?
UpdatePanel1的RenderMode更改为Inline是内嵌式的,UpdateMode选择Conditional 编写DropDownList3_SelectedIndexChanged(object sender,EventArgs e) 在Deault.aspx中的第一个<>中加入EnableEventValidation=”false”否则Lable1会出错,而且还要修改第三DropDownList选择它的小三角AutoPostPack画勾
?
代码如下 : Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
?
public partial class _Default : System.Web.UI.Page { ??? protected void Page_Load(object sender,EventArgs e) ??? {
?
??? } ??? protected void DropDownList3_SelectedIndexChanged(object sender,EventArgs e) ??? { ??????? if (DropDownList3.SelectedItem.Text == "") ??????? { ??????????? this.Label1.Text = "您还有一项没有选择哦!"; ??????? } ??????? else
?
????? ??{ ?????????? Label1.Text = "您的籍贯是" + DropDownList1.SelectedItem.Text+ DropDownList2.SelectedItem.Text + DropDownList3.SelectedItem.Text; ??????? } ??? } }
?
WebService.cs using System; using System.Web; using System.Collections.Specialized; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml;
?
/// <summary> /// WebService 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService]//添加标签,从客户端调用服务端WebService方法时 public class WebService : System.Web.Services.WebService {
?
??? public WebService() ??? {
?
??????? //如果使用设计的组件,请取消注释以下行 ??????? //InitializeComponent(); ??? } ??? private static XmlDocument xmlDoc; ??? private static object objectLock = new object();
?
?
??? public static XmlDocument Document ??? { ??????? get ??????? { ??????????? lock (objectLock)//在lock里是一个临界区,要有其它线程调用必须等这个线程释放才可以.由private static XmlDocument xmlDoc;进行线程程操作 ??????????? { ??????? ????????if (xmlDoc == null) ??????????????? { ??????????????????? xmlDoc = new XmlDocument(); ??????????????????? xmlDoc.Load(HttpContext.Current.Server.MapPath("~/App_Data/XML.xml"));// 加载文件 ??????????????? } ??????????? } ??????????? return xmlDoc; ???? ???} ??? }
?
??? public static string[] Hierarchy ??? { ??????? get { return new string[] { "province","city" }; } //用于返回交节点和子节点 ??? } ??? [WebMethod] ??? public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues,string category) ??? //province name=江苏,category就是province ??? { ??????? //StringDictionary需要using System.Collections.Speciallized ??????? StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);//将value值转换成StringDictionary类的一个 ??????? return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document,Hierarchy,knownCategoryValuesDictionary,category);//返回XML上的信息显示到界面上来
?
?
??? }
?
?
}
?
XML.xml <?xml version="1.0" encoding="utf-8"?> <WebServer> ? <!--根节点 --> ? <province name="江苏"><!—也可以通过单击右键 查看数据风格 打开一个表进行数据添加 --> ??? <city name="盐城市"> ????? <country name="建湖县" /> ????? <country name="盐城县" /> ??? </city> ??? <city name="南通市"> ????? <country name="海安县" /> ????? <country name="如东县" /> ??? </city> ??? <city name="扬州市"> ????? <country name="宝应县" /> ??? </city> ? </province> ? <province name="广东省"> ??? <city name="潮州市"> ????? <country name="潮安县"> ????? </country> ??? </city> ??? <city name="河源市"> ????? <country name="东源县"> ????? </country> ??? </city> ? </province> </WebServer> Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="False"? %>
?
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> ??? <title>Untitled Page</title> </head> <body style="text-align: center"> ??? <form id="form1" runat="server"> ??????? <asp:ScriptManager ID="ScriptManager1" runat="server" /> ??????? <table border="1"> ??????????? <tr> ??????????????? <td style="width: 100px"> ??????????????????? 省</td> ??????????????? <td style="width: 100px"> ??????????????????? <asp:DropDownList ID="DropDownList1" runat="server" Width="100px"> ??????????????????? </asp:DropDownList></td> ??????????? </tr> ??????????? <tr> ??????????????? <td style="width: 100px"> ??????????????????? 市</td> ??????????????? <td style="width: 100px"> ??????????????????? <asp:DropDownList ID="DropDownList2" runat="server" Width="100px"> ??????????????????? </asp:DropDownList></td> ??????????? </tr> ??????????? <tr> ??????????????? <td style="width: 100px"> ??????????????????? 县</td> ??????????????? <td style="width: 100px"> ??????????????????? <asp:DropDownList ID="DropDownList3" runat="server" Width="100px" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="True"> ??????????????????? </asp:DropDownList></td> ??????????? </tr> ??????? </table> ??????? <div> ??????????? <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1" Category="province" LoadingText="正在加载...." PromptText="请选择省份" ServiceMethod="GetDropDownContents"? ServicePath="WebService.asmx"> ??????????? </cc1:CascadingDropDown> ??????? </div> ??????? <cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2" Category="city" ParentControlID="DropDownList1" PromptText="请选择城市" ServiceMethod="GetDropDownContents" ServicePath="WebService.asmx" LoadingText="正在加载...."> ??????? </cc1:CascadingDropDown> ??????? <cc1:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3" Category="country" ParentControlID="DropDownList2" PromptText="请选择县" ServiceMethod="GetDropDownContents" ServicePath="WebService.asmx" LoadingText="正在加载..."> ??????? </cc1:CascadingDropDown> ??????? <asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Inline" UpdateMode="Conditional"> ??????????? <ContentTemplate> ??????????????? <asp:Label ID="Label1" runat="server" Text="还未选择"></asp:Label> ??????????? </ContentTemplate> ??????????? <Triggers> ??????????????? <asp:AsyncPostBackTrigger ControlID="DropDownList3" EventName="SelectedIndexChanged" /> ??????????? </Triggers> ??????? </asp:UpdatePanel> ??? </form> </body> </html> ?(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |