温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:大学生调查投票系统源码
当前文件:
StudentVote/Vote.aspx.cs,打开代码结构图
StudentVote/Vote.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.Data.SqlClient; 12
13
public partial class Vote : System.Web.UI.Page 14
{ 15
public string strContent, strID = null; 16
protected void Page_Load(object sender, EventArgs e) 17
{ 18
strID = Request["ID"].ToString(); 19
20
HttpCookie readcookie = Request.Cookies["Vote"]; 21
22
if (readcookie == null) 23
{ 24
this.labState.Text = "你还未在该投票系统中投票!"; 25
} 26
else 27
{ 28
this.labState.Text = "你已经在该投票系统投过票了!"; 29
} 30
SqlData da = new SqlData(); 31
SqlDataReader dr = da.ExceRead("select * from tb_Vote where ID='" + strID + "'") ; 32
dr.Read(); 33
this.labContent.Text = dr["Title"].ToString(); 34
strContent = dr["Content"].ToString(); 35
string[] str = strContent.Split('|'); 36
int intORDER = 0;//排序 37
if (!Page.IsPostBack)//有提交行为,不执行以下数据的重载 38
{ 39
foreach (string strContentIN in str) 40
{ 41
string strContentOK = strContentIN.Split(',')[1]; 42
this.RadioButtonList1.Items.Add(new ListItem(strContentOK, intORDER.ToString())); 43
intORDER++;//累加选项的值 44
} 45
} 46
} 47
48
49
protected void btnVote_Click(object sender, EventArgs e) 50
{ 51
if (this.RadioButtonList1.SelectedIndex != -1)//如果有选定项目 52
{ 53
//投票防作弊 54
HttpCookie makecookie = new HttpCookie("Vote");//制造cookie 55
HttpCookie readcookie = Request.Cookies["Vote"];//读出cookie 56
57
if (readcookie == null)//从未投过票 58
{ 59
makecookie.Values.Add("VoteItem", "<" + strID + ">"); 60
} 61
else 62
{ 63
//Response.Write(readcookie.Values["VoteItem"]); 64
//Response.End(); 65
string strAllItem = readcookie.Values["VoteItem"].ToString();//读取已投票的项 66
if (strAllItem.IndexOf("<" + strID + ">") == -1)//未投过票 67
{ 68
makecookie.Values.Add("VoteItem", readcookie.Values["VoteItem"] + "<" + strID + ">"); 69
} 70
else//如果已投过票 71
{ 72
Response.Write("<script language=javascript>alert('该主题你已经成功投过票,不能重新投票!');</script>"); 73
return; 74
//Response.Write("<script language=javascript>alert('该主题你已经成功投过票,不能重新投票!');location='Index.aspx'</script>"); 75
} 76
} 77
Response.AppendCookie(makecookie);//写入Cookie 78
79
string strSelect = strContent.Split('|')[Convert.ToInt32(this.RadioButtonList1.SelectedValue.ToString())];//当前选中的项目,如“ 2,不错” 80
int intVote = Convert.ToInt32(strSelect.Split(',')[0]);//当前投票数+1 81
int intVoteOK = intVote + 1; 82
string strSelectOK = strSelect.Replace(intVote.ToString(), intVoteOK.ToString());//更新原投票数,所以期望更新的定位精确,可附带+"," 83
string strUPDATE = strContent.Replace(strSelect, strSelectOK);//得到最终 84
SqlData da = new SqlData(); 85
bool update = da.ExceSQL("update tb_Vote set Content='" + strUPDATE + "' where ID='" + strID + "'"); 86
if (update) 87
{ 88
Response.Write("<script language=javascript>alert('投票成功!');</script>"); 89
} 90
else 91
{ 92
Response.Redirect("Index.aspx"); 93
} 94
} 95
else//如果没有选中项目 96
{ 97
Response.Write("<script language=javascript>alert('请选择投票项!');location='javascript:history.go(-1)'</script>"); 98
} 99
} 100
protected void btnEdit_Click(object sender, EventArgs e) 101
{ 102
Response.Redirect("Login.aspx?ID="+Request["ID"]+""); 103
} 104
protected void btnView_Click(object sender, EventArgs e) 105
{ 106
Response.Redirect("Result.aspx?ID=" + Request["ID"] + ""); 107
} 108
} 109





}
}