一、数据库脚本
需要建立两张表,脚本如下:
create table VoteTitle --投票主题表
(
TitleID int identity primary key,--主题编号
Title varchar(200) not null,--主题信息
SumNumber int default 0,--主题的所有项目投票总数
Mode bit default 0,--0为单选,1为复选,默认为0
Status bit default 0--0表示关闭主题,1为打开主题,默认为0
)
go
insert into VoteTitle(Title,Mode,Status)values('你对我们网站评价如何?',0,1)
insert into VoteTitle(Title,Mode,Status)values('请选择你喜欢的编程语言',0,0)
go
create table VoteItem--投票主题项目表
(
ItemID int identity primary key,--项目编号
Item varchar(200)not null,--项目信息
Number int default 0,--项目的投票总数
TitleID int references VoteTitle(TitleID)on delete cascade on update cascade
--设置级联关系,当对父表中的TtileID进行删除和更新时,会影响子表
)
go
insert into VoteItem(Item,TitleID)values('非常好','1')
insert into VoteItem(Item,TitleID)values('好','1')
insert into VoteItem(Item,TitleID)values('一般','1')
insert into VoteItem(Item,TitleID)values('差','1')
insert into VoteItem(Item,TitleID)values('很差','1')
go
insert into VoteItem(Item,TitleID)values('asp,net web开发语言','2')
insert into VoteItem(Item,TitleID)values('C# 窗体开发语言','2')
insert into VoteItem(Item,TitleID)values('html页面语言','2')
insert into VoteItem(Item,TitleID)values('JS脚本语言','2')
go
create trigger trigger_VoteItem on VoteItem for update--建立触发器
as
begin
if update(Number)--当项目表的投票更新时
begin
update VoteTitle set SumNumber=SumNumber+1 where TitleID=(select TitleID from inserted)
end
end
--触发器结束
go
select * from VoteTitle
select * from VoteItem
go
注意,这里只进行第一个主题的讨论,也就是说默认是让用户对网站进行评价,关于投票选择喜欢语言的主题这里不演示。
表的效果图如下:
二、页面编辑,这里有两张表,第一张是显示投票的页面ShowVote.aspx,第二张是显示投票结果的页面LookVote.aspx
ShowVote.aspx页面的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace VoteSystem
{
public partial class ShowVote : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{ //数据库操作
string str="Server=.;DataBase=Exercise;uid=sa;pwd=;";
SqlConnection conn = new SqlConnection(str);
string sql="select * from VoteTitle where Status=1";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "TitleTable");
sql = "select *from VoteItem where TitleID=(select TitleID from VoteTitle where Status=1)";
da = new SqlDataAdapter(sql, conn);
da.Fill(ds, "ItemTable");
DataView dv = ds.Tables["ItemTable"].DefaultView;
dv.Sort = "ItemID asc";
//获取主题
lblTitle.Text = ds.Tables["TitleTable"].Rows[0]["Title"].ToString();
//获取启用的主题的Mode来决定是用单选框还是复选框,这里我默认的主题是进行网站评论,那么就默认是RadiobuttonList(即单选框)
bool Mode = Convert.ToBoolean(ds.Tables["TitleTable"].Rows[0]["Mode"]);
hfMode.Value = Mode.ToString();
if (Mode)
{
//表示选择的是复选
CheckBoxList obj = new CheckBoxList();
obj.BackColor = System.Drawing.ColorTranslator.FromHtml("#EDEDED");
obj.ID = "listVoteItem";
obj.Width = 180;
obj.DataSource = dv;
obj.DataTextField = "Item";
obj.DataValueField = "ItemID";
obj.DataBind();
Panel1.Controls.Add(obj);
}
else
{
//表示添加的是单选
RadioButtonList obj = new RadioButtonList();
obj.DataSource = dv;
obj.ID = "listVoteItem";
obj.DataTextField = "Item";
obj.DataValueField = "ItemID";
obj.DataBind();
Panel1.Controls.Add(obj);
}
}
//投票按钮
protected void btnVote_Click(object sender, EventArgs e)
{
string pass = string.Empty;
if (Request.Cookies["pass"] == null)
{
pass = "";
}
else
{
pass = Request.Cookies["pass"].Value.ToString();//读取客户端cookies
}
if (pass == "pass")
{
Response.Write("<script language='javascript'>alert('你每天只能投票一次');history.back();</script>");
Response.End();
}
else
{
string str = "Server=.;DataBase=Exercise;uid=sa;pwd=;";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
bool Mode = Convert.ToBoolean(hfMode.Value);//hfMode是隐藏控件,用于记录Mode
if (Mode)
{
CheckBoxList rb = (CheckBoxList)Panel1.FindControl("listVoteItem");
if (rb.SelectedIndex == -1)
{
Response.Write("<script language='javascript'>alert('你必须至少选择一项!');history.back();</script>");
Response.End();
}
else
{
//表示用户进行了选择
foreach (ListItem item in rb.Items)
{
if (item.Selected)
{
cmd.CommandText = "update VoteItem set Number=Number+1 where ItemID=" + item.Value.ToString();
cmd.ExecuteNonQuery();
}
}
Response.Cookies["pass"].Value = "pass";
Response.Cookies["pass"].Expires = DateTime.Now.AddDays(1);
Response.Write("<script language='javascript'>alert('投票成功!');history.back();</script>");
Response.End();
}
}
else
{
//表示添加的是单选
RadioButtonList rb = (RadioButtonList)Panel1.FindControl("listVoteItem");
if (rb.SelectedIndex == -1)
{
Response.Write("<script language='javascript'>alert('你必须至少选择一项!');history.back();</script>");
Response.End();
}
else
{
//表示用户进行了选择
foreach (ListItem item in rb.Items)
{
if (item.Selected)
{
cmd.CommandText = "update VoteItem set Number=Number+1 where ItemID=" + item.Value.ToString();
cmd.ExecuteNonQuery();
}
}
Response.Cookies["pass"].Value = "pass";
Response.Cookies["pass"].Expires = DateTime.Now.AddDays(1);
Response.Write("<script language='javascript'>alert('投票成功!');history.back();</script>");
Response.End();
}
conn.Close();
conn.Dispose();
}
}
}
//查看按钮
protected void btnLook_Click(object sender, EventArgs e)
{
Response.Redirect("LookVote.aspx");
}
}
}
由于LookVote.aspx页面需要有一些样式布局,毕竟要显示给用户,所以先给出Html代码,代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LookVote.aspx.cs" Inherits="VoteSystem.LookVote" %>
<!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="height: 382px">
<asp:Label ID="lblInfo" runat="server" Text="lblInfo"></asp:Label>
<br />
<asp:Label ID="lblTitle" runat="server" Text="lblTitle"></asp:Label>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table style="color:#333333; text-align:left; border:0 width:90%; background-color:#b18a02;
font-size:12px; font-family:宋体;"
cellpadding="2" cellspacing="1">
<tr style="height:25px; background-color:#FFFFFF; text-align:center;">
<td style="width:50px;"> </td>
<td style="width:170px;">选项</td>
<td style="width:170px;">比例</td>
<td style="width:170px;">票数</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="height:25px; background-color:#FFFFFF; text-align:left;">
<td style=" text-align:center;"><%#DataBinder.Eval(Container.DataItem,"ItemID") %>
</td>
<td><%#DataBinder.Eval(Container.DataItem,"Item") %>
</td>
<td><%#DataBinder.Eval(Container.DataItem,"ItemPercent","<img src='Images/1.jpg' height='15' width='{0}'>") %>
<%#DataBinder.Eval(Container.DataItem,"ItemPercent","{0}%") %>
</td>
<td style=" text-align:center;"><%#DataBinder.Eval(Container.DataItem,"Number") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr style="height:25px; background-color:#FFFFFF; text-align:center;">
<td colspan="4">
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="javascript:winow.print();return false;">[打印]
</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClientClick="javascript:winow.close();return false;">[关闭]
</asp:LinkButton>
</td>
</tr>
</table></FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
其中,<td><%#DataBinder.Eval(Container.DataItem,”ItemPercent”,”<img src=’Images/1.jpg’ height=’15’ width='{0}’>”) %> <%#DataBinder.Eval(Container.DataItem,”ItemPercent”,”{0}%”) %> </td>
这个单元格实现了最核心的柱形图的显示。
下面是LookVote.aspx的后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace VoteSystem
{
public partial class LookVote : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = "Server=.;DataBase=Exercise;uid=sa;pwd=;";
SqlConnection conn = new SqlConnection(str);
string sql = "select a.Title,a.SumNumber,a.Mode,a.Status,b.*,case a.SumNumber when 0 then 0 else round(cast(b.Number as float)/cast(a.SumNumber as float)*100,2) end as ItemPercent from VoteTitle a,VoteItem b where a.TitleID=b.TitleID and a.Status=1";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
Repeater1.DataSource = ds.Tables[0].DefaultView;
Repeater1.DataBind();
lblInfo.Text = "共有" + ds.Tables[0].Rows[0]["SumNumber"].ToString() + "人参加投票";
lblTitle.Text = ds.Tables[0].Rows[0]["Title"].ToString();
}
}
}
这部分的重点是SQL语句,string sql = “select a.Title,a.SumNumber,a.Mode,a.Status,b.*,case a.SumNumber when 0 then 0 else round(cast(b.Number as float)/cast(a.SumNumber as float)*100,2) end as ItemPercent from VoteTitle a,VoteItem b where a.TitleID=b.TitleID and a.Status=1”;
为了求出百分比,必须将投票数(整型)转换为浮点型,避免整型除以整型=整型的错误结果。另外还需用 when 表达式 then 结果 这样的语句来描述特殊情况(因为如果投票数0的话,就不能用项目票数/主题总票数这种单一的公式来计算了)
原设计图如下:
投票页面的后台外观
投票结果显示的页面外观如下:
最后这个小程序的运行效果如下:
今天的文章网站投票系统!分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/33096.html