天气预报经常查,使用Web Service实现
实现步骤:
1、先建一个页面,用table布局,防止2个DropDownList控件和一个Button控件,5个label控件,一个Image控件。
2、在网站根目录右击,在弹出的对话框中选择“添加Web引用”,在URL地址中输入:http://www.ayandy.com/Service.asmx 单击“前往”,在“Web引用名”中输入“App_WebReferences”,点击“添加引用”。
3、代码实现:
显示代码
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 System.Data.SqlClient;
using System.Web.Services; //添加引用
public partial class Index : System.Web.UI.Page
{
App_WebReferences.Service myobj = new App_WebReferences.Service(); //实例化
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //绑定信息
{
BindPro();
BindCity();
BindWeather();
Label5.Text = DateTime.Now.ToShortDateString();
}
}
protected void BindPro() //绑定省份
{
string[] pro = myobj.getSupportProvince();
for (int i = 1; i <= Int32.Parse(pro[0]); i++)
{
DropDownList1.Items.Add(new ListItem(pro[i].ToString(), pro[i].ToString()));
}
}
protected void BindCity() //绑定城市
{
DropDownList2.Items.Clear();
string[] city = myobj.getSupportCity(DropDownList1.SelectedValue);
for (int i = 1; i <= Int32.Parse(city[0]); i++)
{
DropDownList2.Items.Add(new ListItem(city[i].ToString(), city[i].ToString()));
}
}
protected void BindWeather() //绑定天气
{
string[] mystr = myobj.getWeatherbyCityName(DropDownList2.SelectedValue, App_WebReferences.theDayFlagEnum.Today);
Label1.Text = mystr[1].ToString();
Label2.Text = mystr[2].ToString();
Label3.Text = mystr[3].ToString();
Label4.Text = mystr[5].ToString();
Image1.ImageUrl = mystr[6].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
BindPro();
BindCity();
BindWeather();
}
}