Gridview 用法大全三

在VS2005提供的GridView中我们可以直接添加一个CommandField删除列:<asp:CommandField ShowDeleteButton="True" />,然后在GridView的OnRowDeleting事件中完成删除。但一般情况下我们在做这种删除操作时都需要先让用户确认一下,然后后再删除记录,以避免误操作引起的误删除。

那我们可以通过下面方法给GridView删除前加上一个确认对话框。

首先,在GridView的属性对框话框中点击“Columns”进入它的"字段"设计器;或者在设计窗口直接点击GridView控件右上角的那个小箭头,点击"编辑列",进入"字段"设计器。

接着在"字段"设计器中的左下方"选定的字段"框中,选择以前已加上的那个CommandField“删除”列,这时在右边它的属性列表下会看到一个"将此它段转换为 TemplateFied"的项,点击将它转换为TemplateFied列。

然后退出"字段"设计器,切换到源码视图你会发现该列已由原来的:
<asp:CommandField ShowDeleteButton="True" />
变为了:

<asp:TemplateField ShowHeader="False">
                                  
<ItemTemplate>
                                      
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"

CommandName
="Delete"     Text="删除"></asp:LinkButton>
</ItemTemplate>

最后在<asp:LinkButton>中加入:OnClientClick="return confirm('您确认删除该记录吗?');"

这样点击删除时就会先在客户端弹出“您确认删除该记录吗?”对话框,点击"确定",则进行删除;点击"取消",则不删除.
而原来在onRowDeleting事件中写的代码完全不用改变。鼠标在GridView上移动时变换颜色

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
          
{
              
if (e.Row.RowType == DataControlRowType.DataRow )
              
{
                   e.Row.Attributes.Add(
"onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
                  
//当鼠标移走时还原该行的背景色
                   e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
               }

           }

RowCreated:开被创建时发生
e.Row.RowType == DataControlRowType.DataRow 判断是否是数据行
DataControlRowType的枚举有:
DataRow数据行
EmptyDataRow
Footer
Header
Pager
Separator
如果对单元格进行变换。

if (e.Row.Cells[8].Text == "USA")
               
{
                   
//e.Row.BackColor = System.Drawing.Color.Red; //修改整行的颜色
                    e.Row.Cells[8].BackColor = System.Drawing.Color.Red;     //修改单元格的颜色(第9列)
                }

不足。前端页面代码过多。站带宽。
在GridView的第一列使用CheckBox控制每一行数据,是经常使用的,这里面我要记录的是全选、全消、选中行的底色更改,还有就是在提交数据的时候,取选中的行的编号等功能

posted @ 2010-09-17 08:38  英雄不问出处  阅读(276)  评论(0编辑  收藏  举报