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

之前写了几个范例,做了GridView的 PreRender事件与 RowCreated、RowDataBound事件

这三种事件的示范

简单的说,如果您只想 "看" 文字说明就能懂

那MSDN原厂网站 屹立数年了,您还是看不懂或是做不出来。

所以,「实作(动手做)」可以解决一切困扰

现在有同一个范例,用「不同作法」营造出「相同成果」应该是最好的比较方式。

=================================================================

题目:计算每一页的学生数学成绩(加总、累加)统计总分

=================================================================

先从简单的讲起:

第一,GridView的 PreRender事件

来看 MSDN网站的说明 
控件的 PreRender事件...... 在 Control 对象加载之后  但在呈现之前发生。
 
 
当GridView已经成形,在「呈现到画面」之前,我们动手最后一次修改
 
如同这个产品 "已经"生产出来(已经离开生产线),在给人看见之前,我最后一次擦亮他
 
所以,我们跑 for循环把本页的每一列、每一笔记录的数学成绩 ( GV_Row.Cells[5].Text) 加总起来。
 
程序代码如下
 

第二,GridView的 RowDataBound事件

这个事件比较难一点点,不自己动手做就不会弄清楚

** RowCreated事件运行时间比RowDataBound事件早!

** 这个事件就是一个「自己跑 for循环」「自动跑 for循环」的事件

当GridView是在这个事件里面,慢慢被产生出来的
每一列的标题、每一列的纪录......都是这个事件 "逐步" 生产出来的
 
简言之,这个事件是就「生产线」,GridView表格就是从这两个事件被生产成形的
 
 
所以,「不需要」 for循环把本页的每一列、每一笔记录的数学成绩 ( e.Row.Cells[5].Text) 加总起来。
 
因为他正在「产生」每一列.....我们让他自己跑 for循环,让他自己产生,我在旁边等
 
生产线「每产生一列」,我就拿起数据加总一次
 
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //== 通常都会搭配这几段 if 判别式
        if (e.Row.RowType == DataControlRowType.Header)
        {    //-- 只有 GridView呈现「表头」列的时候,才会执行这里!
            Response.Write("「表头」列 DataControlRowType.Header <br />");
        }
 
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {   //-- 当 GridView呈现「每一列」数据列(记录)的时候,才会执行这里!
            //-- 所以这里就像循环一样,会反复执行喔!!
            Response.Write("「每一列」数据列(记录) DataControlRowType.DataRow <br />");
        }
 
    }

下图的说明,不知道是否清楚?

第三,YouTube影片教学  https://youtu.be/SahEqQ8-heI

第四,完整范例如下,亲自动手做,亲眼看一次就会懂了。

Web Form画面 (.aspx檔):

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" 
            DataSourceID="SqlDataSource1" OnPreRender="GridView1_PreRender" AllowPaging="True" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                <asp:BoundField DataField="student_id" HeaderText="student_id" SortExpression="student_id" />
                <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
                <asp:BoundField DataField="chinese" HeaderText="chinese" SortExpression="chinese" />
                <asp:BoundField DataField="math" HeaderText="math" SortExpression="math" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT * FROM [student_test]"></asp:SqlDataSource>
 
    </div>
        <asp:Label ID="Label1" runat="server" style="font-weight: 700; color: #0066FF; font-size: large"></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" style="font-weight: 700; color: #CC0099; font-size: large"></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" style="font-weight: 700; color: #669900; font-size: large"></asp:Label>
 
 
后置程序代码:
 
    protected void GridView1_PreRender(object sender, EventArgs e)
    {
        // 在控件呈现到画面「之前」,做最后的处理
        int sum = 0;
               
        foreach (GridViewRow GV_Row in GridView1.Rows)
 
            sum += Convert.ToInt32(GV_Row.Cells[5].Text);
        }
        Label1.Text = "PreRender事件 ** 数学成绩的加总 = " + sum;
    }
 
 
    //************************************************************
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {   //我跟 RowDataBound事件是双胞胎兄弟,但我比较早执行。我是哥哥!
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[5].Text != "")
            {   // 如果这时候抓得到数值,我就累加!
                //sum1 += Convert.ToInt32(e.Row.Cells[5].Text);
                //Label2.Text = "RowCreated ** 数学成绩的加总 = " + sum1;
                Label2.Text = "RowCreated ** e.Row.Cells[5].Text的内容是:" + e.Row.Cells[5].Text;
            }
            else {
                Label2.Text = "RowCreated ** 抱歉,我抓不到数值。";
            }
        }
    }
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)   {            
            sum2 += Convert.ToInt32(e.Row.Cells[5].Text);
            Label3.Text = "RowDataBound事件 ** 数学成绩的加总 = " + sum2;
        }            
    }
 
最后留一个题目给您练习: RowCreated事件 又跟 RowDataBound事件有何不同?

第五,总复习 --  

同一个范例,用「不同作法」营造出「相同成果」

范例一,成绩不及格者(不到六十分),出现红字

GridView的 PreRender事件与范例-- [Case Study]成绩低于60分就出现红字 & 分数加总(累加)

GridView的 RowDataBound与 RowCreated事件--[Case Study]成绩低于60分就出现红字

范例二,复选 GridView+CheckBox,批次删除

[习题] FindControl 简单练习--GridView + CheckBox,点选多列数据(复选删除) #2 - 分页&范例下载

GridView的 PreRender事件与范例--GridView + CheckBox,点选多列资料(复选删除)

相同范例有多种解法,也可以参阅这篇文章:

[GridView] 资料系结表达式?或是RowDataBound事件来作?

GridView的 PreRender事件与 RowCreated、RowDataBound事件大乱斗的更多相关文章

  1. GridView的RowCreated与RowDataBound事件区别

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

  2. GridView事件DataBinding,DataBound,RowCreated,RowDataBound区别及执行顺序分析

    严格的说,DataBinding,DataBound并不是GridView特有的事件,其他的控件诸如ListBox等也有DataBinding,DataBound事件. DataBinding事件MS ...

  3. GridView的 PreRender事件与范例--GridView + CheckBox,点选多列资料(复选删除)

    GridView的 PreRender事件与范例--GridView + CheckBox,点选多列资料(复选删除) 之前有一个范例,相同的结果可以用两种作法来实践 [GridView] 资料系结表达 ...

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

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

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

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

  6. RowDataBound事件

    RowDataBound事件在创建gridView控件时,必须先为GridView的每一行创建一个GridViewRow对象,创建每一行时,将引发一个RowCreated事件:当行创建完毕,每一行Gr ...

  7. GridView“GridView1”激发了未处理的事件“RowDeleting”

    GridView“GridView1”激发了未处理的事件“RowDeleting”. 原因:1.模板列或者buttoncommand里的commandname=“Delete”,“Update”等关键 ...

  8. javaScirpt事件详解-原生事件基础(一)

    事件 JavaScript与HTML之间的交互是通过事件实现的.事件,就是文档或浏览器窗口中发生的一些特定的交互瞬间,通过监听特定事件的发生,你能响应相关的操作.图片引用:UI Events 事件流 ...

  9. javaScript事件(一)事件流

    一.事件 事件是用户或浏览器自身执行的某种动作,如click,load和mouseover都是事件的名字.事件是javaScript和DOM之间的桥梁.你若触发,我便执行——事件发生,调用它的处理函数 ...

随机推荐

  1. CocoaPods常用操作命令

    查看镜像: gem sources -l 删除镜像 gem sources --remove https://rubygems.org/ 添加镜像 gem sources -a https://gem ...

  2. go语言实战教程:项目文件配置和项目初始化运行

    在上节内容中,我们已经搭建了实战项目框架,并将实战项目开发所需要的静态资源文件进行了导入.在本节内容中,我们将讲解如何通过相关的配置,并初始化运行项目. conf配置文件读取配置信息 我们前面说过,使 ...

  3. 解读人:刘杰,Targeted Quantitative Kinome Analysis Identifies PRPS2 as a Promoter for Colorectal Cancer Metastasis(PRM-磷酸化激酶定量发现结肠癌转移促进因子-PRPS2)

    关键词:PRM,kinase,colorectal cancer, metastasis, PRPS2 来自加州大学河滨分校的Yinsheng Wang教授应用PRM技术筛选出介导结肠癌细胞转移促进因 ...

  4. 学习Vim的四周计划

    来源:Python程序员 ID:pythonbuluo vim具有自定义配色方案,语法高亮,linting和自动填充功能 Vim是一个以非常难学而闻名的命令行文本编辑器(有个关于Vim的笑话:问如何生 ...

  5. jenkins+maven+Tomcat+shell构建自动化部署

    https://yq.aliyun.com/articles/685931 1.官网下载war包:jenkins本质上就是一个web应用,直接下载jenkins的war包通过tomcat运行即可.ht ...

  6. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  7. kindle资源

    化繁为简!Kindle 漫画和电子书 资源汇总 & 用法 刚入Kindle那会,在网上翻看了不少文章,有的讲怎么下载电子书,有的讲怎么看漫画,有的讲怎么设置推送邮箱…… 好吧,我不想在用Kin ...

  8. BufferedReader readLine

    import org.apache.commons.codec.binary.Base64;import org.apache.commons.codec.digest.DigestUtils; In ...

  9. TCP/IP三次握手与四次挥手的正确姿势

    0.史上最容易理解的:TCP三次握手,四次挥手 https://cloud.tencent.com/developer/news/257281 A 理解TCP/IP三次握手与四次挥手的正确姿势http ...

  10. 去除 Git 安装后的右键菜单

    64位 windows 8.1 安装 Git 后,右键菜单多了3个选项(Git Init Here,Git Gui, Git Bash),但是用不着,需要删掉.方法如下: 1.在 CMD 中进入 Gi ...