您的位置:首页 > 其它

gridview 删除前提交确认窗口

2010-07-21 14:04 316 查看
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow ){

e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c");
((LinkButton)e.Row.Cells[2].Controls[0]).Attributes.Add("onClick", "return confirm('are you sure?');");

}
}


一.

用惯了datagrid,第一次用gridView,倒有点不习惯.写删除确认时还有点不习惯,经过一番折磨,gridView的删除确认可以这样写:

if (e.Row.RowType == DataControlRowType.DataRow)
...{
e.Row.Cells[0].Attributes.Add
("onclick", "javascript:return confirm('确定删除?')");
}


把上面这段放在GridView1_RowDataBound事件里或GridView1_RowCreated里都可以.

为gridview删除按钮添加确认窗口
1.将存在删除按钮的列转换为模板列。
2.在删除按钮的load事件中添加如下代码:
((Button)sender).Attributes.Add("OnClick", "return confirm('Are you sure you want to delete?');");
这样就可以简单的实现删除前的用户确认操作了。
同样的,如果使用微软最新的gridview网格时,需要将一个绑定列的改为下拉框模式,也需要将绑定列转换为模板列,然后删除默认的文本框控件,更改为下拉框控件即可。值的注意的是模板列分为显示和编辑两种状态,我在显示时将下拉框的Enabled属性设置为false,以避免在浏览是让用户进行了不必要的选择。 回复

首先将gridview的CommandField列转为TemplateField,在模板编辑状态下选中删除按钮,在属性窗口上的OnClientClick中输入:return confirm("确认要删除吗?")
测试一下,你会发现已经为删除按钮添加了确认窗口,点击取消,直接返回页面,点击确定,删除记录。

二.开始想用类似原来 1.1 的处理方式,在 RowCreated 事件里添加,但是折腾了半天发现不行。
现在的按钮列中的控件实际上是 DataControlButton 或 DataControlLinkButton,但是不能直接用这两个类,提示保护性级别有问题,估计是 internal 的。我们可以把它转换为基类 Button 或者 LinkButton 使用,可以向其 Attributes 属性里添加 onclick 处理的提示,这样可以实现但是代码比较丑陋。

简单的办法是利用 2.0 里提供新的 OnClientClick 事件在 aspx 里面直接写。(命令按钮要用模版列)
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="Delete" OnClientClick="return confirm('您确认要删除吗?')" >删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


如果用 Button 代替 LinkButton 也是一样的,这样写:

<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" CommandName="Delete" OnClientClick="return confirm('您确认要删除吗?')" Text="删除" />
</ItemTemplate>
</asp:TemplateField>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: