<html xmlns="<head runat="server">
<title>天氣情況</title>
</head>
<body>
<form id="form1" runat="server">
<table style="width:100%; background-color: #CCFFFF;" cellpadding="0"
cellspacing="0">
<tr>
<td style="line-height: 80px; font-size: large; color: #FF0000; height: 80px;"
colspan="2" align="center">
國(guó)內(nèi)外主要城市3天氣天氣預(yù)報(bào)實(shí)例
</td>
</tr>
<tr>
<td style=" font-size: medium; font-weight: bolder; width: 189px;">
選擇省/洲:<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
</td>
<td style=" font-size: medium; font-weight: bolder;">
選擇城市:<asp:DropDownList ID="DropDownList2" runat="server"
onselectedindexchanged="DropDownList2_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 189px">
</td>
<td align="right">
<asp:Label ID="Label7" runat="server" Text="Label" ForeColor="Red" ></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td style=" font-size: medium; font-weight: bolder;">
今日實(shí)況</td>
<td style="height: 22px">
<asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td style=" font-size: medium; font-weight: bolder;">
天氣預(yù)報(bào)(今天)</td>
<td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Image ID="Image2" runat="server" />
<asp:Image ID="Image3" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td style=" font-size: medium; font-weight: bolder;">
今天指數(shù)</td>
<td>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td style=" font-size: medium; font-weight: bolder;">
天氣預(yù)報(bào)(明天)</td>
<td>
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
<asp:Image ID="Image4" runat="server" />
<asp:Image ID="Image5" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td style=" font-size: medium; font-weight: bolder;">
天氣預(yù)報(bào)(后天)</td>
<td>
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
<asp:Image ID="Image6" runat="server" />
<asp:Image ID="Image7" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td style=" font-size: medium; font-weight: bolder;">
城市介紹</td>
<td>
</td>
</tr>
<tr>
<td style="width: 189px">
<asp:Image ID="Image1" runat="server" Height="130px" Width="210px" />
</td>
<td>
<asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td style="width: 189px">
</td>
<td>
</td>
</tr>
<tr>
<td style="width: 189px">
</td>
<td align="right">
預(yù)報(bào)時(shí)間:<asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
2.在default1.aspx.cs文件里面輸入以下代碼:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using cn.com.webxml.www; //注意此處就是引用web 服務(wù)的引用申明,注意不要把App_WebReferences加入進(jìn)去
//并且注意cn.com.webxml.www是倒過(guò)來(lái)寫的,相對(duì)于網(wǎng)址而言,但是是符合文件夾的層次關(guān)系的
public partial class weathers : System.Web.UI.Page
{
WeatherWebService weatherClass = new WeatherWebService();
protected void Page_Load(object sender, EventArgs e)
{
try
{
DataSet ds = weatherClass.getSupportDataSet();
if (!IsPostBack)
{
DataTable dt = ds.Tables[0];
DropDownList1.DataSource = dt;
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Zone";
DropDownList1.DataBind();
DropDownList2.SelectedIndex = 1;
CityDataBind("1");
GetWeatherByCode("58367");
}
}
catch (Exception exp)
{
Console.WriteLine("Error"); ;
}
}
protected void CityDataBind(string zoneID)
{
DataView dv = new DataView(weatherClass.getSupportDataSet().Tables[1]);
dv.RowFilter = "[ZoneID]=" + zoneID;
DropDownList2.DataSource = dv;
DropDownList2.DataTextField = "Area";
DropDownList2.DataValueField = "AreaCode";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("選擇城市", "0"));
DropDownList2.SelectedIndex = 0;
}
protected void GetWeatherByCode(string cityCode)
{
String[] wa = weatherClass.getWeatherbyCityName(cityCode.Trim());
Label9.Text = wa[10];
Label1.Text = wa[6] + " " + wa[5] + " " + wa[7];
Label4.Text = wa[13] + " " + wa[12] + " " + wa[14];
Label5.Text = wa[18] + " " + wa[17] + " " + wa[19];
Label3.Text = wa[11].Replace("\n", "<br/>");
Label6.Text = wa[22].Replace("\n", "<br/>");
Label8.Text = DateTime.Parse(wa[4]).ToString("yyyy年MM月dd日 HH:mm");
Label7.Text = wa[0] + "/" + wa[1];
Image2.ImageUrl = "~/img/weather/" + wa[8];
Image3.ImageUrl = "~/img/weather/" + wa[9];
Image4.ImageUrl = "~/img/weather/" + wa[15];
Image5.ImageUrl = "~/img/weather/" + wa[16];
Image6.ImageUrl = "~/img/weather/" + wa[20];
Image7.ImageUrl = "~/img/weather/" + wa[21];
Image1.ImageUrl = "Image1.AlternateText = DropDownList2.SelectedItem.Text;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
CityDataBind(DropDownList1.SelectedItem.Value.Trim());
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList2.Items[0].Value == "0")
{
DropDownList2.Items.RemoveAt(0);
}
GetWeatherByCode(DropDownList2.SelectedItem.Value.Trim());
}
}
好的,這里就實(shí)驗(yàn)成功了。調(diào)試運(yùn)行一下,一定成功了吧!截圖如下:
注:這里需要注意的是中間的一些圖片需要自己加到解決方案里去。