Xem mẫu

  1. 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 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 9 Sơn La 10 Hải Phòng 11 Hà 12 Nội Vinh 13 Đà Nẵng 14 Nha Trang 15 Pleiku 16 Tp HCM Việt Trì 17 18 19 20 21 22 23 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 public string GetWeather(string City) 1 { 2 string strWrite = ""; 3 XmlTextReader reader = null; 4 5 try
  2. Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. { 6 string AdImg = ""; 7 string AdImg1 = ""; 8 string AdImg2 = ""; 9 string Weather = ""; 10 11 reader = new XmlTextReader("http://vnexpress.net/ListFile/Weather/" + City + ".xm XmlDocument xmldoc = new XmlDocument(); 12 xmldoc.Load(reader); 13 14 XmlNodeList nodelist = xmldoc.SelectNodes("Item"); 15 XmlNode nodedetail; 16 nodedetail = nodelist.Item(0).SelectSingleNode("AdImg"); 17 AdImg = nodedetail.InnerText; nodedetail = nodelist.Item(0).SelectSingleNode("AdImg1"); 18 AdImg1 = nodedetail.InnerText; 19 nodedetail = nodelist.Item(0).SelectSingleNode("AdImg2"); 20 AdImg2 = nodedetail.InnerText; 21 nodedetail = nodelist.Item(0).SelectSingleNode("Weather"); Weather = nodedetail.InnerText; 22 23 strWrite += " "; 25 strWrite += ""; 27 strWrite += ""; 28 strWrite += "
  3. 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 protected void DropDownList1_SelectedIndexChanged(object sender, 1 EventArgs e) 2 { 3 divWeather.InnerHtml = GetWeather(DropDownList1.SelectedValue); 4 } Download Demo Project tại đây
nguon tai.lieu . vn