Download presentation
Presentation is loading. Please wait.
1
ASP.NET 網頁製作教本 – 從基本語法學起
第 16 章 線上投票、線上問卷調查
2
16-1 線上投票
3
範例網頁的組成
4
Vote.aspx 網頁
5
Vote.mdb 的 Vote 資料庫(表)的結構
6
Vote.mdb 的 Vote 資料庫(表)的內容
7
比例橫條圖的顯示
8
Bar.htm <IMG SRC="100.gif" Width=400 Height=16 Align=TextTop> 100%<P> <IMG SRC="100.gif" Width=200 Height=16 Align=TextTop> 50%<P> <IMG SRC="100.gif" Width=100 Height=16 Align=TextTop> 25%<P> <IMG SRC="100.gif" Width=50 Height=16 Align=TextTop> 12.5%<P> <IMG SRC="100.gif" Width=25 Height=16 Align=TextTop> 6.25%<P>
9
Result.aspx #58 ' 計算得票率,並且產生比例橫條圖
#58 ' 計算得票率,並且產生比例橫條圖 #59 For I = 0 To Table1.Rows.Count - 1 #60 Dim Rate = Table1.Rows(I).Item("得票數") / Total #61 Dim RateHTML As String #62 Dim RateValue As String #63 #64 RateHTML = "<IMG SRC=100.gif Height=16 Width=" & _ # Rate*400 & " Align=TextTop>" #66 RateValue = "(" & FormatNumber(Rate*100, 2) & "%)" #67 Table1.Rows(I).Item("得票率") = RateHTML & RateValue #68 Next
10
動態產生選項 #29 Dim Conn As OleDbConnection, Cmd As OleDbCommand
#30 Dim Rd As OleDbDataReader, SQL As String #31 #32 Conn = New OleDbConnection( Provider & ";" & DataBase ) #33 Conn.Open() #34 SQL = "Select * From Vote" #35 Cmd = New OleDbCommand( SQL, Conn ) #36 Rd = Cmd.ExecuteReader() #37 #38 While Rd.Read() #39 Candidate.Items.Add( Rd.Item(“世界奇景”) ) #40 End While #41 Conn.Close()
11
判斷瀏覽器的 Cookie 是否開啟(1)
12
判斷瀏覽器的 Cookie 是否開啟(2) 第一次被瀏覽
#25 Sub Page_Load(sender As Object, e As EventArgs) # If Not IsPostBack Then # Session("IsCookieOpen") = "Set in Page_Load" #28 … # End If #43 End Sub 投票鈕被按下 #45 Sub Vote_Click(sender As Object, e As EventArgs) # Msg.Text = "" #47 # ' 檢查瀏覽器的 Cookie 是否開啟 # If Session("IsCookieOpen") <> "Set in Page_Load" Then # Msg.Text = "欲參與投票, 請先開啟瀏覽器的 Cookie, " & _ # "然後關閉瀏覽器, 再重新啟動瀏覽器!" #71 End Sub
13
倒票攻防戰 #56 If Session("Voted") <> "Yes" Then ' 還沒投過票
#57 Dim Conn As OleDbConnection, Cmd As OleDbCommand #58 Conn = New OleDbConnection( Provider & ";" & DataBase ) #59 Conn.Open() #60 Dim SQL = "Update Vote Set 得票數=得票數+1 " & _ # "Where 世界奇景='" & Sel & "'" #62 Cmd = New OleDbCommand( SQL, Conn ) #63 Cmd.ExecuteNonQuery() #64 Conn.Close() #65 Session("Voted") = "Yes" #66 Response.Redirect( "Result.aspx" ) #67 Else #68 Msg.Text = "您已經投過票了, 不能重複投票!" #69 End If
14
設定 Cookie 物件的期限 修改一:Vote.aspx #56
#56 If Session("Voted") <> "Yes" Then ' 還沒投過票 修改成:Vote2.aspx #57~62 #57 Dim cookie As HttpCookie, value As String #58 cookie = Request.Cookies("Voted") #59 If Not cookie Is Nothing Then #60 value = Request.Cookies("Voted").Value #61 End If #62 If value <> "Yes" Then ' 還沒投過票 修改二:Vote.aspx #65 #65 Session("Voted") = "Yes" 修改成:Vote2.aspx #71~72 #71 Response.Cookies("Voted").Value = "Yes" #72 Response.Cookies("Voted").Expires = "12/31/2999"
15
Vote2.aspx 的破解 選取功能表的「工具->網際網路選項」,然後按下「刪除 Cookie」鈕,如下圖,就可以將檔案中的 Cookie 全數刪除。
16
線上投票第三版 在 Vote.mdb 資料庫中建立 VoteRecord 資料表,記錄投票者的 IP 位址及投票時間,其內容如下:
17
程式的撰寫(1) #56 ' 檢查上次投票時間是否已經超過 10 分鐘
#56 ' 檢查上次投票時間是否已經超過 10 分鐘 #57 Dim IP = Request.ServerVariables("REMOTE_ADDR") #58 SQL = "Select * From VoteRecord Where IP = '" & IP & "' " #59 SQL &= "Order By VoteTime Desc" #60 Cmd = New OleDbCommand( SQL, Conn ) #61 Rd = Cmd.ExecuteReader() #62 If Rd.Read() #63 NextTime = 10 - DateDiff("n", Rd.Item("VoteTime"), Now) #64 End If #65 Rd.Close() #66 #67 ' 沒投過票或距離上次投票已經超過 10 分鐘 #68 If NextTime <= 0 Then #69 ' 將得票數加一 #70 SQL = "Update Vote Set 得票數=得票數+1 " & _ # "Where 世界奇景='" & Sel & "'" #72 Cmd = New OleDbCommand( SQL, Conn ) #73 Cmd.ExecuteNonQuery()
18
程式的撰寫(2) #74 #75 ' 記錄 IP 及 投票時間 #76 Dim Tm = Format(Now(), "MM/dd/yyyy HH:mm:ss") #77 SQL = "Insert Into VoteRecord (IP, VoteTime) " & _ # "Values ('" & IP & "', #" & Tm & "#)" #79 Cmd = New OleDbCommand( SQL, Conn ) #80 Cmd.ExecuteNonQuery() #81 #82 Response.Redirect( "Result3.aspx" ) #83 Else #84 Msg.Text = "您已經投過票了, 不能重複投票!" #85 End If
19
Proxy 的問題
20
16-2 線上問卷調查
21
範例網頁的組成
22
Opinion.mdb 資料庫 的 Title 資料表
23
Opinion.aspx 網頁
24
OpShow.aspx 網頁
25
動態產生問卷表單 – Panel.aspx(1)
<HTML> <BODY> <H3>Panel.aspx -- 利用 Panel 收納其他控制元件<HR></H3> <Form runat="server"> <asp:Panel runat="server" id="MyPanel" /><p> <asp:Button runat="server" Text="輸入" OnClick="Button_Click" /> <HR> <asp:Label runat="server" id="Msg" /> </Form> </BODY> </HTML> <script language="VB" runat=server> Sub Page_Load(sender As Object, e As EventArgs) ' 利用程式動態產生 TextBox Dim Text1 As New TextBox Text1.Columns = 10
26
動態產生問卷表單 – Panel.aspx(2)
' 利用程式動態產生「文字」 Dim Literal1 As New LiteralControl Literal1.Text = "<br>(請在文字方塊中輸入資料)<p>" ' 加入於 Panel 之中,也就是加入於網頁中 MyPanel.Controls.Add( Text1 ) MyPanel.Controls.Add( Literal1 ) End Sub Sub Button_Click(sender As Object, e As EventArgs) Dim Text1 As TextBox = MyPanel.Controls(0) Msg.Text = Text1.Text </script>
27
Panel.aspx 網頁
28
Button_Click 事件程序中的程式
Dim Text1 As TextBox = MyPanel.Controls(0) Msg.Text = Text1.Text
29
Opinion.aspx #36~62 (1) #36 Conn = New OleDbConnection( Provider & ";" & DataBase ) #37 Conn.Open() #38 SQL = "Select * From Title" #39 Cmd = New OleDbCommand( SQL, Conn ) #40 Rd = Cmd.ExecuteReader() #41 #42 While Rd.Read() #43 Dim Literal1 As New LiteralControl #44 Dim Rlist As New RadioButtonList #45 Dim Literal2 As New LiteralControl #46 #47 Literal1.Text = Rd.Item("Seq") & ". " #48 Literal1.Text &= Rd.Item("Title") & "<Blockquote>" #49 Literal2.Text = "</Blockquote>"
30
Opinion.aspx #36~62 (2) #50 #51 RList.RepeatColumns = 5
#52 For I = 1 To 5 # RList.Items.Add( Rd.Item("Op" & I) ) # RList.Items(I-1).Value = "Op" & I & "Count" #55 Next #56 RList.Items(0).Selected = True #57 #58 MyPanel.Controls.Add( Literal1 ) #59 MyPanel.Controls.Add( RList ) #60 MyPanel.Controls.Add( Literal2 ) #61 DataRows += 1 #62 End While
31
Title 資料表各欄位與控制元件之間的關係
32
4 個 RadioButtonList 請注意 #58~60 加入控制元件到 Panel 時,其順序是「Literal1, RList, Literal2, Literal1, RList, Literal2...」(編號依序是 0, 1, 2, 3, 4, 5...),所以在以上網頁中,4 個 RadioButtonList 將分別等於: 第一個 RadioButtonList = MyPanel.Controls(1) 第二個 RadioButtonList = MyPanel.Controls(4) 第三個 RadioButtonList = MyPanel.Controls(7) 第四個 RadioButtonList = MyPanel.Controls(10)
33
Opinion.aspx #76~91 #76 Dim Rlist(DataRows-1) As RadioButtonList #77
#78 For I = 0 To DataRows - 1 #79 RList(I) = MyPanel.Controls(1 + I*3) #80 Next #81 Conn = New OleDbConnection( Provider & ";" & DataBase ) #82 Conn.Open() #83 #84 For I = 1 To DataRows #85 Dim which As String = RList(I-1).SelectedItem.Value #86 SQL = "Update Title Set " & _ # which & "=" & which & "+1" & _ # " Where Seq=" & I #89 Cmd = New OleDbCommand( SQL, Conn ) #90 Cmd.ExecuteNonQuery() #91 Next
34
舉例 上網者所填寫的問卷是: #85所得到的結果將等於: SQL指令將等於:
第1題: 非常滿意 對應的欄位是 Op1Count 第2題: 不滿意 對應的欄位是 Op3Count 第3題: 滿意 對應的欄位是 Op2Count 第4題: 非常不滿意 對應的欄位是 Op4Count RList(0).SelectedItem.Value = "Op1Count" RList(1).SelectedItem.Value = "Op3Count" RList(2).SelectedItem.Value = "Op2Count" RList(3).SelectedItem.Value = "Op4Count" Update Title Set Op1Count=Op1Count+1 Where Seq = 1 Update Title Set Op3Count=Op3Count+1 Where Seq = 2 Update Title Set Op2Count=Op2Count+1 Where Seq = 3 Update Title Set Op4Count=Op4Count+1 Where Seq = 4
35
OpShow.aspx #10~41 (1) #10 <asp:Repeater runat="server" id="MyRepeater"> #11 <ItemTemplate> #12 <%# Container.DataItem("Seq") %>. <%# Container.DataItem("Title") %> #13 <Blockquote><Table> #14 <Tr> # <Td><%# Container.DataItem("Op1") %></Td> # <Td><%# Container.DataItem("Op1Count") %></Td> # <Td><%# Container.DataItem("Rate1") %></Td> #18 </Tr> #19 <Tr> # <Td><%# Container.DataItem("Op2") %></Td> # <Td><%# Container.DataItem("Op2Count") %></Td> # <Td><%# Container.DataItem("Rate2") %></Td> #23 </Tr> #24 <Tr> # <Td><%# Container.DataItem("Op3") %></Td> # <Td><%# Container.DataItem("Op3Count") %></Td> # <Td><%# Container.DataItem("Rate3") %></Td> #28 </Tr>
36
OpShow.aspx #10~41 (2) #29 <Tr>
# <Td><%# Container.DataItem("Op4") %></Td> # <Td><%# Container.DataItem("Op4Count") %></Td> # <Td><%# Container.DataItem("Rate4") %></Td> #33 </Tr> #34 <Tr> # <Td><%# Container.DataItem("Op5") %></Td> # <Td><%# Container.DataItem("Op5Count") %></Td> # <Td><%# Container.DataItem("Rate5") %></Td> #38 </Tr> #39 </Table></Blockquote> #40 </ItemTemplate> #41 </asp:Repeater><p>
37
OpShow.aspx #65~88(1) #65 Dim Table1 As DataTable = Ds.Tables("Title")
#66 For K = 1 To 5 #67 Table1.Columns.Add(New DataColumn("Rate" & K, GetType(String))) #68 Next #69 #70 For I = 0 To Table1.Rows.Count - 1 #71 Dim Total = 0, Value(5) #72 #73 For K = 1 To 5 # Value(K) = Table1.Rows(I).Item("Op" & K & "Count") # Total += Value(K) #76 Next #77 If Total = 0 Then Total = 1 #78
38
OpShow.aspx #65~88 (2) #79 For K = 1 To 5
# Dim Rate = Value(K) / Total # Dim RateHTML As String # Dim RateValue As String # RateHTML = "<IMG SRC=100.gif Height=16 Width=" & _ # Rate*400 & " Align=TextTop>" # RateValue = "(" & FormatNumber(Rate*100, 2) & "%)" # Table1.Rows(I).Item("Rate" & K) = RateHTML & RateValue #87 Next #88 Next
Similar presentations