新学习到的GridView使用方法
最近新做了一个评论系统,在制作的过程中又学会了几点GridView控件的使用方法,为了以后不忘记还是记录下来吧!
还是用实际例子来描述,根据读者的留言内容来进行评判(支持/反对)。

实例图示(1)
以下是详细代码:
1
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
2
{
3
//首先设定GridView1的DataKeyNames值为"id"
4
int index = Convert.ToInt32(e.CommandArgument.ToString());
5
//e.CommandArgument是选定行的索引
6
int id = Convert.ToInt32(GridView1.DataKeys[index].Value);
7
//获取该行的的id值
8
if (e.CommandName == "zhichi")//如果选择的是支持
9
{
10
if (Request.Cookies["zhichi" + id.ToString()] != null)
11
//如果cookie还没有过期
12
{
13
((Label)GridView1.Rows[index].FindControl("Tip")).Text = "请休息下再点击!";
14
((Label)GridView1.Rows[index].FindControl("Tip")).Visible = true;
15
((Label)GridView1.Rows[index].FindControl("Tip")).ForeColor = System.Drawing.Color.Red;
16
return;
17
}
18
else
19
{
20![]()
21
string sql = "update reply set zhichi=zhichi+1 where id=" + id;
22
UpDateNoP(sql);
23
BindData();
24
//点击过后创建一个新的cookie,防止恶意点击
25
HttpCookie zhichi = new HttpCookie("zhichi" + id.ToString(), "yes");
26
zhichi.Expires = DateTime.Now.AddSeconds(300);//有效期为300秒
27
Response.Cookies.Add(zhichi);//写入Cookie
28![]()
29![]()
30
}
31
}
32
else//如果选择的不是支持
33
{
34
if (Request.Cookies["fandui" + id.ToString()] != null)
35
{
36
((Label)GridView1.Rows[index].FindControl("Tip")).Text = "请休息下再点击!";
37
((Label)GridView1.Rows[index].FindControl("Tip")).Visible = true;
38
((Label)GridView1.Rows[index].FindControl("Tip")).ForeColor = System.Drawing.Color.Red;
39
return;
40
}
41
else
42
{
43
string sql = "update reply set fandui=fandui+1 where id=" + id;
44
UpDateNoP(sql);
45
BindData();
46
HttpCookie fandui = new HttpCookie("fandui" + id.ToString(), "yes");
47
fandui.Expires = DateTime.Now.AddSeconds(300);//有效期为300秒
48
Response.Cookies.Add(fandui);//写入Cookie
49
Label2.Visible = false;
50
}
51
}
52
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)2
{3
//首先设定GridView1的DataKeyNames值为"id"4
int index = Convert.ToInt32(e.CommandArgument.ToString());5
//e.CommandArgument是选定行的索引6
int id = Convert.ToInt32(GridView1.DataKeys[index].Value);7
//获取该行的的id值8
if (e.CommandName == "zhichi")//如果选择的是支持9
{10
if (Request.Cookies["zhichi" + id.ToString()] != null)11
//如果cookie还没有过期12
{13
((Label)GridView1.Rows[index].FindControl("Tip")).Text = "请休息下再点击!";14
((Label)GridView1.Rows[index].FindControl("Tip")).Visible = true;15
((Label)GridView1.Rows[index].FindControl("Tip")).ForeColor = System.Drawing.Color.Red;16
return;17
}18
else19
{20

21
string sql = "update reply set zhichi=zhichi+1 where id=" + id;22
UpDateNoP(sql);23
BindData();24
//点击过后创建一个新的cookie,防止恶意点击25
HttpCookie zhichi = new HttpCookie("zhichi" + id.ToString(), "yes");26
zhichi.Expires = DateTime.Now.AddSeconds(300);//有效期为300秒27
Response.Cookies.Add(zhichi);//写入Cookie28

29

30
}31
}32
else//如果选择的不是支持33
{34
if (Request.Cookies["fandui" + id.ToString()] != null)35
{36
((Label)GridView1.Rows[index].FindControl("Tip")).Text = "请休息下再点击!";37
((Label)GridView1.Rows[index].FindControl("Tip")).Visible = true;38
((Label)GridView1.Rows[index].FindControl("Tip")).ForeColor = System.Drawing.Color.Red;39
return;40
}41
else42
{43
string sql = "update reply set fandui=fandui+1 where id=" + id;44
UpDateNoP(sql);45
BindData();46
HttpCookie fandui = new HttpCookie("fandui" + id.ToString(), "yes");47
fandui.Expires = DateTime.Now.AddSeconds(300);//有效期为300秒48
Response.Cookies.Add(fandui);//写入Cookie49
Label2.Visible = false;50
}51
}52
}



浙公网安备 33010602011771号