weatherwebpart.cs

来自「asp.net 2.0 WebPart应用. 使用Web部件创建模块化的Web」· CS 代码 · 共 81 行

CS
81
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace WingtipWebParts
{
	public class WeatherWebPart : WebPart
	{
		public WeatherWebPart()
		{
			this.Title = "Local Weather Report" + ZipCode;
			this.TitleIconImageUrl = @"~\img\Weather.gif";
			this.CatalogIconImageUrl = @"~\img\Weather.gif";
		}

		// The Zip code parameter
		private string _zipCode = string.Empty;

		// Dynamic script code to add to the Web part
		private const string HtmlFormat = @"
            <div id=""weatherView""></div>
            <script id=""weatherScript"" language=""javascript""></script>
            <script language=""javascript"">
            function CreateWeather(sAcid) {{
            	var oData = window['weatherScript'];
            	if (sAcid != '') {{
            		oData.onreadystatechange = ShowWeather;
            		oData.src = 'http://www.msnbc.com/m/chnk/d/weather_d_src.asp?acid=' + sAcid;
            	}}
            }}
            
            function ShowWeather() {{

                if (typeof(makeWeatherObj) != 'undefined') {{
                                var oWea = new makeWeatherObj();
                var sTmp = '<b>Current Conditions</b><hr size=1>' +
                           '<table cellpadding=2 cellspacing=0 border=0><tr><td valign=""middle"">' +
                           '<img src=""http://www.msnbc.com/m/wea/i/36/' + oWea.swCIcon + '.gif"" align=absmiddle></td>' +
                           '<td>'+ oWea.swCity + ', ' + oWea.swSubDiv + '<br>' +
                           + oWea.swTemp + '&#176;F</td></tr></table>' + 
                           '<a href=""http://www.weather.com"">More weather information</a>';
            	document.all['weatherView'].innerHTML = sTmp;
                }}
            }}
            
            CreateWeather('{0}');
            </script>
            ";

		// ************************************************************************
		// Gets and sets the zip code to populate the part
		[Personalizable]
		[WebBrowsable]
		public string ZipCode
		{
			get { return _zipCode; }
			set { _zipCode = value; }
		}
		// ************************************************************************

		// ************************************************************************
		// Gets and sets the Web part collection of connection points
		protected override void RenderContents(HtmlTextWriter writer)
		{
			if ((_zipCode == null) || (_zipCode.Length == 0))
			{
				writer.Write("Select a zip code by personalizing this WebPart");
			}
			else
			{
				writer.Write(String.Format(HtmlFormat, _zipCode));
				writer.Write("&nbsp;");
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?