zoukankan
html css js c++ java
个人学习代码保存:例3. GridView相关练习操作
前台代码: Default.aspx
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
&
nbsp;
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
AutoGenerateColumns
=
"
False
"
CellPadding
=
"
4
"
ForeColor
=
"
#333333
"
GridLines
=
"
None
"
AllowPaging
=
"
True
"
OnPageIndexChanging
=
"
GridView1_PageIndexChanging
"
PageSize
=
"
6
"
DataKeyNames
=
"
id
"
OnDataBound
=
"
GridView1_DataBound
"
OnRowCancelingEdit
=
"
GridView1_RowCancelingEdit
"
OnRowEditing
=
"
GridView1_RowEditing
"
>
<
FooterStyle BackColor
=
"
#507CD1
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
Columns
>
<
asp:TemplateField HeaderText
=
"
详情
"
>
<
ItemTemplate
>
<
asp:HyperLink ID
=
"
HyperLink1
"
runat
=
"
server
"
NavigateUrl
=
'
<%# "Show.aspx?id="+Eval("id") %>
'
Text
=
'
<%# Bind("id") %>
'
></
asp:HyperLink
><!--
这里的bind方法也可以用Eval方法,但Bind方法性能较好,Eval 方法是静态(只读)方法,eval()方法在运行时使用反射执行后期绑定计算,因此与标准的ASP.NET数据绑定方法bind相比,会导致性能明显下降。它一般用在绑定时需要格式化字符串的情况下。多数情况尽量少用此方法
-->
<
br
/>
<
br
/>
<
asp:LinkButton ID
=
"
LinkButton1
"
runat
=
"
server
"
PostBackUrl
=
'
<%#"Show.aspx?id="+Eval("id") %>
'
Text
=
'
<%# bind("id") %>
'
></
asp:LinkButton
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:HyperLinkField DataNavigateUrlFields
=
"
id
"
DataNavigateUrlFormatString
=
"
show.aspx?id={0}
"
DataTextField
=
"
id
"
HeaderText
=
"
详情2
"
/>
<
asp:BoundField DataField
=
"
id
"
HeaderText
=
"
编号
"
ReadOnly
=
"
True
"
/>
<
asp:BoundField DataField
=
"
title
"
HeaderText
=
"
主题
"
ReadOnly
=
"
True
"
/>
<
asp:BoundField DataField
=
"
body
"
HeaderText
=
"
内容
"
/>
<
asp:BoundField DataField
=
"
datetime
"
DataFormatString
=
"
{0:d}
"
HeaderText
=
"
留言时间
"
ReadOnly
=
"
True
"
/>
<
asp:TemplateField
>
<
HeaderTemplate
>
回发服务器方式
<
asp:CheckBox ID
=
"
chkAll
"
runat
=
"
server
"
AutoPostBack
=
"
True
"
OnCheckedChanged
=
"
chkAll_CheckedChanged
"
/>
</
HeaderTemplate
>
<
ItemTemplate
>
<
asp:CheckBox ID
=
"
chkSingle
"
runat
=
"
server
"
/>
</
ItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
<
RowStyle BackColor
=
"
#EFF3FB
"
/>
<
EditRowStyle BackColor
=
"
#2461BF
"
/>
<
SelectedRowStyle BackColor
=
"
#D1DDF1
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
#333333
"
/>
<
PagerStyle BackColor
=
"
#2461BF
"
ForeColor
=
"
White
"
HorizontalAlign
=
"
Center
"
/>
<
HeaderStyle BackColor
=
"
#507CD1
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
AlternatingRowStyle BackColor
=
"
White
"
/>
</
asp:GridView
>
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
Text
=
"
删除
"
OnClick
=
"
Button1_Click
"
/><
br
/>
<
br
/>
<
br
/>
<
br
/>
<
br
/>
<
asp:GridView id
=
"
GridView2
"
runat
=
"
server
"
AllowPaging
=
"
True
"
AutoGenerateColumns
=
"
False
"
CellPadding
=
"
4
"
DataKeyNames
=
"
id
"
ForeColor
=
"
#333333
"
GridLines
=
"
None
"
OnPageIndexChanging
=
"
GridView2_PageIndexChanging
"
OnRowCancelingEdit
=
"
GridView2_RowCancelingEdit
"
OnRowDeleting
=
"
GridView2_RowDeleting
"
OnRowEditing
=
"
GridView2_RowEditing
"
PageSize
=
"
6
"
Width
=
"
450px
"
OnRowDataBound
=
"
GridView2_RowDataBound
"
>
<
FooterStyle BackColor
=
"
#1C5E55
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
Columns
>
<
asp:BoundField DataField
=
"
title
"
HeaderText
=
"
主题
"
ReadOnly
=
"
True
"
/>
<
asp:TemplateField HeaderText
=
"
内容
"
>
<
EditItemTemplate
>
<
asp:TextBox ID
=
"
TextBox1
"
runat
=
"
server
"
Text
=
'
<%# Bind("body") %>
'
></
asp:TextBox
>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:Label ID
=
"
Label1
"
runat
=
"
server
"
Text
=
'
<%# Bind("body") %>
'
></
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:BoundField DataField
=
"
datetime
"
DataFormatString
=
"
{0:d}
"
HeaderText
=
"
留言时间
"
ReadOnly
=
"
True
"
/>
<
asp:CommandField ShowEditButton
=
"
True
"
/>
<
asp:TemplateField ShowHeader
=
"
False
"
>
<
ItemTemplate
>
<
asp:LinkButton ID
=
"
LinkButton1
"
runat
=
"
server
"
CausesValidation
=
"
False
"
CommandName
=
"
Delete
"
Text
=
"
删除
"
></
asp:LinkButton
>
</
ItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
<
RowStyle BackColor
=
"
#E3EAEB
"
/>
<
EditRowStyle BackColor
=
"
#7C6F57
"
/>
<
SelectedRowStyle BackColor
=
"
#C5BBAF
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
#333333
"
/>
<
PagerStyle BackColor
=
"
#666666
"
ForeColor
=
"
White
"
HorizontalAlign
=
"
Center
"
/>
<
HeaderStyle BackColor
=
"
#1C5E55
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
AlternatingRowStyle BackColor
=
"
White
"
/>
</
asp:GridView
>
</
div
>
</
form
>
</
body
>
</
html
>
后台代码:
using
System;
using
System.Data;
using
System.Configuration;
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
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
IsPostBack)
{
BindData();
}
}
public
void
BindData()
{
string
connstr
=
ConfigurationManager.AppSettings[
"
ConnectionString
"
].ToString();
SqlConnection con
=
new
SqlConnection(connstr);
if
(con.State.Equals(ConnectionState.Closed))
{
con.Open();
}
SqlCommand cmd
=
new
SqlCommand(
"
select * from guestbook
"
,con);
SqlDataAdapter sda
=
new
SqlDataAdapter(cmd);
DataSet ds
=
new
DataSet();
sda.Fill(ds);
this
.GridView1.DataSource
=
ds;
this
.GridView1.DataBind();
this
.GridView2.DataSource
=
ds;
this
.GridView2.DataBind();
}
protected
void
GridView1_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
{
this
.GridView1.PageIndex
=
e.NewPageIndex;
this
.BindData();
}
protected
void
chkAll_CheckedChanged(
object
sender, EventArgs e)
{
CheckBox cb
=
(CheckBox)(GridView1.HeaderRow.FindControl(
"
chkAll
"
));
foreach
(GridViewRow gr
in
GridView1.Rows)
{
CheckBox cb1
=
(CheckBox)gr.Cells[
3
].FindControl(
"
chkSingle
"
);
if
(cb.Checked)
{
cb1.Checked
=
true
;
}
else
{
cb1.Checked
=
false
;
}
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
int
intCount
=
this
.GridView1.Rows.Count;
for
(
int
i
=
0
;i
<
intCount;i
++
)
{
CheckBox CheckSingle
=
(CheckBox)(
this
.GridView1.Rows[i].Cells[
3
].FindControl(
"
chkSingle
"
));
if
(CheckSingle.Checked)
{
//
int id = Convert.ToInt32(this.GridView1.DataKeys[i]["id"]);
//
方法1
int
id
=
Convert.ToInt32(
this
.GridView1.Rows[i].Cells[
0
].Text);
//
方法2
string
sql
=
"
delete from guestbook where id=
"
+
id;
string
connstr
=
ConfigurationManager.AppSettings[
"
ConnectionString
"
].ToString();
SqlConnection con
=
new
SqlConnection(connstr);
con.Open();
SqlCommand cmd
=
new
SqlCommand(sql,con);
cmd.ExecuteNonQuery();
}
}
this
.BindData();
//
这里我犯过错误,误把绑定放到上面的for循环内,结果就只能删除GridView行的第0行了
}
protected
void
GridView1_DataBound(
object
sender, EventArgs e)
{
//
this.GridView1.ShowHeader= false;
//
可以隐藏表头
}
protected
void
GridView1_RowEditing(
object
sender, GridViewEditEventArgs e)
{
GridView1.EditIndex
=
e.NewEditIndex;
BindData();
}
protected
void
GridView1_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex
=
-
1
;
BindData();
}
protected
void
GridView2_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
{
GridView2.PageIndex
=
e.NewPageIndex;
BindData();
}
protected
void
GridView2_RowEditing(
object
sender, GridViewEditEventArgs e)
{
GridView2.EditIndex
=
e.NewEditIndex;
BindData();
}
protected
void
GridView2_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
{
GridView2.EditIndex
=
-
1
;
BindData();
}
protected
void
GridView2_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
{
int
intKey
=
Convert.ToInt32(GridView2.DataKeys[e.RowIndex][
"
id
"
]);
string
upbody
=
((TextBox)(GridView2.Rows[e.RowIndex].Cells[
1
].FindControl(
"
TextBox1
"
))).Text;
string
connstr
=
ConfigurationManager.AppSettings[
"
ConnectionString
"
].ToString();
SqlConnection con
=
new
SqlConnection(connstr);
con.Open();
string
sql
=
"
update guestbook set body='
"
+
upbody
+
"
' where id=
"
+
intKey;
SqlCommand cmd
=
new
SqlCommand(sql, con);
cmd.ExecuteNonQuery();
GridView2.EditIndex
=
-
1
;
BindData();
}
protected
void
GridView2_RowDeleting(
object
sender, GridViewDeleteEventArgs e)
{
string
connstr
=
ConfigurationManager.AppSettings[
"
ConnectionString
"
].ToString();
SqlConnection con
=
new
SqlConnection(connstr);
con.Open();
int
intKey
=
Convert.ToInt32(GridView2.DataKeys[e.RowIndex][
"
id
"
]);
string
sql
=
"
delete from guestbook where id=
"
+
intKey;
SqlCommand cmd
=
new
SqlCommand(sql, con);
cmd.ExecuteNonQuery();
BindData();
}
protected
void
GridView2_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
//
需要转换为模板列
//
第一种实现方法
//
LinkButton lnk = (LinkButton)e.Row.FindControl("LinkButton1");
//
if (lnk != null)
//
{
//
lnk.Attributes.Add("onclick", "return confirm('确定吗?')");
//
}
//
第二种实现方法
if
(e.Row.RowType
==
DataControlRowType.DataRow)
{
LinkButton lbtn
=
(LinkButton)e.Row.Cells[
4
].FindControl(
"
LinkButton1
"
);
lbtn.Attributes.Add(
"
onclick
"
,
"
return confirm('确定吗?');
"
);
//
在对RowType进行了判断后,就可以用下面的这句实现,但有一个问题,就是点击'删除'旁边的空白也会引发这个客户端脚本的
//
e.Row.Cells[4].Attributes.Add("onClick", "return confirm('你确定要删除这一条数据吗?');");
}
if
(e.Row.RowType
==
DataControlRowType.DataRow)
{
e.Row.Attributes.Add(
"
onmouseover
"
,
"
e=this.style.backgroundColor;this.style.backgroundColor='#ffff00'
"
);
e.Row.Attributes.Add(
"
onmouseout
"
,
"
this.style.backgroundColor=e
"
);
TableCell cell
=
e.Row.Cells[
0
];
//
此方法不能应用于模板列
if
(cell.Text
==
"
靠
"
)
{
cell.ForeColor
=
System.Drawing.Color.Red;
//
当有指定的字符内容时,此单元格为红色
}
//
若用于模板列时可以这样使用
string
cellText
=
((Label)(e.Row.FindControl(
"
Label1
"
))).Text;
if
(cellText
==
"
靠
"
)
{
((Label)(e.Row.FindControl(
"
Label1
"
))).ForeColor
=
System.Drawing.Color.Red;
//
当有指定的字符内容时,此单元格为红色
}
//
此种方法 绑定列和模板列均能正确使用
string
title
=
Convert.ToString( DataBinder.Eval(e.Row.DataItem,
"
body
"
));
if
(title
==
"
靠
"
)
{
e.Row.Cells[
1
].ForeColor
=
System.Drawing.Color.Red;
}
}
}
}
查看全文
相关阅读:
安装jdk1.8导致eclipse显示问题
Linux创建定时任务
Burp suite抓取HTTPS请求
Webbench性能测试
Fiddler抓取手机Https请求
Linux下使用Jmeter做性能测试
Charles抓取手机https请求
Jmeter发送Java请求
Linux搭建JDK、Tomcat安装及配置
Loadrunner监控Apache
原文地址:https://www.cnblogs.com/wbcms/p/1036455.html
最新文章
「CF1324A Yet Another Tetris Problem」
「Luogu P4175 [CTSC2008]网络管理」
「Luogu P3799 妖梦拼木棒」
「Luogu P6157 有趣的游戏」
设置Iframe 中某元素的值
pinterest 瀑布流
Js DOM 获取父级值
QQ 在线咨询
Jquery $.ajax 解析json
Sql Server 实现 Mysql Limit 功能
热门文章
LinkButton 报错
用ThoughtWorks.QRCode生成二维码时出现“索引超出了数组界限”的错误
未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项(解决)
docker数据持久化
检测ip代理有效性
docker安装Tomcat并部署war项目
postgresql could not connect to server
记录学习WeakReference发现的问题
记一次面试
上传文件,经过Zuul,中文文件名乱码
Copyright © 2011-2022 走看看