字 段 名 | 數 據 類 型 | 字 段 說 明 | 鍵 引 用 | 備 注 |
TreeID | int | 投票項目ID | PK | 主鍵(自動增一) |
Item | varchar(200) | 投票項目的名稱 | ||
VoteCount | int | 票數 |
/* 存儲過程Pr_GetVotes */ CREATE PROCEDURE Pr_GetVotes AS SELECT * FROM Votes ORDER BY VoteID /* 存儲過程Pr_GetSingleVote */ CREATE PROCEDURE Pr_GetSingleVote (@VoteID int) AS SELECT Votes.* FROM Votes WHERE VoteID = @VoteID /* 存儲過程Pr_AddVote */ CREATE PROCEDURE Pr_AddVote(@Item varchar(100)) AS INSERT INTO Votes(Item,ItemCount) VALUES(@Item,0) RETURN @@Identity /* 存儲過程Pr_UpdateVote */ CREATE PROCEDURE Pr_UpdateVote (@VoteID int) AS UPDATE Votes SET VoteCount = VoteCount + 1 WHERE VoteID = @VoteID /* 存儲過程Pr_DeleteVote */ CREATE PROCEDURE Pr_DeleteVote (@VoteID int) AS DELETE Votes WHERE VoteID = @VoteID |
public class Vote { public SqlDataReader GetVotes() { //定義類SQLHelper SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); //定義保存從數據庫獲取的結果的DataReader SqlDataReader dr = null; try { //執行存儲過程 sqlHelper.RunProc("Pr_GetVotes", out dr); } catch (Exception ex) { //拋出執行數據庫異常 SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } //返回從數據庫獲取的結果 return (dr); } public int AddVote(String sItem) { //定義類SQLHelper SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); //創建訪問數據庫的參數 SqlParameter[] paramList = { sqlHelper.CreateInParam("@Item", SqlDbType.VarChar,100,sItem) }; try { //執行存儲過程 return (sqlHelper.RunProc("Pr_AddVote", paramList)); } catch (Exception ex) { //拋出執行數據庫異常 SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } } public void UpdateVote(int nVoteID) { //定義類SQLHelper SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); //創建訪問數據庫的參數 SqlParameter[] paramList = {sqlHelper.CreateInParam("@VoteID", SqlDbType.Int, 4,nVoteID)}; try { //執行存儲過程 sqlHelper.RunProc("Pr_UpdateVote", paramList); } catch (Exception ex) { //拋出執行數據庫異常 SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } } public void DeleteVote(int nVoteID) { //定義類SQLHelper SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); //創建訪問數據庫的參數 SqlParameter[] paramList = { sqlHelper.CreateInParam("@VoteID", SqlDbType.Int, 4,nVoteID) }; try { //執行存儲過程 sqlHelper.RunProc("Pr_DeleteVote", paramList); } catch (Exception ex) { //拋出執行數據庫異常 SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } } } |
<asp:HyperLink ID="ItemManageLink" NavigateUrl="~/VoteItemManage.aspx" runat="server" Font-Bold="True">投票項目管理</asp:HyperLink> <asp:HyperLink ID="OnlineVoteLink" NavigateUrl="~/WebOnlinVote.aspx" runat="server" Font-Bold="True">網站在線投票</asp:HyperLink> <asp:HyperLink ID="ViewVoteLink" NavigateUrl="~/ShowVoteInfo.aspx" runat="server" Font-Bold="True">查看投票結果</asp:HyperLink> |
在線投票系統運行之后,系統默認頁面Default.aspx的初始化界面如圖3所示,此時顯示3個鏈接按鈕。
圖3 投票頁面Default.aspx的初始化界面
投票項目管理頁面設計
在應用程序WebVote中添加一個新的Web頁面,并命名為VoteItemManage.aspx,它的代碼隱藏文件為VoteItemManage.aspx.cs文件。
1.頁面設計
在頁面VoteItemManage.aspx上添加一個列表控件、一個Button控件、一個TextBox控件和一個ImageButton控件,它們的名稱分別為ItemList、AddBtn、Item和deleteBtn。控件ItemList顯示投票項目表中的所有數據;控件AddBtn實現添加一個新的投票項目;控件Item用來輸入新的投票項目名稱;控件deleteBtn刪除一個投票項目。頁面ItemManage.aspx的設計界面如圖4所示。
圖4 頁面VoteItemManage.aspx的設計界面
頁面VoteItemManage.aspx的HTML設計代碼如下:
<title>網絡在線投票系統</title> <link href="CSS/ASPNET2BaseCss.css" type="text/css" rel="stylesheet"> <asp:ListBox id="ItemList" width="150" rows="10" runat="server" CssClass="SelectSta" /> <asp:ImageButton id="deleteBtn" ImageUrl="~/images/delete.gif" AlternateText="刪除此項" runat="server" CommandName="delete" OnClick="deleteBtn_Click" /> <asp:TextBox ID="Item" Runat="server" Width="252" CssClass="InputCss"></asp:TextBox> <asp:Button ID="AddBtn" Runat="server" Text="增加新的投票項目" CssClass="ButtonCss" OnClick="AddBtn_Click"></asp:Button> |
private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { //綁定投票項目列表的數據 BindVoteListData(); } } private void BindVoteListData() { //獲取投票項目的所有數據 WebVote.Vote vote = new Vote(); SqlDataReader recv = vote.GetVotes(); //設置列表控件的Text屬性和Value屬性 ItemList.DataTextField = "Item"; ItemList.DataValueField = "VoteID"; //設置控件的數據源,并綁定控件的數據 ItemList.DataSource = recv; ItemList.DataBind(); recv.Close(); //關閉數據讀取器 } |
private void AddBtn_Click(object sender, System.EventArgs e) { if (Item.Text.Length > 0) { //定義類 WebVote.Vote vote = new Vote(); try { //添加新數據項 vote.AddVote(Item.Text.Trim()); BindVoteListData(); //顯示操作結果信息 Response.Write("<scr+ ASPNET2System.OperaTIONADDSUCCESSMESSAGE + "')</script>"); } catch (Exception ex) { //顯示添加操作中的失敗、錯誤信息 Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl=" + ASPNET2System.RedirectErrorUrl(Request.RawUrl) + "&ErrorMessage=" + ex.Message.Replace("/n", " ")); } } } |
protected void deleteBtn_Click(object sender, ImageClickEventArgs e) { if (ItemList.SelectedIndex <= -1) { //顯示操作結果信息 Response.Write("<script>window.alert('" + ASPNET2System.OPERATIONNOSELECTMESSAGE + "')</script>"); return; } //定義類 WebVote.Vote vote = new Vote(); try { //刪除數據 vote.DeleteVote(Int32.Parse(ItemList.SelectedValue)); //重新綁定數據 BindVoteListData(); } catch (Exception ex) { //顯示刪除操作中的失敗、錯誤信息 Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl=" + ASPNET2System.RedirectErrorUrl(Request.RawUrl) + "&ErrorMessage=" + ex.Message.Replace("/n", " ")); } } |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebOnlinVote.aspx.cs" Inherits="WebOnlinVote" %> <HTML><HEAD><title>網絡在線投票系統</title></HEAD> <asp:datagrid id="VoteList" CssClass="GbText" Runat="server" AutoGenerateColumns="False" DataKeyField="VoteID"> <Columns> <asp:TemplateColumn ItemStyle-Width="200"> <ItemTemplate><%# DataBinder.Eval(Container.DataItem,"Item")%> </ItemTemplate></asp:TemplateColumn> <asp:TemplateColumn ItemStyle-Width="100"> <ItemTemplate> <asp:CheckBox ID="VoteCheck" Runat="server"></asp:CheckBox> </ItemTemplate></asp:TemplateColumn> </Columns> <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <ItemStyle BackColor="White" ForeColor="#330099" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> </asp:datagrid> <asp:button id="VoteBtn" Runat="server" Width="100" Text="我要投票"></asp:button> <asp:button id="ShowVote" Runat="server" Width="100" Text="查看投票"></asp:button> <asp:Label ID="VoteMessage" Runat="server" Visible="False" ForeColor="red" Font-Bold="True">投票成功!!!</asp:Label></td> </HTML> |
private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { //綁定投票的項目 BindVoteListData(); VoteMessage.Visible = false; } } private void BindVoteListData() { //獲取所有數據 WebVote.Vote vote = new Vote(); SqlDataReader recv = vote.GetVotes(); //設置控件的數據源,并綁定數據 VoteList.DataSource = recv; VoteList.DataBind(); recv.Close(); //關閉數據讀取器 } |
private void VoteBtn_Click(object sender, System.EventArgs e) { //定義類 WebVote.Vote vote = new Vote(); try { //添加用戶的投票的項目 foreach(DataGridItem item in VoteList.Items) { //查找每個投票項目的選擇控件 CheckBox check = (CheckBox)item.FindControl("VoteCheck"); if(check != null) { //說明用戶已經投票,則需要添加這一票 if(check.Checked == true) { //修改數據庫中的票數 vote.UpdateVote(Int32.Parse( VoteList.DataKeys[item.ItemIndex].ToString())); VoteMessage.Visible = true; //顯示用戶投票操作的結果 } } } //顯示操作結果信息 Response.Write("<script>window.alert(' 投票成功,感謝您的參與!!!')</script>"); } catch (Exception ex) { //顯示修改操作中的失敗、錯誤信息 Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl=" + ASPNET2System.RedirectErrorUrl(Request.RawUrl) + "&ErrorMessage=" + ex.Message.Replace("/n", " ")); } } private void ShowVote_Click(object sender, System.EventArgs e) { //導向查看投票結果頁面 Response.Redirect("~/ShowVoteInfo.aspx"); } |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowVoteInfo.aspx.cs" Inherits="ShowVoteInfo" %> <HTML><HEAD><title>網絡在線投票系統</title></HEAD> <asp:DataGrid ID="VoteList" Runat="server" CssClass="Normal" AutoGenerateColumns="False" DataKeyField="VoteID"> <HeaderStyle BackColor="Orange"></HeaderStyle> <Columns> <asp:TemplateColumn HeaderText="投票項目"> <ItemStyle Width="200px"></ItemStyle> <ItemTemplate><%# DataBinder.Eval(Container.DataItem,"Item")%> </ItemTemplate></asp:TemplateColumn> <asp:TemplateColumn HeaderText="所占總票的百分比"> <ItemStyle Width="300px"></ItemStyle> <ItemTemplate> <asp:Image ID="voteImage" Runat="server" Height="20" Width='<%# FormatVoteImage(FormatVoteCount(DataBinder.Eval( Container.DataItem,"VoteCount").ToString()))%>' mageUrl="Images/vote.gif"> </asp:Image> <%# FormatVoteCount(DataBinder.Eval(Container.DataItem, "VoteCount").ToString())%>% </ItemTemplate></asp:TemplateColumn> <asp:TemplateColumn HeaderText="票數"> <ItemStyle Width="100px"></ItemStyle> <ItemTemplate> <asp:Label ID="VoteCount" Runat="server"> <%# DataBinder.Eval(Container.DataItem,"VoteCount")%> </asp:Label> </ItemTemplate></asp:TemplateColumn> </Columns> </asp:DataGrid> <asp:Label ID="VoteMessage" Runat="server" ForeColor="Red" Width="100%"></asp:Label> <asp:button id="WebOnlineVoteBtn" Runat="server" Width="100" Text="返回投票頁面" CssClass="ButtonCss" OnClick="WebOnlineVoteBtn_Click"></asp:button> </HTML> |
int voteTotal = 0; private void Page_Load(object sender, System.EventArgs e) { //設置總票數voteTotal SetVoteTotal(); if(!Page.IsPostBack) { //顯示用戶投票的具體情況 BindVoteListData(); VoteMessage.Text = "總票數為:" + voteTotal.ToString(); } } private void SetVoteTotal() { //獲取所有數據 WebVote.Vote vote = new Vote(); SqlDataReader recv = vote.GetVotes(); voteTotal = 0; //讀取每一個參與投票的項目,并計算票數總和 while(recv.Read()) { //計算它們的總和 voteTotal += Int32.Parse(recv["VoteCount"].ToString()); } recv.Close(); } private void BindVoteListData() { //獲取數據 WebVote.Vote vote = new Vote(); SqlDataReader recv = vote.GetVotes(); //設置控件的數據源,并綁定控件的數據 VoteList.DataSource = recv; VoteList.DataBind(); recv.Close(); } |
public int FormatVoteCount(String voteCount) { //如果投票沒有被投票 if(voteCount.Length <= 0) { //返回0個百分比 return(0); } if(voteTotal > 0) { //返回實際的百分比 return((Int32.Parse(voteCount)* 100/voteTotal)); } return(0); } public int FormatVoteImage(int voteCount) { //返回百分比的圖像的長度 return(voteCount * 3); } |
新聞熱點
疑難解答