用ASP.NET 2.0设计网络在线投票系统
一、系統(tǒng)功能設(shè)計(jì)和數(shù)據(jù)庫(kù)設(shè)計(jì)
1、系統(tǒng)功能設(shè)計(jì)和數(shù)據(jù)庫(kù)設(shè)計(jì)
1.1 系統(tǒng)功能設(shè)計(jì)
網(wǎng)絡(luò)在線投票系統(tǒng)實(shí)現(xiàn)的功能比較簡(jiǎn)單,具體如下:
◎投票項(xiàng)目的管理;
◎添加投票的項(xiàng)目;
◎刪除投票的項(xiàng)目;
◎?qū)?xiàng)目進(jìn)行投票;
◎查看項(xiàng)目的投票情況。
1.2 數(shù)據(jù)庫(kù)設(shè)計(jì)
本系統(tǒng)的數(shù)據(jù)庫(kù)設(shè)計(jì)比較簡(jiǎn)單,只需要存儲(chǔ)投票的信息即可。在SQL Server 2000中創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),名稱為“WebVoteDB”,并在該數(shù)據(jù)庫(kù)中創(chuàng)建投票項(xiàng)目表Votes。其中“VoteID”字段存儲(chǔ)投票項(xiàng)目ID;“Item”字段存儲(chǔ)投票項(xiàng)目的名稱;“VoteCount”字段存儲(chǔ)每個(gè)項(xiàng)目的票數(shù)。創(chuàng)建投票項(xiàng)目表Votes的操作界面如圖1所示。
投票項(xiàng)目表Votes需要存儲(chǔ)投票項(xiàng)目名稱及其票數(shù),表的字段說(shuō)明如表1所示。
圖1 創(chuàng)建投票項(xiàng)目表Votes的操作界面
表1 Votes表
字 段 名
?
數(shù) 據(jù) 類 型
?
字 段 說(shuō) 明
?
鍵 引 用
?
備 注
?
TreeID int? 投票項(xiàng)目ID? PK 主鍵(自動(dòng)增一)
Item varchar(200)?
投票項(xiàng)目的名稱
??
?
VoteCount
?
int
?
票數(shù)
??
?
在線投票功能是網(wǎng)站應(yīng)用程序最常用的功能之一,也是網(wǎng)站應(yīng)用程序開(kāi)發(fā)常用的功能模塊。當(dāng)網(wǎng)站的管理員或用戶提出一些新的想法與建議或者出現(xiàn)一種新產(chǎn)品時(shí),他們可能需要通過(guò)用戶或者客戶的投票方式來(lái)確定這些新的想法、建議或者新的產(chǎn)品是否滿足用戶或者客戶的需求,另外,網(wǎng)站還可以通過(guò)網(wǎng)站在線投票功能做一些實(shí)際性的調(diào)查工作。本章介紹的網(wǎng)絡(luò)在線投票系統(tǒng)還以直觀的圖形化界面顯示投票信息,而且還可以及時(shí)查看投票的情況。
二、投票系統(tǒng)實(shí)現(xiàn)
創(chuàng)建好系統(tǒng)所需要的數(shù)據(jù)庫(kù)之后,網(wǎng)絡(luò)在線投票系統(tǒng)的具體實(shí)現(xiàn)可以分為下面3個(gè)部分:
(1)存儲(chǔ)過(guò)程的實(shí)現(xiàn)部分;
(2)數(shù)據(jù)庫(kù)訪問(wèn)層的實(shí)現(xiàn)部分;
(3)功能頁(yè)面的實(shí)現(xiàn)部分。
下面將詳細(xì)介紹上述3個(gè)部分的具體實(shí)現(xiàn)方法。首先在Microsoft Visual Studio .NET 2005中創(chuàng)建一個(gè)Web站點(diǎn),名稱為“WebVote”。
2.1 存儲(chǔ)過(guò)程設(shè)計(jì)
在數(shù)據(jù)庫(kù)WebVoteDB中創(chuàng)建存儲(chǔ)過(guò)程Pr_GetVotes、Pr_GetSingleVote、Pr_AddVote、Pr_UpdateVote和Pr_DeleteVote。其中:
Pr_GetVotes 從投票項(xiàng)目表Votes中獲取所有投票項(xiàng)目的信息;
Pr_GetSingleVote 從投票項(xiàng)目表Votes中獲取某一條投票項(xiàng)目的信息;
Pr_AddVote 添加一條新記錄到投票項(xiàng)目表Votes中;
Pr_UpdateVote 更新參與投票項(xiàng)目的票數(shù);
Pr_DeleteVote 從投票項(xiàng)目表Votes中獲取刪除一條投票項(xiàng)目信息。
以上各存儲(chǔ)過(guò)程的程序代碼如下:
/* 存儲(chǔ)過(guò)程Pr_GetVotes */
CREATE PROCEDURE Pr_GetVotes
AS
SELECT * FROM Votes ORDER BY VoteID
/* 存儲(chǔ)過(guò)程Pr_GetSingleVote */
CREATE PROCEDURE Pr_GetSingleVote
(@VoteID int)
AS
SELECT Votes.* FROM Votes WHERE VoteID = @VoteID
/* 存儲(chǔ)過(guò)程Pr_AddVote */
CREATE PROCEDURE Pr_AddVote(@Item varchar(100))
AS
INSERT INTO Votes(Item,ItemCount) VALUES(@Item,0) RETURN @@Identity
/* 存儲(chǔ)過(guò)程Pr_UpdateVote */
CREATE PROCEDURE Pr_UpdateVote (@VoteID int)
AS
UPDATE Votes SET VoteCount = VoteCount + 1
WHERE VoteID = @VoteID
/* 存儲(chǔ)過(guò)程Pr_DeleteVote */
CREATE PROCEDURE Pr_DeleteVote (@VoteID int)
AS
DELETE Votes
WHERE VoteID = @VoteID
2.2 數(shù)據(jù)庫(kù)訪問(wèn)層設(shè)計(jì)
在應(yīng)用程序WebVote中添加訪問(wèn)投票表Votes的類Vote,該類封裝對(duì)投票項(xiàng)目表Votes中記錄的選擇、添加、修改和刪除的方法。其中:
方法GetVotes() 從投票項(xiàng)目表Votes中獲取所有投票項(xiàng)目的信息;
方法AddVote(String sItem) 添加一條新記錄到投票項(xiàng)目表Votes中;
方法UpdateVote(int nVoteID) 更新參與投票項(xiàng)目的票數(shù);
方法DeleteVote(int nVoteID) 從投票項(xiàng)目表Votes中獲取刪除一條投票項(xiàng)目信息。
類Vote的程序設(shè)計(jì)代碼如下:
public class Vote
{
public SqlDataReader GetVotes()
{
//定義類SQLHelper
SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
//定義保存從數(shù)據(jù)庫(kù)獲取的結(jié)果的DataReader
SqlDataReader dr = null;
try
{ //執(zhí)行存儲(chǔ)過(guò)程
sqlHelper.RunProc("Pr_GetVotes", out dr);
}
catch (Exception ex)
{ //拋出執(zhí)行數(shù)據(jù)庫(kù)異常
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);
}
//返回從數(shù)據(jù)庫(kù)獲取的結(jié)果
return (dr);
}
public int AddVote(String sItem)
{ //定義類SQLHelper
SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
//創(chuàng)建訪問(wèn)數(shù)據(jù)庫(kù)的參數(shù)
SqlParameter[] paramList = {
sqlHelper.CreateInParam("@Item", SqlDbType.VarChar,100,sItem)
};
try
{ //執(zhí)行存儲(chǔ)過(guò)程
return (sqlHelper.RunProc("Pr_AddVote", paramList));
}
catch (Exception ex)
{ //拋出執(zhí)行數(shù)據(jù)庫(kù)異常
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);
}
}
public void UpdateVote(int nVoteID)
{ //定義類SQLHelper
SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
//創(chuàng)建訪問(wèn)數(shù)據(jù)庫(kù)的參數(shù)
SqlParameter[] paramList = {sqlHelper.CreateInParam("@VoteID", SqlDbType.Int, 4,nVoteID)};
try
{ //執(zhí)行存儲(chǔ)過(guò)程
sqlHelper.RunProc("Pr_UpdateVote", paramList);
}
catch (Exception ex)
{ //拋出執(zhí)行數(shù)據(jù)庫(kù)異常
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);
}
}
public void DeleteVote(int nVoteID)
{ //定義類SQLHelper
SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
//創(chuàng)建訪問(wèn)數(shù)據(jù)庫(kù)的參數(shù)
SqlParameter[] paramList = {
sqlHelper.CreateInParam("@VoteID", SqlDbType.Int, 4,nVoteID)
};
try
{ //執(zhí)行存儲(chǔ)過(guò)程
sqlHelper.RunProc("Pr_DeleteVote", paramList);
}
catch (Exception ex)
{ //拋出執(zhí)行數(shù)據(jù)庫(kù)異常
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);
}
}
}
系統(tǒng)主頁(yè)面設(shè)計(jì)
在應(yīng)用程序WebVote中添加一個(gè)新的Web頁(yè)面,并命名為Default.aspx,它的代碼隱藏文件為Default.aspx.cs。
在頁(yè)面Default.aspx上添加3個(gè)超鏈接控件,名稱分別為ItemManageLink、OnlineVoteLink、ViewVoteLink。它們分別實(shí)現(xiàn)跳轉(zhuǎn)投票項(xiàng)目管理頁(yè)面VoteItemManage.aspx、投票頁(yè)面WebOnlinVote.aspx、投票結(jié)果頁(yè)面ShowVoteInfo.aspx。頁(yè)面Default.aspx的設(shè)計(jì)界面如圖2所示。
圖2 頁(yè)面Default.aspx的設(shè)計(jì)界面
頁(yè)面Default.aspx的HTML設(shè)計(jì)代碼如下:
<asp:HyperLink ID="ItemManageLink" NavigateUrl="~/VoteItemManage.aspx"
runat="server" Font-Bold="True">投票項(xiàng)目管理</asp:HyperLink>
<asp:HyperLink ID="OnlineVoteLink" NavigateUrl="~/WebOnlinVote.aspx"
runat="server" Font-Bold="True">網(wǎng)站在線投票</asp:HyperLink>
<asp:HyperLink ID="ViewVoteLink" NavigateUrl="~/ShowVoteInfo.aspx"
runat="server" Font-Bold="True">查看投票結(jié)果</asp:HyperLink>
在線投票系統(tǒng)運(yùn)行之后,系統(tǒng)默認(rèn)頁(yè)面Default.aspx的初始化界面如圖3所示,此時(shí)顯示3個(gè)鏈接按鈕。
圖3 投票頁(yè)面Default.aspx的初始化界面
投票項(xiàng)目管理頁(yè)面設(shè)計(jì)
在應(yīng)用程序WebVote中添加一個(gè)新的Web頁(yè)面,并命名為VoteItemManage.aspx,它的代碼隱藏文件為VoteItemManage.aspx.cs文件。
1.頁(yè)面設(shè)計(jì)
在頁(yè)面VoteItemManage.aspx上添加一個(gè)列表控件、一個(gè)Button控件、一個(gè)TextBox控件和一個(gè)ImageButton控件,它們的名稱分別為ItemList、AddBtn、Item和deleteBtn。控件ItemList顯示投票項(xiàng)目表中的所有數(shù)據(jù);控件AddBtn實(shí)現(xiàn)添加一個(gè)新的投票項(xiàng)目;控件Item用來(lái)輸入新的投票項(xiàng)目名稱;控件deleteBtn刪除一個(gè)投票項(xiàng)目。頁(yè)面ItemManage.aspx的設(shè)計(jì)界面如圖4所示。
圖4 頁(yè)面VoteItemManage.aspx的設(shè)計(jì)界面
頁(yè)面VoteItemManage.aspx的HTML設(shè)計(jì)代碼如下:
<title>網(wǎng)絡(luò)在線投票系統(tǒng)</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="刪除此項(xiàng)" 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="增加新的投票項(xiàng)目"
CssClass="ButtonCss" OnClick="AddBtn_Click"></asp:Button>
2.頁(yè)面初始化
頁(yè)面VoteItemManage.aspx調(diào)用函數(shù)Page_Load(Object sender,EventArgs e)初始化,該函數(shù)調(diào)用函數(shù)BindVoteListData()從數(shù)據(jù)庫(kù)投票表Votes中獲取所有投票的項(xiàng)目,并把獲取的數(shù)據(jù)綁定到列表控件ItemList。函數(shù)Page_Load(Object sender,EventArgs e)和函數(shù)BindVoteListData()的程序代碼如下:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{ //綁定投票項(xiàng)目列表的數(shù)據(jù)
BindVoteListData();
}
}
private void BindVoteListData()
{ //獲取投票項(xiàng)目的所有數(shù)據(jù)
WebVote.Vote vote = new Vote();
SqlDataReader recv = vote.GetVotes();
//設(shè)置列表控件的Text屬性和Value屬性
ItemList.DataTextField = "Item";
ItemList.DataValueField = "VoteID";
//設(shè)置控件的數(shù)據(jù)源,并綁定控件的數(shù)據(jù)
ItemList.DataSource = recv;
ItemList.DataBind();
recv.Close(); //關(guān)閉數(shù)據(jù)讀取器
}
網(wǎng)絡(luò)在線投票系統(tǒng)運(yùn)行之后,投票項(xiàng)目管理頁(yè)面VoteItemManage.aspx的初始化界面如圖5所示,此時(shí)已經(jīng)顯示投票的項(xiàng)目信息。
圖5 投票項(xiàng)目管理頁(yè)面VoteItemManage.aspx的初始化界面
3.添加功能
單擊頁(yè)面VoteItemManage.aspx中的【增加新的投票項(xiàng)目】按鈕,觸發(fā)事件AddBtn_Click(object sender, System.EventArgs e),該事件實(shí)現(xiàn)添加一個(gè)新的投票項(xiàng)目。事件AddBtn_Click(object sender, System.EventArgs e)的程序代碼如下:
private void AddBtn_Click(object sender, System.EventArgs e)
{
if (Item.Text.Length > 0)
{ //定義類
WebVote.Vote vote = new Vote();
try
{ //添加新數(shù)據(jù)項(xiàng)
vote.AddVote(Item.Text.Trim());
BindVoteListData();
//顯示操作結(jié)果信息
Response.Write("<script>window.alert('"
+ ASPNET2System.OPERATIONADDSUCCESSMESSAGE + "')</script>");
}
catch (Exception ex)
{ //顯示添加操作中的失敗、錯(cuò)誤信息
Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
+ ASPNET2System.RedirectErrorUrl(Request.RawUrl)
+ "&ErrorMessage=" + ex.Message.Replace("\n", " "));
}
}
}
4.刪除功能
單擊頁(yè)面VoteItemManage.aspx中的【×】按鈕,觸發(fā)事件deleteBtn_Click(object sender, System.EventArgs e),該事件實(shí)現(xiàn)刪除已選擇的投票項(xiàng)目。事件deleteBtn_Click(object sender, System.EventArgs e)的程序代碼如下:
protected void deleteBtn_Click(object sender, ImageClickEventArgs e)
{
if (ItemList.SelectedIndex <= -1)
{ //顯示操作結(jié)果信息
Response.Write("<script>window.alert('"
+ ASPNET2System.OPERATIONNOSELECTMESSAGE + "')</script>");
return;
}
//定義類
WebVote.Vote vote = new Vote();
try
{ //刪除數(shù)據(jù)
vote.DeleteVote(Int32.Parse(ItemList.SelectedValue));
//重新綁定數(shù)據(jù)
BindVoteListData();
}
catch (Exception ex)
{ //顯示刪除操作中的失敗、錯(cuò)誤信息
Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
+ ASPNET2System.RedirectErrorUrl(Request.RawUrl)
+ "&ErrorMessage=" + ex.Message.Replace("\n", " "));
}
}
投票頁(yè)面設(shè)計(jì)
在應(yīng)用程序WebVote中添加一個(gè)新的Web頁(yè)面,并命名為WebOnlineVote.aspx,它的代碼隱藏文件為WebOnlineVote.aspx.cs文件。
1.頁(yè)面設(shè)計(jì)
在頁(yè)面WebOnlineVote.aspx上添加一個(gè)數(shù)據(jù)網(wǎng)格控件、兩個(gè)Button控件和一個(gè)Label控件,它們的名稱分別為VoteList、VoteBtn、ShowVote和VoteMessage。控件VoteList用來(lái)顯示參與投票的所有項(xiàng)目;控件VoteBtn提交用戶的投票;控件ShowVote實(shí)現(xiàn)用戶查看投票情況;控件VoteMessage顯示用戶投票的操作結(jié)果。頁(yè)面WebOnlinVote.aspx的設(shè)計(jì)界面如圖6所示。
圖6 頁(yè)面WebOnlinVote.aspx的設(shè)計(jì)界面
頁(yè)面WebOnlinVote.aspx的HTML設(shè)計(jì)代碼如下:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="WebOnlinVote.aspx.cs" Inherits="WebOnlinVote" %>
<HTML><HEAD><title>網(wǎng)絡(luò)在線投票系統(tǒng)</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>
1.頁(yè)面初始化
頁(yè)面WebOnlinVote.aspx調(diào)用函數(shù)Page_Load(Object sender,EventArgs e)初始化,該函數(shù)調(diào)用函數(shù)BindVoteListData()從數(shù)據(jù)庫(kù)投票表Votes中獲取所有投票項(xiàng)目的信息,并把獲取的數(shù)據(jù)設(shè)置為數(shù)據(jù)網(wǎng)格控件VoteList的數(shù)據(jù)源。函數(shù)Page_Load(Object sender,EventArgs e)和函數(shù)BindVoteListData()的程序代碼如下:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{ //綁定投票的項(xiàng)目
BindVoteListData();
VoteMessage.Visible = false;
}
}
private void BindVoteListData()
{ //獲取所有數(shù)據(jù)
WebVote.Vote vote = new Vote();
SqlDataReader recv = vote.GetVotes();
//設(shè)置控件的數(shù)據(jù)源,并綁定數(shù)據(jù)
VoteList.DataSource = recv;
VoteList.DataBind();
recv.Close(); //關(guān)閉數(shù)據(jù)讀取器
}
網(wǎng)絡(luò)在線投票系統(tǒng)運(yùn)行之后,投票頁(yè)面WebOnlinVote.aspx的初始化界面如圖7所示,此時(shí)顯示被投票的項(xiàng)目信息。
圖7 投票頁(yè)面WebOnlinVote.aspx的初始化界面
2.投票功能
用戶單擊頁(yè)面WebOnlinVote.aspx中的【我要投票】按鈕和【查看投票】按鈕分別觸發(fā)事件VoteBtn_Click(object sender, System.EventArgs e)和事件ShowVote_Click(object sender, System.EventArgs e),它們分別實(shí)現(xiàn)用戶投票功能和查看投票功能。在投票事件中,事件首先檢查用戶對(duì)哪些項(xiàng)目進(jìn)行了投票,然后更改項(xiàng)目的票數(shù)。在查看投票事件中,事件重定向到頁(yè)面ShowVoteInfo.aspx。事件VoteBtn_Click(object sender, System.EventArgs e)和事件ShowVote_Click(object sender, System.EventArgs e)的程序代碼如下:
private void VoteBtn_Click(object sender, System.EventArgs e)
{ //定義類
WebVote.Vote vote = new Vote();
try
{ //添加用戶的投票的項(xiàng)目
foreach(DataGridItem item in VoteList.Items)
{ //查找每個(gè)投票項(xiàng)目的選擇控件
CheckBox check = (CheckBox)item.FindControl("VoteCheck");
if(check != null)
{ //說(shuō)明用戶已經(jīng)投票,則需要添加這一票
if(check.Checked == true)
{ //修改數(shù)據(jù)庫(kù)中的票數(shù)
vote.UpdateVote(Int32.Parse(
VoteList.DataKeys[item.ItemIndex].ToString()));
VoteMessage.Visible = true; //顯示用戶投票操作的結(jié)果
}
}
}
//顯示操作結(jié)果信息
Response.Write("<script>window.alert('
投票成功,感謝您的參與!!!')</script>");
}
catch (Exception ex)
{ //顯示修改操作中的失敗、錯(cuò)誤信息
Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
+ ASPNET2System.RedirectErrorUrl(Request.RawUrl)
+ "&ErrorMessage=" + ex.Message.Replace("\n", " "));
}
}
private void ShowVote_Click(object sender, System.EventArgs e)
{ //導(dǎo)向查看投票結(jié)果頁(yè)面
Response.Redirect("~/ShowVoteInfo.aspx");
}
顯示投票結(jié)果頁(yè)面設(shè)計(jì)
在應(yīng)用程序WebVote中添加一個(gè)新的Web頁(yè)面,并命名為ShowVoteInfo.aspx,它的代碼隱藏文件為ShowVoteInfo.aspx.cs文件。
1.頁(yè)面設(shè)計(jì)
在頁(yè)面ShowVoteInfo.aspx上添加一個(gè)數(shù)據(jù)網(wǎng)格控件、一個(gè)Label控件和一個(gè)Button控件,它們的名稱分別為VoteList、VoteMessage、WebOnlineVoteBtn。控件VoteList用來(lái)顯示參與投票的項(xiàng)目的投票情況,并計(jì)算各個(gè)投票項(xiàng)目所占的百分比;控件VoteMessage顯示用戶投票的總票數(shù);控件WebOnlineVoteBtn實(shí)現(xiàn)投票頁(yè)面WebOnlinVote.aspx。頁(yè)面ShowVoteInfo.aspx的設(shè)計(jì)界面如圖8所示。
?
圖8 頁(yè)面ShowVoteInfo.aspx的設(shè)計(jì)界面
頁(yè)面ShowVoteInfo.aspx的HTML設(shè)計(jì)代碼如下:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="ShowVoteInfo.aspx.cs" Inherits="ShowVoteInfo" %>
<HTML><HEAD><title>網(wǎng)絡(luò)在線投票系統(tǒng)</title></HEAD>
<asp:DataGrid ID="VoteList" Runat="server" CssClass="Normal"
AutoGenerateColumns="False" DataKeyField="VoteID">
<HeaderStyle BackColor="Orange"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="投票項(xiàng)目">
<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="票數(shù)">
<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="返回投票頁(yè)面" CssClass="ButtonCss"
OnClick="WebOnlineVoteBtn_Click"></asp:button>
</HTML>
2.頁(yè)面初始化
頁(yè)面ShowVoteInfo.aspx調(diào)用函數(shù)Page_Load(Object sender,EventArgs e)初始化。該函數(shù)調(diào)用函數(shù)BindVoteListData()從數(shù)據(jù)庫(kù)投票表Votes中獲取所有投票的項(xiàng)目,并把獲取的數(shù)據(jù)綁定到數(shù)據(jù)網(wǎng)格控件VoteList。函數(shù)Page_Load(Object sender,EventArgs e)還調(diào)用函數(shù)SetVoteTotal()從數(shù)據(jù)庫(kù)中獲取投票的總票數(shù)。函數(shù)Page_Load(Object sender,EventArgs e)、函數(shù)SetVoteTotal()和函數(shù)BindVoteListData()的程序代碼如下:
int voteTotal = 0;
private void Page_Load(object sender, System.EventArgs e)
{ //設(shè)置總票數(shù)voteTotal
SetVoteTotal();
if(!Page.IsPostBack)
{ //顯示用戶投票的具體情況
BindVoteListData();
VoteMessage.Text = "總票數(shù)為:" + voteTotal.ToString();
}
}
private void SetVoteTotal()
{ //獲取所有數(shù)據(jù)
WebVote.Vote vote = new Vote();
SqlDataReader recv = vote.GetVotes();
voteTotal = 0;
//讀取每一個(gè)參與投票的項(xiàng)目,并計(jì)算票數(shù)總和
while(recv.Read())
{ //計(jì)算它們的總和
voteTotal += Int32.Parse(recv["VoteCount"].ToString());
}
recv.Close();
}
private void BindVoteListData()
{ //獲取數(shù)據(jù)
WebVote.Vote vote = new Vote();
SqlDataReader recv = vote.GetVotes();
//設(shè)置控件的數(shù)據(jù)源,并綁定控件的數(shù)據(jù)
VoteList.DataSource = recv;
VoteList.DataBind();
recv.Close();
}
頁(yè)面ShowVoteInfo.aspx初始化時(shí)(即數(shù)據(jù)網(wǎng)格控件VoteList綁定數(shù)據(jù)時(shí)),分別調(diào)用函數(shù)FormatVoteCount(String voteCount)和函數(shù)FormatVoteImage(int voteCount)來(lái)計(jì)算每個(gè)投票項(xiàng)目所占的百分比和圖像的長(zhǎng)度(繪制比例圖片)。函數(shù)FormatVoteCount(String voteCount)和函數(shù)FormatVoteImage(int voteCount)的程序代碼如下:
public int FormatVoteCount(String voteCount)
{ //如果投票沒(méi)有被投票
if(voteCount.Length <= 0)
{ //返回0個(gè)百分比
return(0);
}
if(voteTotal > 0)
{ //返回實(shí)際的百分比
return((Int32.Parse(voteCount)* 100/voteTotal));
}
return(0);
}
public int FormatVoteImage(int voteCount)
{ //返回百分比的圖像的長(zhǎng)度
return(voteCount * 3);
}
網(wǎng)絡(luò)在線投票系統(tǒng)運(yùn)行之后,顯示投票結(jié)果頁(yè)面ShowVoteInfo.aspx的初始化界面如圖9所示,此時(shí)顯示各個(gè)項(xiàng)目的投票結(jié)果。
?
圖9 某個(gè)時(shí)候的投票結(jié)果頁(yè)面ShowVoteInfo.aspx
ASP.NET2SQLHelper 下載
?
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/mengyao/archive/2007/08/20/1751935.aspx
轉(zhuǎn)載于:https://www.cnblogs.com/China-Dragon/archive/2010/03/12/1684113.html
總結(jié)
以上是生活随笔為你收集整理的用ASP.NET 2.0设计网络在线投票系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用cvs或svn从sourceforg
- 下一篇: WinForm中使用WPF的控件