📄 vote.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Vote : System.Web.UI.Page
{
public string strContent, strID = null;
protected void Page_Load(object sender, EventArgs e)
{
strID = Request["ID"].ToString();
HttpCookie readcookie = Request.Cookies["Vote"];
if (readcookie == null)
{
this.labState.Text = "你还未在该投票系统中投票!";
}
else
{
this.labState.Text = "你已经在该投票系统投过票了!";
}
SqlData da = new SqlData();
SqlDataReader dr = da.ExceRead("select * from tb_Vote where ID='" + strID + "'") ;
dr.Read();
this.labContent.Text = dr["Title"].ToString();
strContent = dr["Content"].ToString();
string[] str = strContent.Split('|');
int intORDER = 0;//排序
if (!Page.IsPostBack)//有提交行为,不执行以下数据的重载
{
foreach (string strContentIN in str)
{
string strContentOK = strContentIN.Split(',')[1];
this.RadioButtonList1.Items.Add(new ListItem(strContentOK, intORDER.ToString()));
intORDER++;//累加选项的值
}
}
}
protected void btnVote_Click(object sender, EventArgs e)
{
if (this.RadioButtonList1.SelectedIndex != -1)//如果有选定项目
{
//投票防作弊
HttpCookie makecookie = new HttpCookie("Vote");//制造cookie
HttpCookie readcookie = Request.Cookies["Vote"];//读出cookie
if (readcookie == null)//从未投过票
{
makecookie.Values.Add("VoteItem", "<" + strID + ">");
}
else
{
//Response.Write(readcookie.Values["VoteItem"]);
//Response.End();
string strAllItem = readcookie.Values["VoteItem"].ToString();//读取已投票的项
if (strAllItem.IndexOf("<" + strID + ">") == -1)//未投过票
{
makecookie.Values.Add("VoteItem", readcookie.Values["VoteItem"] + "<" + strID + ">");
}
else//如果已投过票
{
Response.Write("<script language=javascript>alert('该主题你已经成功投过票,不能重新投票!');</script>");
return;
//Response.Write("<script language=javascript>alert('该主题你已经成功投过票,不能重新投票!');location='Index.aspx'</script>");
}
}
Response.AppendCookie(makecookie);//写入Cookie
string strSelect = strContent.Split('|')[Convert.ToInt32(this.RadioButtonList1.SelectedValue.ToString())];//当前选中的项目,如“ 2,不错”
int intVote = Convert.ToInt32(strSelect.Split(',')[0]);//当前投票数+1
int intVoteOK = intVote + 1;
string strSelectOK = strSelect.Replace(intVote.ToString(), intVoteOK.ToString());//更新原投票数,所以期望更新的定位精确,可附带+","
string strUPDATE = strContent.Replace(strSelect, strSelectOK);//得到最终
SqlData da = new SqlData();
bool update = da.ExceSQL("update tb_Vote set Content='" + strUPDATE + "' where ID='" + strID + "'");
if (update)
{
Response.Write("<script language=javascript>alert('投票成功!');</script>");
}
else
{
Response.Redirect("Index.aspx");
}
}
else//如果没有选中项目
{
Response.Write("<script language=javascript>alert('请选择投票项!');location='javascript:history.go(-1)'</script>");
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx?ID="+Request["ID"]+"");
}
protected void btnView_Click(object sender, EventArgs e)
{
Response.Redirect("Result.aspx?ID=" + Request["ID"] + "");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -