添加引用: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>

posted on 2013-04-08 14:47  骑牛射雕  阅读(248)  评论(0编辑  收藏  举报