温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:Asp.net投票系统[单/复选,投票数量/项随意]源码
当前文件路径:ComplexVote/show.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
namespace vote 13
{ 14
/// <summary> 15
/// show 的摘要说明。 16
/// </summary> 17
public partial class show : System.Web.UI.Page 18
{ 19
20
protected void Page_Load(object sender, System.EventArgs e) 21
{ 22
// 在此处放置用户代码以初始化页面 23
24
if(!Page.IsPostBack) 25
{ 26
Server.Execute("clog.aspx");//转到这个页去判断是否登录 27
int mid=Convert.ToInt32(Request.QueryString["id"]); 28
this.HyperLink1.Text=db.title(mid); 29
this.HyperLink1.NavigateUrl="vote.aspx?id="+mid; 30
this.fill(); 31
} 32
} 33
34
Web 窗体设计器生成的代码 60
61
protected void Button2_Click(object sender, System.EventArgs e) 62
{ 63
//int id=db.id("select id from vote order by id desc")+1;因为在数据库中,这个字段被设置为自动增长,所以这里就不需要关他的ID了 64
string votexiang=this.vote.Text.ToString();//取出投票子项内容 65
int mid=Convert.ToInt32(Request.QueryString["id"]); 66
int num=Convert.ToInt32(this.mnum.Text); 67
if(db.insert("insert into vote(votexiang,mid,votenum) values('"+votexiang+"',"+mid+","+num+")")) 68
{ 69
this.Label2.Text="插入成功"; 70
if(num>0) 71
{//如果这里不加的话,那么总票数和实际票数必然有误差 72
db.update("update votemaster set num=num+"+num+" where id="+mid+""); 73
} 74
} 75
else 76
{ 77
this.Label2.Text="插入失败"; 78
79
} 80
this.fill(); 81
} 82
private void fill() 83
{//根据定制的SQL语句去db类索要数据,然后填充网格 84
int mid=Convert.ToInt32(Request.QueryString["id"]); 85
this.DataGrid1.DataSource=db.fill("select * from vote where mid="+mid+""); 86
this.DataGrid1.DataKeyField="voteid"; 87
this.DataGrid1.DataBind(); 88
} 89
90
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) 91
{//取消编辑 92
this.DataGrid1.EditItemIndex=-1; 93
this.fill(); 94
} 95
96
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) 97
{//删除指定条目 98
string voteid=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); 99
db.delete("delete from vote where voteid="+voteid+""); 100
this.fill(); 101
} 102
103
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) 104
{//将所选行变成编辑状态 105
this.DataGrid1.EditItemIndex=e.Item.ItemIndex; 106
this.fill(); 107
} 108
109
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) 110
{//翻页设置 111
this.DataGrid1.CurrentPageIndex=e.NewPageIndex; 112
this.fill(); 113
} 114
115
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) 116
{//鼠标行为 117
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem) 118
{//指定只是控件中的项或者交替项 119
e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#ffcc00'"); 120
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c"); 121
((LinkButton)(e.Item.Cells[5].Controls[0])).Attributes.Add("onclick","return confirm('确认删除吗?')"); 122
} 123
} 124
125
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) 126
{//数据排序 127
if(ViewState["Order"]==null) 128
{ 129
ViewState["Order"]="DESC"; 130
} 131
else 132
{ 133
if(ViewState["Order"].ToString()=="DESC") 134
{ 135
ViewState["Order"]="ASC"; 136
} 137
else 138
{ 139
ViewState["Order"]="DESC"; 140
} 141
} 142
int mid=Convert.ToInt32(Request.QueryString["id"]); 143
SqlConnection con=db.con(); 144
con.Open(); 145
SqlDataAdapter sda=new SqlDataAdapter(); 146
DataSet ds=new DataSet(); 147
sda.SelectCommand=new SqlCommand("select * from vote where mid="+mid+"",con); 148
sda.Fill(ds,"vote"); 149
ds.Tables["vote"].DefaultView.Sort=e.SortExpression+" "+ViewState["Order"].ToString(); 150
this.DataGrid1.DataSource=ds.Tables["vote"].DefaultView; 151
this.DataGrid1.DataBind(); 152
} 153
154
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) 155
{//更新,修改 156
int mid=Convert.ToInt32(Request.QueryString["id"]); 157
string id=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); 158
string votexiang=((TextBox)(e.Item.Cells[1].Controls[0])).Text.ToString(); 159
string votenum=((TextBox)(e.Item.Cells[2].Controls[0])).Text.ToString(); 160
int orde=Convert.ToInt32(((TextBox)(e.Item.Cells[3].Controls[0])).Text); 161
db.update("update vote set votexiang='"+votexiang+"',votenum='"+votenum+"',orde="+orde+" where voteid='"+id+"'"); 162
db.update("update votemaster set num=num+"+votenum+" where id="+mid+""); 163
this.DataGrid1.EditItemIndex=-1; 164
this.fill(); 165
166
} 167
168
} 169
} 170





}