asp.net c投票系统(防刷功能)
asp教程.net c投票系统(防刷功能)
本文章是利用了asp.net教程 mssql做的一款防刷投票系统,他是利用了cookie来实现的。
*/
using system;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using system.data.sqlclient;public partial class _default : system.web.ui.page
{
protected string title = "1";protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
votefun();
}
}protected void votefun()
{
sqlconnection conn = dbconn.conn();
sqlcommand cmd = new sqlcommand("select vitems from votetest where vid = '" + title + "'", conn);
try
{
conn.open();
this.showtitle.text = convert.tostring(cmd.executescalar());
cmd.commandtext = "select * from votedetails where vid ='" + title + "'";
this.rbl.datasource = cmd.executereader();
this.rbl.datatextfield = "contents";
this.rbl.datavaluefield = "id";
this.rbl.databind();
}
catch (sqlexception ex)
{
response.write(ex.errors.tostring());
}
finally
{
conn.close();
cmd.dispose();
}
}
// 投票
protected void butvoteyes_click(object sender, eventargs e)
{
if (this.rbl.selectedindex != -1)
{
//投票防作弊
httpcookie makecookie = new httpcookie("vote");//制造cookie
httpcookie readcookie = request.cookies["vote"];//读出cookie
//response.write("<hr/>" + title);
if (readcookie == null)//从未投过票
{
makecookie.values.add("voteitem", title);
}
else
{
string strallitem = readcookie.values["voteitem"].tostring();//读取已投票的项
if (strallitem.indexof(title) == -1)//未投过票
{
makecookie.values.add("voteitem", readcookie.values["voteitem"] + title);
}
else//如果已投过票
{
response.write("<script language=网页特效>alert('该主题你已经成功投过票,不能重新投票!');</script>");
return;}
}
response.appendcookie(makecookie);
string selectid = this.rbl.selectedvalue.tostring();
sqlconnection conn = dbconn.conn();
sqlcommand cmd = new sqlcommand("update votedetails set num = num +1 where id = '" + selectid + "'", conn);
try
{
conn.open();
cmd.executenonquery();
showmessage.box("投票成功!");
}
catch (sqlexception ex)
{
response.write(ex.errors.tostring());
}
finally
{
conn.close();
cmd.dispose();
}
}
else
{
showmessage.box("请选择投票项目");
}
}
/// <summary>
/// 查看结果
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void butvoteno_click(object sender, eventargs e)
{
response.redirect("showvotedetails.aspx?vid="+title+"");
}
#region 测试代码
//public static string m_str_voteid;
//protected void page_load(object sender, eventargs e)
//{
// if (!ispostback)
// {
// //获取用户选择的投票标题
// m_str_voteid = request["voteid"];
// //绑定投票标题和投票选项
// labbind();
// rblbind();
// }
//}
////绑定label控件
//private void rblbind()
//{
// dataset ds = db.reds("select votetitle from tb_vote where voteid=" + m_str_voteid);
// labvotetitle.text = ds.tables[0].rows[0][0].tostring();
//}
////绑定radiobuttonlist控件
//private void labbind()
//{
// dataset ds = db.reds("select * from tb_vote where voteid=" + m_str_voteid);
// rblvoteitem.datasource = ds;
// //votecontent字段绑定到text,voteitemid绑定到value
// rblvoteitem.datatextfield = "votecontent";
// rblvoteitem.datavaluefield = "voteitemid";
// rblvoteitem.databind();
//}
////投票按钮
//protected void btnvote_click(object sender, eventargs e)
//{
// //投票防作弊
// httpcookie makecookie = new httpcookie("vote" + m_str_voteid);//创建cookie
// httpcookie readcookie = request.cookies["vote" + m_str_voteid];//读取cookie
// if (readcookie == null)
// {
// //从未投过票的话,将参与投票的标题放进cookie,并设置过期时间
// makecookie.values.add("voteitem", "<" + m_str_voteid + ">");
// makecookie.expires = datetime.maxvalue;
// }
// else
// {
// //读取已投票的项
// string p_str_allitem = readcookie.values["voteitem"].tostring();
// //如果未对该主题投过票,将该主题添加到cookie中
// if (p_str_allitem.indexof("<" + m_str_voteid + ">") == -1)
// {
// makecookie.values.add("voteitem", readcookie.values["voteitem"] + "<" + m_str_voteid + ">");
// }
// else
// {
// response.write("<script language=javascript>alert('该主题你已经投过票,不能重新投票!');</script>");
// //这里return很关键,直接跳出btnvote_click事件
// return;
// }
// }
// //执行投票操作,票数+1
// string p_str_voteitemid = this.rblvoteitem.selectedvalue;
// string p_str_cmdtxt = "update tb_voteitem set votetotal=votetotal+1 where voteitemid=" + p_str_voteitemid + " and voteid=" + m_str_voteid;
// bool p_bl_reval = db.exsql(p_str_cmdtxt);
// if (p_bl_reval)
// {
// //写入cookie
// response.appendcookie(makecookie);
// //在新窗口中弹出投票结果
// response.write("<script language=javascript>alert('投票成功,感谢你的支持!');windows.open('voteresult.aspx?voteid=" + m_str_voteid + "&title=" + server.urlencode(labvotetitle.text) + "','new');</script>");
// }
// else
// {
// response.write("<script language=javascript>alert('投票失败!');</script>");
// }
//}
////查看结果按钮
//protected void btnresult_click(object sender, eventargs e)
//{
// response.write("<script language=javascript>alert('投票成功,感谢你的支持!');windows.open('voteresult.aspx?voteid=" + m_str_voteid + "&title=" + server.urlencode(labvotetitle.text) + "','new');</script>");
//}
#endregion
}
补充:asp.net教程,.Net开发