利用AJAX的UpdatePanel與Timer控制項結合MSN Weather的RSS來達到即時氣象資訊更新的動作
這個範例主要是透過ASP.Net AJAX的非同步傳輸技術,結合MSN Weather的RSS xml 取得氣象資訊
來達到即時更新氣象的一個小範例,並且可以查詢不同台灣不同地區的氣象資訊
ps.程式部分是參考藍色小舖的阿達猴大大的原始碼
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20070724164013E8D
VB.net範例
.aspx
01 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="RSS_VB.aspx.vb" Inherits="RSS_VB" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:ScriptManager ID="ScriptManager1" runat="server">
12 </asp:ScriptManager>
13 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
14 <ContentTemplate>
15 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
16 <asp:ListItem Value="http://weather.msn.com/RSS.aspx?wealocations=wc:TWXX0021&weadegreetype =C">台北</asp:ListItem>
17 <asp:ListItem Value="http://weather.msn.com/RSS.aspx?wealocations=wc:32433&weadegreetype=C">台南</asp:ListItem>
18 <asp:ListItem Value="http://weather.msn.com/RSS.aspx?wealocations=wc:16133&weadegreetype=C">高雄</asp:ListItem>
19 </asp:DropDownList>
20 <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
21 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
22 </ContentTemplate>
23 <Triggers>
24 <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
25 </Triggers>
26 </asp:UpdatePanel>
27 <asp:Timer ID="Timer1" runat="server">
28 </asp:Timer>
29 </div>
30 </form>
31 </body>
32 </html>
33
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:ScriptManager ID="ScriptManager1" runat="server">
12 </asp:ScriptManager>
13 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
14 <ContentTemplate>
15 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
16 <asp:ListItem Value="http://weather.msn.com/RSS.aspx?wealocations=wc:TWXX0021&weadegreetype =C">台北</asp:ListItem>
17 <asp:ListItem Value="http://weather.msn.com/RSS.aspx?wealocations=wc:32433&weadegreetype=C">台南</asp:ListItem>
18 <asp:ListItem Value="http://weather.msn.com/RSS.aspx?wealocations=wc:16133&weadegreetype=C">高雄</asp:ListItem>
19 </asp:DropDownList>
20 <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
21 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
22 </ContentTemplate>
23 <Triggers>
24 <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
25 </Triggers>
26 </asp:UpdatePanel>
27 <asp:Timer ID="Timer1" runat="server">
28 </asp:Timer>
29 </div>
30 </form>
31 </body>
32 </html>
33
.vb
01 Imports System.DataImports System.TextPartial Class RSS_VB Inherits System.Web.UI.Page
02
03 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
04 If Not IsPostBack Then
05 Me.Label1.Text = readRSS()
06 Me.Label2.Text = Now.ToString("yyyy/MM/dd-hh:mm")
07 End If
08 End Sub
09
10 Function readRSS()
11 Dim myDataset As New DataSet
12 myDataset.ReadXml(Me.DropDownList1.SelectedValue)
13 Dim tempString As String
14 tempString = myDataset.Tables(3).Rows(1).Item("description")
15 Dim myDay1, myDay2, myDay3 As String
16 tempString = tempString.Replace("<p>", "")
17 tempString = tempString.Replace("</p>", "")
18 myDay1 = tempString.Substring(0, tempString.IndexOf("<br />"))
19 tempString = tempString.Remove(0, tempString.IndexOf("<br />") + 6)
20 myDay2 = tempString.Substring(0, tempString.IndexOf("<br />"))
21 tempString = tempString.Remove(0, tempString.IndexOf("<br />") + 6)
22 myDay3 = tempString.Substring(0, tempString.IndexOf("<br />"))
23 Dim sb As New StringBuilder
24 sb.Append("今天:" & myWeather(myDay1) & "<BR>")
25 sb.Append("明天:" & myWeather(myDay2) & "<BR>")
26 sb.Append("後天:" & myWeather(myDay3))
27 Return sb.ToString()
28 End Function
29
30 Function myWeather(ByVal tempString As String)
31 Dim Weather As String
32 Dim myString As String
33 myString = tempString
34 myString = myString.Remove(0, myString.IndexOf("<img"))
35 myString = myString.Substring(0, myString.IndexOf(">") + 1)
36 Weather = myString
37 myString = tempString
38 myString = myString.Remove(0, myString.IndexOf("Lo:"))
39 myString = myString.Substring(0, myString.IndexOf("."))
40 myString = myString.Replace("Lo", "最低")
41 Weather = Weather & myString
42 myString = tempString
43 myString = myString.Remove(0, myString.IndexOf("Hi:"))
44 myString = myString.Substring(0, myString.IndexOf("."))
45 myString = myString.Replace("Hi", "最高")
46 Weather = Weather & "," & myString
47 Return Weather
48 End Function
49
50 Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
51 Me.Label1.Text = readRSS()
52 Me.Label2.Text = Now.ToString("yyyy/MM/dd-hh:mm")
53 End Sub
54
55 Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
56 Me.Label1.Text = readRSS()
57 Me.Label2.Text = Now.ToString("yyyy/MM/dd-hh:mm")
58 End Sub
59 End Class
60
61
02
03 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
04 If Not IsPostBack Then
05 Me.Label1.Text = readRSS()
06 Me.Label2.Text = Now.ToString("yyyy/MM/dd-hh:mm")
07 End If
08 End Sub
09
10 Function readRSS()
11 Dim myDataset As New DataSet
12 myDataset.ReadXml(Me.DropDownList1.SelectedValue)
13 Dim tempString As String
14 tempString = myDataset.Tables(3).Rows(1).Item("description")
15 Dim myDay1, myDay2, myDay3 As String
16 tempString = tempString.Replace("<p>", "")
17 tempString = tempString.Replace("</p>", "")
18 myDay1 = tempString.Substring(0, tempString.IndexOf("<br />"))
19 tempString = tempString.Remove(0, tempString.IndexOf("<br />") + 6)
20 myDay2 = tempString.Substring(0, tempString.IndexOf("<br />"))
21 tempString = tempString.Remove(0, tempString.IndexOf("<br />") + 6)
22 myDay3 = tempString.Substring(0, tempString.IndexOf("<br />"))
23 Dim sb As New StringBuilder
24 sb.Append("今天:" & myWeather(myDay1) & "<BR>")
25 sb.Append("明天:" & myWeather(myDay2) & "<BR>")
26 sb.Append("後天:" & myWeather(myDay3))
27 Return sb.ToString()
28 End Function
29
30 Function myWeather(ByVal tempString As String)
31 Dim Weather As String
32 Dim myString As String
33 myString = tempString
34 myString = myString.Remove(0, myString.IndexOf("<img"))
35 myString = myString.Substring(0, myString.IndexOf(">") + 1)
36 Weather = myString
37 myString = tempString
38 myString = myString.Remove(0, myString.IndexOf("Lo:"))
39 myString = myString.Substring(0, myString.IndexOf("."))
40 myString = myString.Replace("Lo", "最低")
41 Weather = Weather & myString
42 myString = tempString
43 myString = myString.Remove(0, myString.IndexOf("Hi:"))
44 myString = myString.Substring(0, myString.IndexOf("."))
45 myString = myString.Replace("Hi", "最高")
46 Weather = Weather & "," & myString
47 Return Weather
48 End Function
49
50 Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
51 Me.Label1.Text = readRSS()
52 Me.Label2.Text = Now.ToString("yyyy/MM/dd-hh:mm")
53 End Sub
54
55 Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
56 Me.Label1.Text = readRSS()
57 Me.Label2.Text = Now.ToString("yyyy/MM/dd-hh:mm")
58 End Sub
59 End Class
60
61