微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

调用天气预报WebService

添加引用:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

.cs代码

 WeatherWebServiceSoap w = new WeatherWebServiceSoapClient("WeatherWebServiceSoap");
        protected void Page_Load(object sender,EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPro();
                BindCity();
            }
        }
        //province
        protected void BindPro()
        {
            string[] pro = w.getSupportProvince();
            for (int i = 0; i < pro.Length; i++)
            {
                ddlProv.Items.Add(new ListItem(pro[i],pro[i]));
            }
        }
        //city
        protected void BindCity()
        {
            ddlCity.Items.Clear();
            string[] city = w.getSupportCity(ddlProv.SelectedValue);
            for (int i = 0; i < city.Length; i++)
            {
                ddlCity.Items.Add(new ListItem(city[i],city[i]));
            }
        }
        //weather
        protected void BindWeather()
        {
            string city = ddlCity.SelectedValue.ToString().Substring(0,2);
            //Response.Write(city);
            string[] mystr = w.getWeatherbyCityName(city);
            //for (int i = 0; i < mystr.Length; i++)
            //{
            //  Response.Write(mystr[8]+"<br/>");
              //img.ImageUrl = "images/a_" + mystr[8];
            //}
              //mystr[0] 省
              //mystr[2] 城市后的代码
           
            labCity.Text = mystr[1];//城市
            Label2.Text = mystr[6];//日期+天气情况(如:7月4日 多云)
            Label3.Text = mystr[5];//温度(如:28℃/34℃)
            Label4.Text = mystr[4];//时间(如:2012-7-4 12:04:38)
        }

        protected void ddlProv_SelectedindexChanged(object sender,EventArgs e)
        {
            BindCity();
        }

        protected void Button1_Click(object sender,EventArgs e)
        {
            BindWeather();
        }

 

HTML代码

<body>     <form id="form1" runat="server">     <div>         <asp:DropDownList ID="ddlProv" runat="server" AutopostBack="True" onselectedindexchanged="ddlProv_SelectedindexChanged"></asp:DropDownList>         <asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>         <asp:Label ID="labCity" runat="server" Text="Label"></asp:Label>         <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>         <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>         <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>         <br />         <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="获 取" />     </div>     </form> </body>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐