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) { // 参考范例 http://www.dotblogs.com.tw/mis2000lab/archive/2012/01/13/gridview_multi_row_updating_20120113.aspx 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,点选多列资料(复选删除)
相同范例有多种解法,也可以参阅这篇文章: