DataGrid Event Order

This demo shows that the Page's Load event always fires before any of the DataGrid events. To see this in action, click on the Edit hyperlink of the DataGrid. Then, scroll down to the Trace Information section. There you'll see that the Page_Load event handler's Trace.Write() statement is outputted prior to the EditCommand event handler's Trace.Write(). (For more information about tracing see: Tracing in ASP.NET.)


 TitleDate WrittenCommentsViews
EditBidirectional Sorting with Up and Down Arrows in the Header6/15/2005This article shows how to create a bi-directional sortable DataGrid that displays an up or down arrow in the header of the column that the grid is sorted by, depending on if the data is sorted in ascending or descending order.10443
EditCreating a Fully Editable DataGrid12/15/2004Learn how to create a DataGrid where each record in the grid is editable, rather than having to edit one record at a time. Useful for scenarios where the user needs to update a large amount of data at once.14616
EditCommon DataGrid Mistakes11/6/2003This article, by the DataGridGirl herself, lists the most common mistakes and blunders developers make when using the DataGrid.18940
EditCreating Custom Columns for the ASP.NET Datagrid9/10/2003This article, by DataGridGirl herself, examines how to build a custom DataGrid column that displays a databound DropDownList.14696
EditDataGrid Made Compliant with Section 508 of the Web Accessibility Initiative Guidelines8/19/2003The DataGrid control that was included with the .NET Framework 1.1 was not compliant with Section 508 of the Rehabilitation Act (www.Section508.gov) or with the World Wide Web Consortium (W3C) Web Accessibility Initiative (WAI). Data tables that contain two or more rows or columns must identify row and column headers. Microsoft has released a HotFix that you can install that will have DataGrids render according to 508 specifications.6695


Source Code

<%@ Page Language="VB" Trace="True" %>
<script runat="server">

    Sub Page_Load(sender as Object, e as EventArgs)
        If Not Page.IsPostBack then
           dgArticles.DataSource = GetArticles("DateAuthored DESC")
           dgArticles.DataBind()
        End If
        
        Trace.Write("EVENT NOTIFICATION", "Page_Load has fired!")
    End Sub
    
    
     Function GetArticles(sortBy as String) As System.Data.SqlClient.SqlDataReader
         Dim connectionString As String = "ConnectionString"
         Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)
    
         Dim queryString As String = "SELECT TOP 5 ArticleID, [Articles].[Title], [Articles].[URL], [Articles].[dateAuthored], [Articles"& _
"].[Comments], [Articles].[ClickThroughs] FROM [Articles] ORDER BY " & sortBy
         Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
    
         sqlConnection.Open
         Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
         Return dataReader
     End Function
    
    
   Sub dgArticles_Edit(sender as Object, e as DataGridCommandEventArgs)
     dgArticles.EditItemIndex = e.Item.ItemIndex
     
     dgArticles.DataSource = GetArticles("DateAuthored DESC")
     dgArticles.DataBind()
     
     Trace.Write("EVENT NOTIFICATION", "EditCommand has fired!")
   End Sub

   Sub dgArticles_Cancel(sender as Object, e as DataGridCommandEventArgs)
     dgArticles.EditItemIndex = -1
     
     dgArticles.DataSource = GetArticles("DateAuthored DESC")
     dgArticles.DataBind()

     Trace.Write("EVENT NOTIFICATION", "CancelCommand has fired!")
   End Sub

   Sub dgArticles_Update(sender as Object, e as DataGridCommandEventArgs)
     dgArticles.EditItemIndex = -1
     
     dgArticles.DataSource = GetArticles("DateAuthored DESC")
     dgArticles.DataBind()

     Trace.Write("EVENT NOTIFICATION", "UpdateCommand has fired!")
   End Sub

</script>


    <form runat="server">
        <p>
            <asp:DataGrid id="dgArticles" runat="server" AutoGenerateColumns="False" Font-Size="10pt" Font-Names="Verdana"
					OnEditCommand="dgArticles_Edit" OnUpdateCommand="dgArticles_Update" OnCancelCommand="dgArticles_Cancel">
                <HeaderStyle font-size="13pt" font-bold="True" horizontalalign="Center" forecolor="White" backcolor="#006699"></HeaderStyle>
                <AlternatingItemStyle backcolor="#EEEEEE"></AlternatingItemStyle>
                <Columns>
					<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
                    <asp:HyperLinkColumn DataNavigateUrlField="ArticleID" DataNavigateUrlFormatString="/Articles/Goto.aspx?ID={0}" DataTextField="Title" SortExpression="Title" HeaderText="Title"></asp:HyperLinkColumn>
                    <asp:BoundColumn DataField="DateAuthored" SortExpression="dateAuthored DESC" HeaderText="Date Written" DataFormatString="{0:d}"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Comments" HeaderText="Comments"></asp:BoundColumn>
                    <asp:BoundColumn DataField="ClickThroughs" SortExpression="ClickThroughs DESC" HeaderText="Views" DataFormatString="{0:d}" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>            
        </p>
    </form>
    

[Return to the FAQ...]

Request Details

Session Id:xvjz5cuwdvoocm55ozfkabflRequest Type:GET
Time of Request:2/4/2012 7:56:23 PMStatus Code:200
Request Encoding:Unicode (UTF-8)Response Encoding:Unicode (UTF-8)

Trace Information

CategoryMessageFrom First(s)From Last(s)
aspx.pageBegin PreInit
aspx.pageEnd PreInit0.004684234703932630.004684
aspx.pageBegin Init0.01801968001433310.013335
aspx.pageEnd Init0.01812529606736540.000106
aspx.pageBegin InitComplete0.01814797240885070.000023
aspx.pageEnd InitComplete0.01817358989518950.000026
aspx.pageBegin PreLoad0.01819527582191170.000022
aspx.pageEnd PreLoad0.01822011358953690.000025
aspx.pageBegin Load0.01825155889993730.000031
EVENT NOTIFICATIONPage_Load has fired!0.06257489205410730.044323
aspx.pageEnd Load0.06264518713607450.000070
aspx.pageBegin LoadComplete0.06266815013885160.000023
aspx.pageEnd LoadComplete0.06269100707695060.000023
aspx.pageBegin PreRender0.06271213258084740.000021
aspx.pageEnd PreRender0.06275444522081880.000042
aspx.pageBegin PreRenderComplete0.062777977246260.000024
aspx.pageEnd PreRenderComplete0.06279917441547970.000021
aspx.pageBegin SaveState0.08107170043895010.018273
aspx.pageEnd SaveState0.101394134909970.020322
aspx.pageBegin SaveStateComplete0.1014463731971690.000052
aspx.pageEnd SaveStateComplete0.1014705244110010.000024
aspx.pageBegin Render0.1014916169488490.000021
aspx.pageEnd Render0.1141800397742540.012688

Control Tree

Control UniqueIDTypeRender Size Bytes (including children)ViewState Size Bytes (excluding children)ControlState Size Bytes (excluding children)
__PageASP.demos_datagrideventorder_aspx1163700
    ctl01System.Web.UI.ResourceBasedLiteralControl68400
    ctl00System.Web.UI.HtmlControls.HtmlForm692500
        ctl02System.Web.UI.LiteralControl2700
        dgArticlesSystem.Web.UI.WebControls.DataGrid38361040
            dgArticles$ctl00System.Web.UI.WebControls.ChildTable383600
                dgArticles$ctl01System.Web.UI.WebControls.DataGridItem42100
                    dgArticles$ctl01$ctl00System.Web.UI.WebControls.TableCell7500
                    dgArticles$ctl01$ctl01System.Web.UI.WebControls.TableCell7200
                    dgArticles$ctl01$ctl02System.Web.UI.WebControls.TableCell7900
                    dgArticles$ctl01$ctl03System.Web.UI.WebControls.TableCell7500
                    dgArticles$ctl01$ctl04System.Web.UI.WebControls.TableCell7200
                dgArticles$ctl02System.Web.UI.WebControls.DataGridItem67100
                    dgArticles$ctl02$ctl02System.Web.UI.WebControls.TableCell11900
                        dgArticles$ctl02$ctl00System.Web.UI.WebControls.DataGridLinkButton7100
                    dgArticles$ctl02$ctl03System.Web.UI.WebControls.TableCell14500
                        dgArticles$ctl02$ctl01System.Web.UI.WebControls.HyperLink991520
                    dgArticles$ctl02$ctl04System.Web.UI.WebControls.TableCell55320
                    dgArticles$ctl02$ctl05System.Web.UI.WebControls.TableCell2723240
                    dgArticles$ctl02$ctl06System.Web.UI.WebControls.TableCell66280
                dgArticles$ctl03System.Web.UI.WebControls.DataGridItem64600
                    dgArticles$ctl03$ctl02System.Web.UI.WebControls.TableCell11900
                        dgArticles$ctl03$ctl00System.Web.UI.WebControls.DataGridLinkButton7100
                    dgArticles$ctl03$ctl03System.Web.UI.WebControls.TableCell12000
                        dgArticles$ctl03$ctl01System.Web.UI.WebControls.HyperLink741200
                    dgArticles$ctl03$ctl04System.Web.UI.WebControls.TableCell56320
                    dgArticles$ctl03$ctl05System.Web.UI.WebControls.TableCell2532960
                    dgArticles$ctl03$ctl06System.Web.UI.WebControls.TableCell66280
                dgArticles$ctl04System.Web.UI.WebControls.DataGridItem53700
                    dgArticles$ctl04$ctl02System.Web.UI.WebControls.TableCell11900
                        dgArticles$ctl04$ctl00System.Web.UI.WebControls.DataGridLinkButton7100
                    dgArticles$ctl04$ctl03System.Web.UI.WebControls.TableCell11000
                        dgArticles$ctl04$ctl01System.Web.UI.WebControls.HyperLink641040
                    dgArticles$ctl04$ctl04System.Web.UI.WebControls.TableCell55320
                    dgArticles$ctl04$ctl05System.Web.UI.WebControls.TableCell1731880
                    dgArticles$ctl04$ctl06System.Web.UI.WebControls.TableCell66280
                dgArticles$ctl05System.Web.UI.WebControls.DataGridItem57700
                    dgArticles$ctl05$ctl02System.Web.UI.WebControls.TableCell11900
                        dgArticles$ctl05$ctl00System.Web.UI.WebControls.DataGridLinkButton7100
                    dgArticles$ctl05$ctl03System.Web.UI.WebControls.TableCell13400
                        dgArticles$ctl05$ctl01System.Web.UI.WebControls.HyperLink881360
                    dgArticles$ctl05$ctl04System.Web.UI.WebControls.TableCell55320
                    dgArticles$ctl05$ctl05System.Web.UI.WebControls.TableCell1711880
                    dgArticles$ctl05$ctl06System.Web.UI.WebControls.TableCell66280
                dgArticles$ctl06System.Web.UI.WebControls.DataGridItem91000
                    dgArticles$ctl06$ctl02System.Web.UI.WebControls.TableCell11900
                        dgArticles$ctl06$ctl00System.Web.UI.WebControls.DataGridLinkButton7100
                    dgArticles$ctl06$ctl03System.Web.UI.WebControls.TableCell17300
                        dgArticles$ctl06$ctl01System.Web.UI.WebControls.HyperLink1271880
                    dgArticles$ctl06$ctl04System.Web.UI.WebControls.TableCell55320
                    dgArticles$ctl06$ctl05System.Web.UI.WebControls.TableCell4846040
                    dgArticles$ctl06$ctl06System.Web.UI.WebControls.TableCell65240
                dgArticles$ctl07System.Web.UI.WebControls.DataGridItem000
                    dgArticles$ctl07$ctl00System.Web.UI.WebControls.TableCell000
                    dgArticles$ctl07$ctl01System.Web.UI.WebControls.TableCell000
                    dgArticles$ctl07$ctl02System.Web.UI.WebControls.TableCell000
                    dgArticles$ctl07$ctl03System.Web.UI.WebControls.TableCell000
                    dgArticles$ctl07$ctl04System.Web.UI.WebControls.TableCell000
        ctl03System.Web.UI.LiteralControl3200
    ctl04System.Web.UI.ResourceBasedLiteralControl402800

Session State

Session KeyTypeValue

Application State

Application KeyTypeValue

Request Cookies Collection

NameValueSize

Response Cookies Collection

NameValueSize

Headers Collection

NameValue
Cache-Controlno-cache
Connectionclose
Pragmano-cache
Accepttext/html,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-CharsetISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encodinggzip
Accept-Languageen-us,en;q=0.5
Hostwww.datawebcontrols.com
If-Modified-SinceMon, 09 Aug 2010 10:05:17 GMT
User-AgentCCBot/1.0 (+http://www.commoncrawl.org/bot.html)
x-cc-idccc02-02

Response Headers Collection

NameValue
X-AspNet-Version2.0.50727
Cache-Controlprivate
Content-Typetext/html

Form Collection

NameValue

Querystring Collection

NameValue

Server Variables

NameValue
ALL_HTTPHTTP_CACHE_CONTROL:no-cache HTTP_CONNECTION:close HTTP_PRAGMA:no-cache HTTP_ACCEPT:text/html,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 HTTP_ACCEPT_CHARSET:ISO-8859-1,utf-8;q=0.7,*;q=0.7 HTTP_ACCEPT_ENCODING:gzip HTTP_ACCEPT_LANGUAGE:en-us,en;q=0.5 HTTP_HOST:www.datawebcontrols.com HTTP_IF_MODIFIED_SINCE:Mon, 09 Aug 2010 10:05:17 GMT HTTP_USER_AGENT:CCBot/1.0 (+http://www.commoncrawl.org/bot.html) HTTP_X_CC_ID:ccc02-02
ALL_RAWCache-Control: no-cache Connection: close Pragma: no-cache Accept: text/html,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Encoding: gzip Accept-Language: en-us,en;q=0.5 Host: www.datawebcontrols.com If-Modified-Since: Mon, 09 Aug 2010 10:05:17 GMT User-Agent: CCBot/1.0 (+http://www.commoncrawl.org/bot.html) x-cc-id: ccc02-02
APPL_MD_PATH/LM/W3SVC/3710/ROOT
APPL_PHYSICAL_PATHC:\Inetpub\wwwroot\fuzzylogicinc.net\wwwroot\
AUTH_TYPE 
AUTH_USER 
AUTH_PASSWORD 
LOGON_USER 
REMOTE_USER 
CERT_COOKIE 
CERT_FLAGS 
CERT_ISSUER 
CERT_KEYSIZE 
CERT_SECRETKEYSIZE 
CERT_SERIALNUMBER 
CERT_SERVER_ISSUER 
CERT_SERVER_SUBJECT 
CERT_SUBJECT 
CONTENT_LENGTH0
CONTENT_TYPE 
GATEWAY_INTERFACECGI/1.1
HTTPSoff
HTTPS_KEYSIZE 
HTTPS_SECRETKEYSIZE 
HTTPS_SERVER_ISSUER 
HTTPS_SERVER_SUBJECT 
INSTANCE_ID3710
INSTANCE_META_PATH/LM/W3SVC/3710
LOCAL_ADDR70.85.20.8
PATH_INFO/demos/DataGridEventOrder.aspx
PATH_TRANSLATEDC:\Inetpub\wwwroot\fuzzylogicinc.net\wwwroot\demos\DataGridEventOrder.aspx
QUERY_STRING 
REMOTE_ADDR38.107.179.221
REMOTE_HOST38.107.179.221
REMOTE_PORT35825
REQUEST_METHODGET
SCRIPT_NAME/demos/DataGridEventOrder.aspx
SERVER_NAMEwww.datawebcontrols.com
SERVER_PORT80
SERVER_PORT_SECURE0
SERVER_PROTOCOLHTTP/1.1
SERVER_SOFTWAREMicrosoft-IIS/6.0
URL/demos/DataGridEventOrder.aspx
HTTP_CACHE_CONTROLno-cache
HTTP_CONNECTIONclose
HTTP_PRAGMAno-cache
HTTP_ACCEPTtext/html,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
HTTP_ACCEPT_CHARSETISO-8859-1,utf-8;q=0.7,*;q=0.7
HTTP_ACCEPT_ENCODINGgzip
HTTP_ACCEPT_LANGUAGEen-us,en;q=0.5
HTTP_HOSTwww.datawebcontrols.com
HTTP_IF_MODIFIED_SINCEMon, 09 Aug 2010 10:05:17 GMT
HTTP_USER_AGENTCCBot/1.0 (+http://www.commoncrawl.org/bot.html)
HTTP_X_CC_IDccc02-02

Microsoft .NET Framework Version:2.0.50727.3625; ASP.NET Version:2.0.50727.3634