📄 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
{
DB db = new DB();
bool flag = false;
private int a = 0;
protected void Page_Load(object sender, EventArgs e)
{
a = Convert.ToInt32(Request.QueryString["id"].ToString());
getTitle();
if (!Page.IsPostBack)
{
dataBind();//第一次加载时对数据进行绑定
}
}
//获取投票标题
public void getTitle()
{ //获得投票题目
string strsql = "select voteTitle from voteMaster where id=" +a;
this.Label2.Text = db.executeGetReturn(strsql).ToString();
}
public void dataBind()
{ //绑定选项
string strsql = "select voteDetailsID,voteItem from voetDetails where id=" +a;
SqlDataReader sdr = db.getsdr(strsql);
RadioButtonList1.DataSource = sdr;
RadioButtonList1.DataTextField = "voteItem";
this.RadioButtonList1.DataValueField = "voteDetailsID";
RadioButtonList1.DataBind();
sdr.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
flag = false;
DateTime d = Convert.ToDateTime(DateTime.Now);
string x = Request.ServerVariables.Get("Remote_Addr").ToString();
string strsql = "update voetDetails set voteNum=voteNum+1 where voteDetailsID=" + this.RadioButtonList1.SelectedValue;//更新票数
if (checkip())
{ //系统需要检测IP
if (isIP())
{//此用户第一次投票
//string IP=Request.UserHostAddress.ToString();
string c = "insert into voter(id,ip,voteTime) values(" + a + ",'" + x + "','" + d + "') ";
string[] arrg = new string[2];
arrg[0] = strsql;
arrg[1] = c;
if (db.trantion(arrg))
{
this.labMessage.Text = "感谢你的投票";
}
else
{
this.labMessage.Text = "投票不成功!";
}
}
else
{ //此用户已投过票
if (checkTime())
{ //你的投票过于频繁
this.labMessage.Text = "对不起,你的投票太过于频繁,为了公平,请稍后进行投票!";
}
else
{ //你可以再次进行投票
string c = "update voter set voteNum=voteNum+1,voteTime=getdate() where id=" + a + "and ip='" + x + "'";
string[] orrg = new string[2];
orrg[0] = strsql;
orrg[1] = c;
if (db.trantion(orrg))
{
this.labMessage.Text = "你的再次投票成功!";
}
else
{
this.labMessage.Text = "你的投票不成功";
}
}
}
}
else
{ //系统不需要检测IP
if (isIP())
{//此用户第一次投票
//string IP=Request.UserHostAddress.ToString();
string c = "insert into voter(id,ip,voteTime) values(" + a + ",'" + x + "','" + d + "') ";
string[] srrg = new string[2];
srrg[0] = strsql;
srrg[1] = c;
if (db.trantion(srrg))
{
this.labMessage.Text = "welocom!";
}
else
{
this.labMessage.Text = "eorr!";
}
}
else
{
string c = "update voter set voteNum=voteNum+1,voteTime=getdate(),where id=" + a+ "and ip='" + x + "'";
string[] zrrg = new string[2];
zrrg[0] = strsql;
zrrg[1] = c;
if (db.trantion(zrrg))
{
this.labMessage.Text = "欢迎再来!";
}
else
{
this.labMessage.Text = "hello word!";
}
}
}
}
public bool checkip()
{ //检测是否需要限定IP
flag = false;
string strsql = "select checkIP from voteConfig where id=" +a;
int c = Convert.ToInt16(db.executeGetReturn(strsql));
if (c == 0)
{ //系统检测IP
flag = true;
}
else
{
flag = false;
}
return flag;
}
public bool checkTime()
{
//检测投票时间是否合格
flag = false;
string IP = Request.ServerVariables.Get("Remote_Addr").ToString();
string strsql = "select datediff(s,voteTime,getdate()) from voter where ip='" + IP + "'and id=" +a;
string m = "select checkTime from voteConfig where id=" + a;
long c = Convert.ToInt64(db.executeGetReturn(strsql));
int b = Convert.ToInt16(db.executeGetReturn(m));
if (c < b * 60)
{ //投票过于频繁
flag = true;
}
else
{ //你可以进行投票了
flag = false;
}
return flag;
}
public bool isIP()
{
//检测是否已投过票
flag = false;
//string IP = Request.UserHostAddress;
string IP = Request.ServerVariables.Get("Remote_Addr").ToString();
string strsql = "select count(*) from voter where ip='" + IP + "'and id=" +a;
int c = Convert.ToInt16(db.executeGetReturn(strsql));
if (c == 0)
{//此IP第一次对此项目投票
flag = true;
}
else
{ //此用户与针对此项目投过票
flag = false;
}
return flag;
}
protected void Button2_Click(object sender, EventArgs e)
{
//int a = Convert.ToInt32(Request.QueryString["id"].ToString());
Response.Redirect("showResult.aspx?id=" +a);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -