Lấy thông tin thời tiết từ VnExpress với ASP.NET potx

3 575 3
Lấy thông tin thời tiết từ VnExpress với ASP.NET potx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Lấy thông tin thời tiết từ VnExpress với ASP.NET Bài trước chúng ta đã lấy được thông tin thời tiết từ VnExpress bằng Javascript Ajax, trong bài này chúng ta hãy thử lấy chúng về bằng ASP.NET xem sao > Tạo box lấy thông tin thời tiết đơn giản từ VnExpress do file thời tiết của VnExpress là XML vì vậy chúng ta sẽ sử dụng lớp XmlTextReader để lấy dữ liệu về dạng XML B1. Sau khi các bạn taọ Project trong file Default.aspx chúng ta kéo thả 1 ScriptManager 1 UpdatePanel và 1 DropDownList mục đích là để sử dụng Ajax của ASP.NET để Website của chúng ta không bị load lại khi bạn thay chọn Item ở DropDownList ? code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Value="SonLa">Sơn La</asp:ListItem> <asp:ListItem Value="Haiphong">Hải Phòng</asp:ListItem> <asp:ListItem Value="Hanoi" Selected="True">Hà Nội</asp:ListItem> <asp:ListItem Value="Vinh">Vinh</asp:ListItem> <asp:ListItem Value="Danang">Đà Nẵng</asp:ListItem> <asp:ListItem Value="Nhatrang">Nha Trang</asp:ListItem> <asp:ListItem Value="Pleicu">Pleiku</asp:ListItem> <asp:ListItem Value="HCM">Tp HCM</asp:ListItem> <asp:ListItem Value="Viettri">Việt Trì</asp:ListItem> </asp:DropDownList> <div id="divWeather" runat="server"></div> </ContentTemplate> </asp:UpdatePanel> </div> </form> B2: Các bạn ấn F7 để vào code behide default.aspx.cs các bạn viết một hàm GetWeather và hàm này trả về String, tham số nhận vào cũng là 1 string mục đích string City truyền vào hàm để chúng ta sẽ ghép chuỗi để thành link của VnExpress http://vnexpress.net/ListFile/Weather/Hanoi.xml http://vnexpress.net/ListFile/Weather/Danang.xml http://vnexpress.net/ListFile/Weather/HCM.xml ? code 1 2 3 4 5 public string GetWeather(string City) { string strWrite = ""; XmlTextReader reader = null; try Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 { string AdImg = ""; string AdImg1 = ""; string AdImg2 = ""; string Weather = ""; reader = new XmlTextReader("http://vnexpress.net/ListFile/Weather/" + City + ".xml"); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(reader); XmlNodeList nodelist = xmldoc.SelectNodes("Item"); XmlNode nodedetail; nodedetail = nodelist.Item(0).SelectSingleNode("AdImg"); AdImg = nodedetail.InnerText; nodedetail = nodelist.Item(0).SelectSingleNode("AdImg1"); AdImg1 = nodedetail.InnerText; nodedetail = nodelist.Item(0).SelectSingleNode("AdImg2"); AdImg2 = nodedetail.InnerText; nodedetail = nodelist.Item(0).SelectSingleNode("Weather"); Weather = nodedetail.InnerText; strWrite += "<img src='http://blog.meotom.net/images/Weather/" + AdImg + "' border='0' width='36' height='35' />&nbsp;"; strWrite += "<img src='http://blog.meotom.net/images/Weather/" + AdImg1 + "' border='0' width='19' height='28' />"; strWrite += "<img src='http://blog.meotom.net/images/Weather/" + AdImg2 + "' border='0' width='19' height='28' />"; strWrite += "<img src='http://blog.meotom.net/images/Weather/c.gif ' width='35' height='28' /><br />"; strWrite += Weather; } catch (Exception ex) { strWrite = ex.Message; } finally { reader.Close(); } return strWrite; } trong sự kiện Page_Load chúng ta viết ? code 1 2 3 4 protected void Page_Load(object sender, EventArgs e) { divWeather.InnerHtml = GetWeather("HaNoi"); } với sự kiện Page_Load như trên thì mỗi khi trang Web được load chúng ta sẽ mặc định lấy thời tiết của Hà Nội lên đầu, để thay đổi sang thành phố khác các bạn thay divWeather.InnerHtml = GetWeather("HaNoi"); divWeather.InnerHtml = GetWeather("HCM"); Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. divWeather.InnerHtml = GetWeather("Danang"); divWeather.InnerHtml = GetWeather("HaiPhong"); Sự kiện khi DropDownList được chọn ? code 1 2 3 4 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { divWeather.InnerHtml = GetWeather(DropDownList1.SelectedValue); } Download Demo Project tại đây Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. . Lấy thông tin thời tiết từ VnExpress với ASP. NET Bài trước chúng ta đã lấy được thông tin thời tiết từ VnExpress bằng Javascript Ajax, trong bài này chúng ta hãy thử lấy chúng về bằng ASP. NET. ASP. NET xem sao > Tạo box lấy thông tin thời tiết đơn giản từ VnExpress do file thời tiết của VnExpress là XML vì vậy chúng ta sẽ sử dụng lớp XmlTextReader để lấy dữ liệu về dạng XML B1 Trang< /asp: ListItem> < ;asp: ListItem Value="Pleicu">Pleiku< /asp: ListItem> < ;asp: ListItem Value="HCM">Tp HCM< /asp: ListItem> < ;asp: ListItem

Ngày đăng: 21/06/2014, 09:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan