里面所使用的服务提供商为:上海思集信息科技有限公司是个免费的天气预报服务接口 Coder 1usingSystem;
2usingSystem.ComponentModel;
3usingSystem.Web.UI;
4usingSystem.Web.UI.WebControls;
5usingSystem.Net;
6usingSystem.IO;
7usingSystem.Xml;
8usingSystem.Text;
9usingSystem.Collections.Generic;
10
11[assembly:WebResource("WeatherReport.image.a_1.gif","image/gif")]
12namespaceWeatherReport
13{
14///<summary>
15///天气预报控件
16///</summary>
17[DefaultProperty("Province")]
18[DefaultEvent("OnWeatherReportLoadDataComplete")]
19[ToolboxData("<{0}:WebWeatherReportShowType=\"Style\"City=\"兰州\"Width=\"100%\"runat=server></{0}:WebWeatherReport>")]
20[System.Drawing.ToolboxBitmap(typeof(WebWeatherReport),"ToolBar.ico")]
21publicclassWebWeatherReport:WebControl
22{
23///<summary>
24///注册服务读取以后的显示行为事件
25///</summary>
26[Description("该事件配合控件呈现方式显示,一般在控件显示为字符串数组时回发结果数组")]
27publiceventEventHandler<OnWeatherReportLoadDataCompleteEventArgs>OnWeatherReportLoadDataComplete;
28///<summary>
29///获取或者设置控件的文本值
30///</summary>
31[Bindable(true)]
32[Category("Appearance")]
33[DefaultValue("")]
34[Localizable(true)]
35publicstringText
36{
37get
38{
39Strings=(String)ViewState["Text"];
40return((s==null)?String.Empty:s);
41}
42
43set
44{
45ViewState["Text"]=value;
46}
47}
48
49///<summary>
50///创建控件
51///</summary>
52///<paramname="output"></param>
53protectedoverridevoidRenderContents(HtmlTextWriteroutput)
54{
55WebClientclient=newWebClient();
56StreaminStream=null;
57if((this.City.Trim()==string.Empty))
58{
59inStream=client.OpenRead(".cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=兰州");
60}
61else
62{
63inStream=client.OpenRead(".cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName="+this.City.ToString().Trim());
64}
65XmlDocumentdocument=newXmlDocument();
66document.Load(inStream);
67XmlNodeListchildNodes=document.DocumentElement.ChildNodes;
68if(childNodes.Count!=0&&childNodes[0].InnerText!="高速访问被限制!")
69{
70switch(this.ShowType)
71{
72caseControlsShowType.Style:
73StringBuildersb=newStringBuilder("<divstyle=\"margin:0auto;font-size:10pt;with:"+base.Width+";height:"+base.Height+";\">");
74sb.Append(String.Format("<spanstyle=\"color:#FF0000;font-family:微软雅黑;\">{0}天气预报,中央气象台{1}预报,</span>",childNodes[1].InnerText,childNodes[4].InnerText));
75sb.Append(String.Format("<spanstyle=\"color:#FF0000;font-family:微软雅黑;\">日期:{0}</span>&nbsp;&nbsp;&nbsp;<imgsrc=\"weather/{1}\"/>/<imgsrc=\"weather/{2}\"/>&nbsp;&nbsp;&nbsp;",childNodes[6].InnerText,childNodes[8].InnerText,childNodes[9].InnerText));
76sb.Append(String.Format("<spanstyle=\"color:#FF0000;font-family:微软雅黑;\">今日气温:{0}</span></div>",childNodes[5].InnerText.Replace("℃","度(℃)")));
77output.Write(sb.ToString());
78break;
79caseControlsShowType.StringByEvents:
80List<String>MyString=newList<string>();
81foreach(XmlNodendinchildNodes)
82{
83MyString.Add(nd.InnerText.ToString().Trim());
84}
85ShowMessage(newOnWeatherReportLoadDataCompleteEventArgs(){IsComplete=true,Result=MyString.ToArray()},output);
86break;
87default:
88thrownewException("未能正确选择显示类型!");
89caseControlsShowType.None:
90StringBuildersbs=newStringBuilder("<divstyle=\"margin:0auto;font-size:10pt;with:"+base.Width+";height:"+base.Height+";display:none;\">");
91sbs.Append(String.Format("<spanstyle=\"color:#FF0000;font-family:微软雅黑;\">{0}天气预报,中央气象台{1}预报</span>",childNodes[1].InnerText,childNodes[4].InnerText));
92sbs.Append(String.Format("<spanstyle=\"color:#FF0000;font-family:微软雅黑;\">日期:{0}</span>&nbsp;&nbsp;&nbsp;<imgsrc=\"weather/{1}\"/>/<imgsrc=\"weather/{2}\"/>",childNodes[6].InnerText,childNodes[8].InnerText,childNodes[9].InnerText));
93sbs.Append(String.Format("<spanstyle=\"color:#FF0000;font-family:微软雅黑;\">今日气温:{0}</span></div>",childNodes[5].InnerText.Replace("℃","度(℃)")));
94output.Write(sbs.ToString());
95break;
96}
97}
98else
99{
100ShowMessage(newOnWeatherReportLoadDataCompleteEventArgs(){IsComplete=false,Result=null},output);
101output.Write("未能获取正确的天气预报预览,因为本控件使用免费的服务器接口,但不影响使用,详情请查看:<ahref=\"TextWriterT)
110{
111if(this.OnWeatherReportLoadDataComplete!=null)
112{
113OnWeatherReportLoadDataComplete(this,e);
114}
115else
116{
117T.Write("要使用字符串形数组式返回的天气预报数据,必须在PageOnLoad事件中手动添加OnWebServiceLoaded事件!");
118}
119}
120///<summary>
121///枚举类型,获取控件显示类型
122///</summary>
123publicenumControlsShowType
124{
125///<summary>
126///无样式
127///</summary>
128None,
129///<summary>
130///原有样式
131///</summary>
132Style,
133///<summary>
134///根据事件返回样式
135///</summary>
136StringByEvents
137}
138///<summary>
139///获取或者设置控件的显示样式
140///</summary>
141[Description("获取控件显示样式")]
142[DefaultValue("Style")]
143[Category("Appearance")]
144[Browsable(true)]
145publicControlsShowTypeShowType{get;set;}
146///<summary>
147///获取或者设置城市名称
148///</summary>
149[Description("获取或者设置城市")]
150[DefaultValue("北京")]
151[Category("Appearance")]
152[Browsable(true)]
153publicstringCity
154{
155get;
156set;
157}
158///<summary>
159///获取或者设置是否是直辖市
160///</summary>
161[Description("获取或者设置城市所在的省份")]
162[DefaultValue("北京")]
163[Category("Appearance")]
164[Browsable(false)]
165publicboolIsMunicipalities
166{
167get;
168set;
169}
170}
171///<summary>
172///为字符数组显示类提供数据基类
173///</summary>
174publicclassOnWeatherReportLoadDataCompleteEventArgs:EventArgs
175{
176///<summary>
177///默认构造函数
178///</summary>
179publicOnWeatherReportLoadDataCompleteEventArgs()
180{
181}
182///<summary>
183///是否已经完成
184///</summary>
185publicBooleanIsComplete{get;internalset;}
186///<summary>
187///数组返回值
188///</summary>
189publicstring[]Result{get;internalset;}
190}
191}
192
posted on 2011-05-24 09:47  专注NET开发  阅读(518)  评论(0编辑  收藏  举报