📄 weatherwebpart.cs
字号:
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 + '°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(" ");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -