用showModalDialog弹出窗口和UltraWebTree树实现的行政区域(省,市,县)选择

行政区域的省,市,县选择一般多用DropDownlist的三级联动就可以了,技术经理说那个不是很好,对于当前的项目。
要做成showModalDialog弹出窗口根据树展开进行选择,然后返回行政区域的全名和地区代码。
实现过程:
新建一个用户控件DistrictSelect.ascx;cs没有代码的

HTMLCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->1<%@ControlLanguage="C#"AutoEventWireup="true"CodeFile="DistrictSelect.ascx.cs"
2Inherits="Common_DistrictSelect"%>
3
4<scripttype="text/javascript"language="javascript">
5functionopenwin()
6{
7varstr=window.showModalDialog('district.aspx','行政区域选择','dialogHeight:800px;dialogWidth:600px;status:yes;scroll:yes;help:no');
8//打开弹出窗口,str用来取district.aspx的返回值
9districtName=str.split(",");
10document.getElementById("<%=txt_district.ClientID%>").value=districtName[0];//行政区域全程
11document.getElementById("<%=hf_district.ClientID%>").value=districtName[1];//行政区域代码
12}

13</script>
14
15<div>
16<asp:TextBoxID="txt_district"runat="server"></asp:TextBox>
17<asp:Buttonrunat="server"ID="btn_select"Text="选择"OnClientClick="openwin()"/>
18<asp:HiddenFieldID="hf_district"runat="server"/>
19</div>
20
接下来是 district.aspx页面了,引入了Anthem
HTMLCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->1<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="district.aspx.cs"Inherits="district"%>
2
3<%@RegisterAssembly="Infragistics2.WebUI.UltraWebNavigator.v7.1,Version=7.1.20071.40,Culture=neutral,PublicKeyToken=7dd5c3163f2cd0cb"
4Namespace="Infragistics.WebUI.UltraWebNavigator"TagPrefix="ignav"%>
5<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6<htmlxmlns="http://www.w3.org/1999/xhtml">
7<headrunat="server">
8<title>无标题页</title>
9<scripttype="text/javascript"language="javascript">
10{
11functiongetTreeNode()
12{
13varname=document.getElementById('btn_search').value;
14
15if(name=="查找")//如果按钮的值查找查询下级节点
16{
17
18Anthem_InvokePageMethod('findTreeNode',[document.getElementById('txt_condition').value],showResult);
19}

20else//判断txt_condition是否为空,不为空将值返回父窗口,关闭当前窗口
21{
22
23vardistrict=document.getElementById('txt_condition').value;
24if(district.Length!=0)
25{//alert(district+document.getElementById('hf_districtCode').value)
26//返回行政名称和代码window.returnValue
27window.returnValue=district+","+document.getElementById('hf_districtCode').value;
28window.close();
29}

30else
31{
32alert('请选择行政区域');
33}

34}

35}

36
37functionshowResult(result)
38{
39
40varstr=result.value.split(",");
41if(str[0]=="0")
42{
43alert("找不到匹配的行政区,请重新输入!");
44}

45else
46{
47vartxt=document.getElementById('txt_condition');
48txt.value=str[0];
49txt.readonly=true;
50document.getElementById("hf_districtCode").value=str[1];
51document.getElementById('btn_search').value="确定";
52}

53}

54
55
56}

57</script>
58
59</head>
60<body>
61<formid="form1"runat="server">
62<div>
63<spanstyle="font-size:12pt">请选择行政区域</span>
64<divstyle="width:420px;height:40px;"align="center">
65<inputid="txt_condition"type="text"runat="server"style="width:300px"/>
66<inputid="btn_search"type="button"value="查找"onclick="getTreeNode()"runat="server"/>
67<asp:HiddenFieldID="hf_districtCode"runat="server"/>
68</div>
69
70</div>
71<divstyle="width:150px;font-size:10pt"id="province">
72<ignav:UltraWebTreeID="treeDistrict"runat="server"DefaultImage=""HoverClass=""
73Indentation="20"Height="100%"Width="100%"OnNodeClicked="treeDistrict_OnNodeClicked">
74</ignav:UltraWebTree>
75</div>
76</form>
77</body>
78</html>
79
Cs里面
csCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->1publicpartialclassdistrict:System.Web.UI.Page
2{
3protectedvoidPage_Load(objectsender,EventArgse)
4{
5Anthem.Manager.Register(this);//anthemManager注册
6if(!Page.IsPostBack)
7{
8District[]dis=BaseDistrictMng.GetAllProvinces();
9foreach(Districtprovinceindis)//加载省级行政单位
10{
11NoderootNode=newNode();
12rootNode.DataKey=province.DistrictCode;
13rootNode.Text=province.DistrictName;
14rootNode.ToolTip=province.DistrictName;
15rootNode.ImageUrl="~/images/Grade.gif";
16rootNode.ShowExpand=true;
17rootNode.SelectedImageUrl="~/images/icon_ok.gif";
18treeDistrict.Nodes.Add(rootNode);
19}

20}

21//不重新弹出,这个很重要,否则点击树结点会在showModalDialog上弹出一个district.aspx页面
22Response.Write("<basetarget=_self/>");
23
24}

25
26protectedvoidtreeDistrict_OnNodeClicked(objectsender,WebTreeNodeEventArgse)
27{
28stringdistrictcode=SelectedNode.DataKey.ToString();;
29stringdistrictname=SelectedNode.Text;
30
31District[]dis=BaseDistrictMng.GetLowerDistricts(districtcode);
32if(dis!=null&&dis.Length!=0)//有下级填充节点
33{
34fillTreeNode(SelectedNode,dis);
35this.treeDistrict.SelectedNode.Expand(true);
36}

37else//没有下级,取值
38{
39Districtdd=BaseDistrictMng.GetDistrictById(districtcode);
40stringallName=dd.ParentDistrict.ParentDistrict.DistrictName+dd.ParentDistrict.DistrictName+dd.DistrictName;
41this.txt_condition.Value=allName;
42this.btn_search.Value="确定";
43this.hf_districtCode.Value=districtcode;
44
45}

46}

47//填充子节点
48protectedvoidfillTreeNode(NoderootNode,District[]dis)
49{
50foreach(Districtvarindis)
51{
52Nodenode=newNode();
53node.DataKey=var.DistrictCode;
54node.Text=var.DistrictName;
55node.ToolTip=var.DistrictName;
56node.ImageUrl="~/images/Grade.gif";
57node.SelectedImageUrl="~/images/icon_ok.gif";
58rootNode.Nodes.Add(node);
59}

60rootNode.ShowExpand=true;
61}

62[Anthem.Method]//寻找子节点,实现查询功能
63publicstringfindTreeNode(stringdistrictName)
64{
65District[]disList=BaseDistrictMng.GetDistrictList();
66Districtdis=newDistrict();
67boolisFind=false;
68foreach(DistrictvarindisList)
69{
70if(var.DistrictName==districtName)
71{
72isFind=true;
73dis=var;
74}

75}

76if(isFind)
77{
78stringallName=dis.ParentDistrict.ParentDistrict.DistrictName+dis.ParentDistrict.DistrictName+dis.DistrictName;
79returnallName+","+dis.DistrictCode;
80}

81elsereturn"0";
82}

83
84/**////<summary>
85///选中节点
86///</summary>

87publicNodeSelectedNode
88{
89get{returntreeDistrict.SelectedNode;}
90}

91
92}
然后将 DistrictSelect.ascx注册新建一个页面即可。效果如图


确定之后关闭 showModalDialog窗口将选择值返回到DistrictSelect.ascx用户控件,对于上面完成的 distric.aspx 中 UltraWebTree加载市县级节点时时会刷新的,对于点击县级节点返回行政区域全称和查询功能是不会刷新的,这里感觉就是Anthem的一个不太好的地方,一定要由客户端发起事件,对于UltraWebTree找不到客户端nodeclick也不知如何注册。不过技术经理是很推崇的,呵呵,因为《Ajax与.Net2.0高级程序设计》这本书是他翻译的,里面主要讲的就是Anthem。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值