温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:Asp.net投票系统[单/复选,投票数量/项随意]源码
当前文件路径:ComplexVote/vote.aspx.cs

1using System; 2
using System.Collections; 3
using System.ComponentModel; 4
using System.Data; 5
using System.Drawing; 6
using System.Web; 7
using System.Web.SessionState; 8
using System.Web.UI; 9
using System.Web.UI.WebControls; 10
using System.Web.UI.HtmlControls; 11
using System.Data.SqlClient; 12
13
namespace vote 14
{ 15
/// <summary> 16
/// vote 的摘要说明。 17
/// </summary> 18
public partial class vote : System.Web.UI.Page 19
{ 20
protected System.Web.UI.WebControls.Panel Panel3; 21
// public int id=2; 22
protected void Page_Load(object sender, System.EventArgs e) 23
{ 24
// 在此处放置用户代码以初始化页面 25
if(!Page.IsPostBack) 26
{ 27
//限制时间就在这下面,主要是时间的转换我不怎么明白,所以没有做好,希望那位做好了告诉我下!!实在感谢! 28
string ip=Request.ServerVariables["REMOTE_ADDR"]; //得到当前IP 29
this.Label1.Text=ip; 30
string dt=DateTime.Now.Hour.ToString(); //得到当前时间 31
this.Label2.Text=dt; 32
int id=Convert.ToInt32(Request.QueryString["id"].ToString());//得到当前投票ID 33
string jgdt=db.rengyi("select jgdt from votemaster where id="+id+"");////得到当前投票的间隔时间 34
if(db.vlog("select count(*) from vlog where voteid="+id+"")) 35
{ 36
string dtime=db.rengyi("select dtime from vlog where addr='"+ip+"' and voteid="+id+""); //得到当前IP上次给该ID投票的时间 37
if(db.vlog("select count(*) from vlog where addr='"+ip+"' and voteid="+id+""))//判断数据库里是否已经有相同的IP啦 38
{ 39
if(Convert.ToInt32(dt)>Convert.ToInt32(jgdt)+Convert.ToInt32(dtime))//如果是相同的就判断现在的时间是否大于预设间隔时间加上次投票时间 40
{ 41
this.typfill(); 42
Response.Write("时间允许"); 43
} 44
else 45
{ 46
this.Panel1.Visible=false; 47
this.Panel2.Visible=true; 48
this.fill(); 49
this.filltitle(); 50
} 51
} 52
else 53
{ 54
this.typfill(); 55
Response.Write("IP可以过"); 56
} 57
58
59
this.fill(); 60
} 61
else 62
{ 63
this.fill(); 64
this.typfill(); 65
Response.Write("数据库没有东西"); 66
} 67
} 68
69
} 70
71
Web 窗体设计器生成的代码 94
95
private void typfill() 96
{ 97
//----------------------------------------下面判断显示投票项------------------------------------- 98
int id=Convert.ToInt32(Request.QueryString["id"].ToString());//得到当前投票ID 99
this.filltitle(); 100
int typ=db.typ(id); 101
if(typ==1) 102
{ 103
this.title.Text=db.title(1); 104
this.radl.DataSource=db.fill("select * from vote where mid='"+id+"' order by orde asc"); 105
this.radl.DataTextField="votexiang"; 106
this.radl.DataValueField="voteid"; 107
this.radl.DataBind(); 108
} 109
else 110
{ 111
this.title.Text=db.title(2); 112
this.chkl.DataSource=db.fill("select * from vote where mid='"+id+"' order by orde asc"); 113
this.chkl.DataTextField="votexiang"; 114
this.chkl.DataValueField="voteid"; 115
this.chkl.DataBind(); 116
} 117
//--------------------------------------------------------------------------------------------- 118
} 119
private void fill() 120
{ 121
int id=Convert.ToInt32(Request.QueryString["id"].ToString()); 122
this.DataGrid1.DataSource=db.fill("select * from vote where mid="+id+""); 123
this.DataGrid1.DataBind(); 124
} 125
private void filltitle() 126
{ 127
this.DataGrid2.DataSource=db.fill("select * from votemaster"); 128
this.DataGrid2.DataBind(); 129
} 130
protected void updata_Click(object sender, System.EventArgs e) 131
{//处理投票 132
string IP=Request.ServerVariables["Remote_Addr"]; 133
string dt=DateTime.Now.Hour.ToString(); 134
int id=Convert.ToInt32(Request.QueryString["id"].ToString()); 135
int typ=db.typ(id); 136
//--------------------------------------------------------------------------------------------- 137
if(typ==1) 138
{ 139
for(int i=0;i<this.radl.Items.Count;i++) 140
{//循环到投票项的最大项 141
if(this.radl.Items[i].Selected) 142
{ 143
string voteid=this.radl.Items[i].Value.ToString(); 144
SqlConnection con=db.con(); 145
con.Open(); 146
SqlCommand cmd=new SqlCommand("update vote set votenum=votenum+1 where voteid='"+voteid+"'",con); 147
SqlCommand cmddt=new SqlCommand("insert into vlog(dtime,addr,voteid) values('"+dt+"','"+IP+"','"+id+"')",con); 148
cmd.ExecuteNonQuery(); 149
cmddt.ExecuteNonQuery(); 150
} 151
} 152
} 153
else 154
{ 155
for(int i=0;i<this.chkl.Items.Count;i++) 156
{//循环到投票项的最大项 157
if(this.chkl.Items[i].Selected) 158
{ 159
160
string voteid=this.chkl.Items[i].Value.ToString(); 161
SqlConnection con=db.con(); 162
con.Open(); 163
SqlCommand cmd=new SqlCommand("update vote set votenum=votenum+1 where voteid='"+voteid+"'",con); 164
SqlCommand cmddt1=new SqlCommand("insert into vlog(dtime,addr,voteid) values('"+dt+"','"+IP+"','"+id+"')",con); 165
cmd.ExecuteNonQuery(); 166
cmddt1.ExecuteNonQuery(); 167
} 168
} 169
} 170
//--------------------------------------------------------------------------------------------- 171
this.fill(); 172
this.updata.Text="感谢参与"; 173
this.updata.Enabled=false; 174
} 175
176
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) 177
{ 178
if(ViewState["Order"]==null) 179
{ 180
ViewState["Order"]="ASC"; 181
} 182
else 183
{ 184
if(ViewState["Order"].ToString()=="ASC") 185
{ 186
ViewState["Order"]="DESC"; 187
} 188
else 189
{ 190
ViewState["Order"]="ASC"; 191
} 192
} 193
int id=Convert.ToInt32(Request.QueryString["id"].ToString()); 194
SqlConnection con=db.con(); 195
SqlDataAdapter sda=new SqlDataAdapter(); 196
sda.SelectCommand=new SqlCommand("select * from vote where mid="+id+"",con); 197
DataSet ds=new DataSet(); 198
sda.Fill(ds,"vote"); 199
ds.Tables["vote"].DefaultView.Sort=e.SortExpression+" "+ViewState["Order"].ToString(); 200
this.DataGrid1.DataSource=ds.Tables["vote"].DefaultView; 201
this.DataGrid1.DataBind(); 202
} 203
204
protected void sohw_Click(object sender, System.EventArgs e) 205
{ 206
this.Panel2.Visible=true; 207
this.Panel1.Visible=false; 208
} 209
210
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) 211
{ 212
if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item) 213
{ 214
e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#99ff66'"); 215
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c"); 216
} 217
} 218
219
private void DataGrid2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) 220
{ 221
if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item) 222
{ 223
e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#99ff66'"); 224
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c"); 225
} 226
} 227
228
private void DataGrid2_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) 229
{ 230
this.DataGrid2.CurrentPageIndex=e.NewPageIndex; 231
this.filltitle(); 232
} 233
234
} 235
} 236





}