通过IP查询客户端用户所在地的天气预报

此方法实现调用了网上公开的webservices

天气预报webservices http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

IP库webservices http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx

前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Weater._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>天气预报</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:DropDownList ID="ddlPro" runat="server" onselectedindexchanged="ddlPro_SelectedIndexChanged" AutoPostBack="true"> </asp:DropDownList> <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="True" onselectedindexchanged="ddlCity_SelectedIndexChanged"> </asp:DropDownList> <div> 省份:<asp:Label ID="LbProvince" runat="server" Text="Label"></asp:Label><br /> 城市:<asp:Label ID="LbCity" runat="server"></asp:Label><br /> 当前时间:<asp:Label ID="LbTime" runat="server"></asp:Label><br /> 当前天气:<asp:Label ID="LbThisWeather" runat="server"></asp:Label><asp:Image ID="Image1" runat="server" /><asp:Image ID="Image2" runat="server" /> </div> </ContentTemplate> </asp:UpdatePanel> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> </form> </body> </html>

后台代码

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 Weater.cn.com.webxml.www; using System.Net; using Weater.cn.com.webxml.www1; using System.Web.UI.MobileControls; using System.Collections.Generic; namespace Weater { public partial class _Default : System.Web.UI.Page { /// <summary> /// 调用天气预报的类 /// </summary> WeatherWebService ww = new WeatherWebService(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ww = new WeatherWebService(); DataSet ds = ww.getSupportDataSet(); DataView dv = ds.Tables[0].DefaultView; dv.RowFilter = "ID<=30"; ddlPro.DataSource = dv; ddlPro.DataTextField = "Zone"; ddlPro.DataValueField = "ID"; ddlPro.DataBind(); ddlPro.Items.Insert(0, new ListItem("请选择省份", "")); ddlCity.Items.Insert(0, new ListItem("请选择城市", "")); } } private void DDlCityBind(int pid) { ww = new WeatherWebService(); DataSet ds = ww.getSupportDataSet(); DataView dv = ds.Tables[1].DefaultView; dv.RowFilter = "ZoneID="+pid;//过滤 ddlCity.DataSource = dv; ddlCity.DataTextField = "Area"; ddlCity.DataValueField = "AreaCode"; ddlCity.DataBind(); ddlCity.Items.Insert(0, new ListItem("请选择城市", "")); } protected void ddlPro_SelectedIndexChanged(object sender, EventArgs e) { if (this.ddlPro.SelectedValue != "") { DDlCityBind(Convert.ToInt32(this.ddlPro.SelectedValue)); } } protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e) { if (ddlCity.SelectedItem.Text != "请选择城市") { SetWeather(ddlCity.SelectedItem.Text); } } protected void Button1_Click(object sender, EventArgs e) { DataSet ds = ww.getSupportDataSet(); DataTable dt = ds.Tables[1];//获取所有城市 //调用IP查询接口的类 IpAddressSearchWebService ip = new IpAddressSearchWebService(); string address = ip.getCountryCityByIp(TextBox1.Text)[1]; foreach (DataRow dr in dt.Rows) { if (address.IndexOf(dr["Area"].ToString()) > -1) { SetWeather(dr["Area"].ToString()); break; } } } /// <summary> /// 输出天气 /// </summary> /// <param name="cityName"></param> protected void SetWeather(string cityName) { string[] ss = ww.getWeatherbyCityName(cityName); LbProvince.Text = ss[0].ToString(); LbTime.Text = ss[4].ToString() + " " + ss[5].ToString() + " " + ss[6].ToString() + " " + " " + ss[7].ToString() + " " + ss[10].ToString(); LbCity.Text = ss[1].ToString(); Image1.ImageUrl = "~/images/weather/" + ss[8].ToString(); Image2.ImageUrl = "~/images/weather/" + ss[9].ToString(); } } }

 

 

下载地址 http://download.csdn.net/source/2158337

你可能感兴趣的:(server,image,asp,button,textbox,dataset)