GridView的 PreRender事件與 RowCreated、RowDataBound事件大亂鬥

GridView的 PreRender事件與 RowCreated、RowDataBound事件大亂鬥

 

 

 

以前寫了幾個範例,作了GridView的 PreRender事件與 RowCreated、RowDataBound事件express

這三種事件的示範ide

 

簡單的說,若是您只想 "看" 文字說明就能懂post

那MSDN原廠網站 屹立數年了,您仍是看不懂或是作不出來。網站

因此,「實做(動手作)」能夠解決一切困擾spa

 

如今有同一個範例,用「不一樣做法」營造出「相同成果」應該是最好的比較方式。.net

=================================================================orm

題目:計算每一頁的學生數學成績(加總、累加統計總分server

=================================================================對象

 

先從簡單的講起:blog

第一,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事件來做?

相關文章
相關標籤/搜索