GridView使用-添加事件在服务器端执行

本文介绍了ASP.NET中GridView控件的多种高级用法,包括删除前增加确认提示、编辑时控制文本框宽度、显示图片、绑定各种鼠标及键盘事件以及解决列不可更新的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、删除前加判断
protected void gv_productJingPing_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[4].Attributes.Add(" "javascript:return confirm('您真的要删除["+e.Row.Cells[1].Text+"]吗?')");
}
}
2、编辑时控制文本框的宽度
protected void gv_productJingPing_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) != 0)
{
TextBox txtUrl = (TextBox)e.Row.Cells[2].Controls[0];
txtUrl.Width = 230;
}
}
}
(说明:如果使用模板列,则可自定义列长)

3、显示图片
<asp:ImageField DataImageUrlField="imagepath" DataImageUrlFormatString="img/{0}">
</asp:ImageField>
说明:imagepath指的是数据库图片字段名,img指图片存放位置

4 、 GridView的双击/单击/键盘按键/鼠标悬浮/移出等事件
<script language="javascript">
function DbClickEvent(d)
{
window.alert("事件类型: DoubleClidk 作用对象: " + d);
}
function ClickEvent(d)
{
window.alert("事件类型: OnClick 作用对象: " + d);
}
function GridViewItemKeyDownEvent(d)
{
window.alert("事件类型: GridViewItemKeyDownEvent 作用对象: " + d);
}
</script>
(绑定事件)
if( e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到每项时颜色交替效果
e.Row.Attributes.Add("OnMouseOut","this.style.backgroundColor='White';this.style.color='#003399'");
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");
//单击/双击 事件
e.Row.Attributes.Add("OnDblClick", "DbClickEvent('" + e.Row.Cells[1].Text + "')");
e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[1].Text + "')");
e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");
//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] = "Cursor:hand";

}

5、将GridView的列的Visible设置为false时,该列无法更新到数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值