.NET2.0学习笔记之--在线投票系统

首先说明一下,这个系统在网上的版本很多,感觉都是不完整的,这是我个人结合网上的一些资料和此系统的原教材而写出来的,毕竟书上和网上的错漏是有的...VS2005+SQL2000+XPSP2下运行成功...

一、系统功能设计和数据库设计

  1、系统功能设计和数据库设计

  1.1 系统功能设计

  网络在线投票系统实现的功能比较简单,具体如下:

  ◎投票项目的管理;

  ◎添加投票的项目;

  ◎删除投票的项目;

  ◎对项目进行投票;

  ◎查看项目的投票情况。
1.2 数据库设计

  本系统的数据库设计比较简单,只需要存储投票的信息即可。在SQL Server 2000中创建一个数据库,名称为“WebVoteDB”,并在该数据库中创建投票项目表Votes。其中“VoteID”字段存储投票项目ID;“Item”字段存储投票项目的名称;“VoteCount”字段存储每个项目的票数。创建投票项目表Votes的操作界面如图1所示。

  投票项目表Votes需要存储投票项目名称及其票数,表的字段说明如表1所示。


图1 创建投票项目表Votes的操作界面

注释表1,VoteID为主键,初始为1.自动增量为1.

线投票功能是网站应用程序最常用的功能之一,也是网站应用程序开发常用的功能模块。当网站的管理员或用户提出一些新的想法与建议或者出现一种新产品时,他们可能需要通过用户或者客户的投票方式来确定这些新的想法、建议或者新的产品是否满足用户或者客户的需求,另外,网站还可以通过网站在线投票功能做一些实际性的调查工作。本章介绍的网络在线投票系统还以直观的图形化界面显示投票信息,而且还可以及时查看投票的情况。

  二、投票系统实现

  创建好系统所需要的数据库之后,网络在线投票系统的具体实现可以分为下面3个部分:

  (1)存储过程的实现部分;

  (2)数据库访问层的实现部分;

  (3)功能页面的实现部分。

  下面将详细介绍上述3个部分的具体实现方法。首先在Microsoft Visual Studio .NET 2005中创建一个Web站点,名称为“WebVote”。

  2.1 存储过程设计

  在数据库WebVoteDB中创建存储过程Pr_GetVotes、Pr_GetSingleVote、Pr_AddVote、Pr_UpdateVote和Pr_DeleteVote。其中:

  Pr_GetVotes 从投票项目表Votes中获取所有投票项目的信息;

  Pr_GetSingleVote 从投票项目表Votes中获取某一条投票项目的信息;

  Pr_AddVote 添加一条新记录到投票项目表Votes中;

  Pr_UpdateVote 更新参与投票项目的票数;

  Pr_DeleteVote 从投票项目表Votes中获取删除一条投票项目信息。

  以上各存储过程的程序代码如下:

/* 存储过程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,VoteCount)

 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  

2.2 数据库访问层设计

  在应用程序WebVote中添加访问投票表Votes的类Vote,该类封装对投票项目表Votes中记录的选择、添加、修改和删除的方法。其中:

  方法GetVotes() 从投票项目表Votes中获取所有投票项目的信息;

  方法AddVote(String sItem) 添加一条新记录到投票项目表Votes中;

  方法UpdateVote(int nVoteID) 更新参与投票项目的票数;

  方法DeleteVote(int nVoteID) 从投票项目表Votes中获取删除一条投票项目信息。

  类Vote的程序设计代码如下:

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);
   }
  }
 }
系统主页面设计

  在应用程序WebVote中添加一个新的Web页面,并命名为Default.aspx,它的代码隐藏文件为Default.aspx.cs。

  在页面Default.aspx上添加3个超链接控件,名称分别为ItemManageLink、OnlineVoteLink、ViewVoteLink。它们分别实现跳转投票项目管理页面VoteItemManage.aspx、投票页面WebOnlinVote.aspx、投票结果页面ShowVoteInfo.aspx。页面Default.aspx的代码如下,至于界面的布局,由个人另行设计.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>欢迎您进入在线投票系统</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center">
        <strong><span style="font-size: 24pt; color: #0000ff; background-color: olive;">在线投票系统</span></strong><br />
        <asp:HyperLink ID="ItemManageLink" runat="server" Width="768px" ForeColor="#003333" NavigateUrl="~/VoteItemManage.aspx" Font-Bold="True" style="background-color: aqua">投票项目管理</asp:HyperLink><br />
        <asp:HyperLink ID="OnlineVoteLink" runat="server" Width="768px" ForeColor="#003333" NavigateUrl="~/WebOnlinVote.aspx" Font-Bold="True" style="background-color: teal">网站在线投票</asp:HyperLink><br />
        <asp:HyperLink ID="ViewVoteLink" runat="server" Width="768px" ForeColor="#003333" NavigateUrl="~/ShowVoteInfo.aspx" Font-Bold="True" style="background-color: yellow">查看投票结果</asp:HyperLink>&nbsp;</div>
    </form>
</body>
</html>
投票项目管理页面设计

  在应用程序WebVote中添加一个新的Web页面,并命名为VoteItemManage.aspx,它的代码隐藏文件为VoteItemManage.aspx.cs文件。

  1.页面设计

  在页面VoteItemManage.aspx上添加一个列表控件、一个Button控件、一个TextBox控件和一个ImageButton控件,它们的名称分别为ItemList、AddBtn、Item和deleteBtn。控件ItemList显示投票项目表中的所有数据;控件AddBtn实现添加一个新的投票项目;控件Item用来输入新的投票项目名称;控件deleteBtn删除一个投票项目。页面ItemManage.aspx的设计界面自行设计.代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="VoteItemManage.aspx.cs" Inherits="VoteItemManage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>网络在线投票系统管理</title>
</head>
<body bgcolor="#ffcc66">
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ItemList" runat="server" Height="228px" Style="background-color: gray"
            Width="173px"></asp:ListBox>
        <asp:ImageButton ID="deleteBtn" runat="server" AlternateText="删除此项" OnClick="deleteBtn_Click" Height="76px"
            Style="left: -1px; position: relative; top: -63px" Width="24px" /><br />
        请在下面输入新投票项目的名称:<br />
        <asp:TextBox ID="Item" runat="server"></asp:TextBox>
        &nbsp; &nbsp;&nbsp;
        <asp:Button ID="AddBtn" runat="server" EnableTheming="True" Text="增加新的投票项目" OnClick="AddBtn_Click" /><br />
   
    </div>
    </form>
</body>
</html>
 2.页面初始化

  页面VoteItemManage.aspx调用函数Page_Load(Object sender,EventArgs e)初始化,该函数调用函数BindVoteListData()从数据库投票表Votes中获取所有投票的项目,并把获取的数据绑定到列表控件ItemList。函数Page_Load(Object sender,EventArgs e)和函数BindVoteListData()的程序代码如下:
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindVoteListData();
        }
    }
    private void BindVoteListData()
    {
       Vote vote = new Vote();
        SqlDataReader recv = vote.GetVotes();
        ItemList.DataTextField = "Item";
        ItemList.DataValueField = "VoteID";
        ItemList.DataSource = recv;
        ItemList.DataBind();
        recv.Close();
    }
    protected void AddBtn_Click(object sender, EventArgs e)
    {
        if (Item.Text.Length > 0)
        {
           Vote vote = new Vote();
            try
            {
                vote.AddVote(Item.Text.Trim());
                BindVoteListData();
                Response.Write("<script>window.alert('" + 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;
        }
        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", " "));
        }
    }

 投票页面设计

  在应用程序WebVote中添加一个新的Web页面,并命名为WebOnlineVote.aspx,它的代码隐藏文件为WebOnlineVote.aspx.cs文件。

  1.页面设计

  在页面WebOnlineVote.aspx上添加一个数据网格控件、两个Button控件和一个Label控件,它们的名称分别为VoteList、VoteBtn、ShowVote和VoteMessage。控件VoteList用来显示参与投票的所有项目;控件VoteBtn提交用户的投票;控件ShowVote实现用户查看投票情况;控件VoteMessage显示用户投票的操作结果。页面WebOnlinVote.aspx的设计界面自行设计,代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebOnlinVote.aspx.cs" Inherits="WebOnlinVote" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>网络在线投票系统</title>
</head >
<body bgcolor="#74a0d0" >
    <form id="form1" runat="server">
    <div>
        <asp:DataGrid ID="VoteList" runat="server" AutoGenerateColumns="false" DataKeyField="VoteID" style="left: 206px; position: relative; top: 7px">
        <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" />
        </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>
    </div>
    <br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<asp:Button ID="VoteBtn" runat="server" Text="我要投票"
            Width="100" OnClick="VoteBtn_Click" />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<asp:Button ID="ShowVote" runat="server"
            Text="查看投票" Width="100" OnClick="ShowVote_Click" />
        &nbsp;&nbsp;<br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp;&nbsp;
        <asp:Label ID="VoteMessage" runat="server" Text="谢谢您的参与!投票成功!" Width="236px" Font-Bold="True" ForeColor="Red" Visible="False"></asp:Label>
    </form>
</body>
</html>
1.页面初始化

  页面WebOnlinVote.aspx调用函数Page_Load(Object sender,EventArgs e)初始化,该函数调用函数BindVoteListData()从数据库投票表Votes中获取所有投票项目的信息,并把获取的数据设置为数据网格控件VoteList的数据源。函数Page_Load(Object sender,EventArgs e)和函数BindVoteListData()的程序代码如下:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindVoteListData();
            VoteMessage.Visible = false;
        }
    }
    private void BindVoteListData()
    {
        Vote vote = new Vote();
        SqlDataReader recv = vote.GetVotes();
        VoteList.DataSource = recv;
        VoteList.DataBind();
        recv.Close();
    }
    protected void VoteBtn_Click(object sender, EventArgs e)
    {
        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", " "));
        }
    }
    protected void ShowVote_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/ShowVoteInfo.aspx");
    }

<<待续>>....

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值