RowDataBound事件
在创建gridView控件时,必须先为GridView的每一行创建一个GridViewRow对象,创建每一行时,将引发一个RowCreated事件;当行创建完毕,每一行GridViewRow就要绑定数据源中的数据,当绑定完成后,将引发RowDataBound事件。如果说我们可以利用RowCreated事件来控制每一行绑定的控件,那么我们同样可以利用RowDataBound事件来控制每一行绑定的数据,也就是让数据如何呈现给大家。

还举同样的例子,在数据表中,存在性别列,上面我们用DropListDown控件的DataBounding来表示出了中文的性别,但是毕竟不太美观,我们现在可以利用Label控件和RowDataBound事件来实现完美的中文性别显示。RowDataBound,
首先,还是把性别列,设置为模板列,并添加一个Label控件,将Label控件绑定到数据源的性别段,然后我们在GridView控件属性的事件列表中双击RowDataBound,生成如下事件:
Example:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判断当前行是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        { //用FindControl方法找到模板中的Label控件
Label lb1= (Label)e.Row.FindControl("Label1");
//因为RowDataBound是发生在数据绑定之后,所以我们可以
//判断Label绑定的数据,如果是True,就更改其text属性为男
                if (lb1.Text== "True")
                      lb1.Text = "男";
                else
                      lb1.Text = "female";
        }
    }
 
3、RowType
RowType可以确定GridView中行的类型,RowType是玫举变量DataControlRowType中的一个值。RowType可以取值包括 DataRow、Footer、Header、EmptyDataRow、Pager、Separator。很多时候,我们需要判断当前是否是数据行,通过如下代码来进行判断:
   if (e.Row.RowType == DataControlRowType.DataRow)
 
4、RowDeleting和RowDeleted事件
RowDeleting发生在删除数据之前,RowDeleted发生在删除数据之后。
使用RowDeleting事件,可以在真正删除前再次确认是否删除,可以通过设置GridViewDeleteEventArgs.Cancel=True来取消删除;也可以用于判断当前数据库记录数,如果只剩一条记录且数据库不能为空则提示并取消删除操作。
使用RowDeleted事件,可以在删除后,通过GridViewDeletedEventArgs的Exception属性判断删除过程中是否产生异常,如无异常,则可以显示类似于” 1 Records deleted” 之类的提示信息。
Example:
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//取得当前行号,并取得当前行的GridViewRow对象
        int index=e.RowIndex ;
        GridViewRow gvr=GridView1.Rows[index];
//取得当前行第二个单元格中的文字
        str1 = gvr.Cells[1].Text;
//进行提示
        Message.Text ="您将删除一个用户,其姓名为"+str1 ;
    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
//如果没有产生异常,则提示成功删除,否则提示删除失败
        if (e.Exception == null)
            Message.Text += "<br>您成功删除了"+str1 ;
        else
            Message.Text += "删除失败,请联系管理员";
}
5、RowEditing事件
在GridView中的行进入编辑模式之前,引发RowEditing事件,如果您需要在编辑记录前进行某些预处理,可以在这里操作。如果想取消对当前行的编辑,可以把GridViewEditEventArgs 对象的 Cancel 属性设置为 true即可。
Example:
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//用NewEidIndex取得当前编辑的行号,然后获取gridviewrow对象
        GridViewRow gvr = GridView1.Rows[e.NewEditIndex];
 
//判断,如果当前编辑行姓名栏为admin用户,则取消对当前行的编辑
        if (gvr.Cells[1].Text =="admin")
            e.Cancel = true;
}
 
6、RowUpdating和RowUpdated事件
RowUpdating事件发生在更新数据源之前,RowUpdated发生在更新数据源之后。
我们可以在记录更新前利用RowUpdating做一些预处理工作,比如修改密码时,因为密码在数据库中不是明文存储,进行了hash,所以在更新密码前,应该生成其hash值,再进行更新操作。RowUpdated则可以检验更新是否成功。
Example:
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gvr = GridView1.Rows[GridView1 .EditIndex ];
//寻找输入密码的控件
        TextBox tb1 = (TextBox)gvr.FindControl("tb_password");
//将此控件中的文本hash后,把password存入NewValues这个字典中
        e.NewValues ["password"] =tb1.Text .GetHashCode().ToString () ;
 
    }
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
//如无异常,则更新成功
        if (e.Exception == null)
            Message.Text += "更新成功!";
    }
 
7、Keys、OldValues、NewValues集合
Keys字典中一般存放的是数据源中的主键字段的key和value的对应值,如果主键由多个字段组成,那么Keys为每个键字段添加其字段名称和值。OldValues中存放的是要更新的行的字段名和原始值,每个字段为其中的一项。NewValues中存放的是要更新的行的字段名和修改后的值,每个字段为其中的一项。注意,主键字段只存放于keys集合中。
这三个集合中的每一项都是DictionaryEntry类型的对象,我们可以用DictionaryEntry.Key来确定一个项的字段名称,用DictionaryEntry.Value来确定某项的值。
在上面的例子中,为了把密码明文加密后再存入数据库,我们利用了NewValues字段,重新设置key为password的项的值。为了保证安全性,我们在更新数据前对NewValues中的所有值进行html编码:
Example1:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//遍历NewValues,取得其中每一对DictionaryEntry对象
       foreach (DictionaryEntry de in e.NewValues)
 
//de.key就是字段名,如果此处单独更新某字段的话,也可以直接填写字段名,//比如 e.NewValues[“password”]
 
       e.NewValues[de.Key] = Server.HtmlEncode(de.Value.ToString());
    }
 
Example2:
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//分别利用Keys、OldValues、NewValues取得主键名、原始数据和更新后数据
        Message .Text = e.Keys["username"] + "的email地址从" + e.OldValues["email"] + "变更为" + e.NewValues["email"];
}

RowDataBound事件的更多相关文章

  1. GridView控件RowDataBound事件中获取列字段值的几种途径 !!!

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == ...

  2. GridView控件RowDataBound事件中获取列字段值的几种途径

    前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...

  3. GridView的RowCreated与RowDataBound事件区别

    在西门子面试时,项目负责人除了道试题关于RowCreated与RowDataBound事件区别,经过google一下,得出结果: GridView的RowCreated与RowDataBound的一个 ...

  4. GridView的 PreRender事件与 RowCreated、RowDataBound事件大乱斗

    GridView的 PreRender事件与 RowCreated.RowDataBound事件大乱斗 之前写了几个范例,做了GridView的 PreRender事件与 RowCreated.Row ...

  5. Gridview的RowDataBound事件(添加删除提示,改变背景颜色)

    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e) { //如果是绑定数据行 if (e.Row.Row ...

  6. Gridview的RowDataBound事件可以做很多事情

    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)        {            //如果是绑 ...

  7. gridView RowDataBound事件 鼠标经过行颜色变化及根据字段值显示指定内容

    protected void gvBarInfo_RowDataBound(object sender, GridViewRowEventArgs e)        {            if ...

  8. GridView控件RowDataBound事件中获取列字段途径

    今天不知道怎么回事怎么也找不到gridview列中的控件,关键是其为编辑时隐藏域中的控件,取值就很成问题了,网上搜了很到,找到这个比较经典的东东了,可能大家都知道,但很少对比整理到一起,有多种方法可以 ...

  9. GridView控件RowDataBound事件的一个实例

    实现点击两个按钮,跳转到同一个界面,HyperLink显示不同的东西,主要代码段如下 前台代码: <asp:TemplateField HeaderText="操作"> ...

随机推荐

  1. chrome浏览器关闭标签页面

    chrome浏览器关闭标签页提示:Scripts may close only the windows that were opened by it. 解决办法:通过open方法进行关闭. open( ...

  2. 【转】Python 代码调试技巧

    转载自:http://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/ Debug 对于任何开发人员都是一项非常重要的技能,它能够帮助我 ...

  3. phpmyadmin 主机名自动补全

    2015年2月9日 14:29:25 新安装的phpmyadmin在登录界面中, 主机名的输入框没有自动补全功能, 导致每次都得手动输入ip地址 找到 phpmyadmin/libraries/aut ...

  4. CStringUtf8ToUnicode

    CString CStringUtf8ToUnicode( CString Utf8 ) { int wLen = 0; CString strUnicode; LPSTR pBufChar = NU ...

  5. Java for LeetCode 168 Excel Sheet Column Title

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...

  6. elk平台分析nginx日志的基本搭建

    一.elk套件介绍 ELK 由 ElasticSearch . Logstash 和 Kiabana 三个开源工具组成.官方网站: https://www.elastic.co/products El ...

  7. mybatisforeach循环,传入多个参数

    上代码: controller: @RequestMapping(value = "/findPage", method = RequestMethod.POST) @Respon ...

  8. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  9. js之事件冒泡和事件捕获介绍

    链接:http://www.jb51.net/article/42492.htm (1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. (2)捕获型事件 ...

  10. Mac OS X 上的安装haskell开发环境

    到haskell官方下载haskell的工具包: https://downloads.haskell.org/~platform/2014.2.0.0/Haskell%20Platform%20201 ...