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.9539
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.13425
EditCommon DataGrid Mistakes11/6/2003This article, by the DataGridGirl herself, lists the most common mistakes and blunders developers make when using the DataGrid.17824
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.13544
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.5936


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:wpegf245np2ged2dnxbox5ujRequest Type:GET
Time of Request:3/11/2010 10:44:24 AMStatus 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 PreInit1.50291233011408E-050.000015
aspx.pageBegin Init2.85983317639804E-050.000014
aspx.pageEnd Init4.37224495237778E-050.000015
aspx.pageBegin InitComplete5.40668461006441E-050.000010
aspx.pageEnd InitComplete6.44662394693643E-050.000010
aspx.pageBegin PreLoad7.45506512120126E-050.000010
aspx.pageEnd PreLoad8.57499979167882E-050.000011
aspx.pageBegin Load9.59694017848959E-050.000010
EVENT NOTIFICATIONPage_Load has fired!0.0009374503153982680.000841
aspx.pageEnd Load0.0009564992042130880.000019
aspx.pageBegin LoadComplete0.0009678635412934250.000011
aspx.pageEnd LoadComplete0.0009783079320372980.000010
aspx.pageBegin PreRender0.0009911271842475860.000013
aspx.pageEnd PreRender0.001013130900697460.000022
aspx.pageBegin PreRenderComplete0.001024035264609560.000011
aspx.pageEnd PreRenderComplete0.001034364662061380.000010
aspx.pageBegin SaveState0.002162248868815990.001128
aspx.pageEnd SaveState0.002294221170431720.000132
aspx.pageBegin SaveStateComplete0.002307960368978480.000014
aspx.pageEnd SaveStateComplete0.002318594748639660.000011
aspx.pageBegin Render0.002328724157757460.000010
aspx.pageEnd Render0.003360338980226150.001032

Control Tree

Control UniqueIDTypeRender Size Bytes (including children)ViewState Size Bytes (excluding children)ControlState Size Bytes (excluding children)
__PageASP.demos_datagrideventorder_aspx1163600
    ctl01System.Web.UI.ResourceBasedLiteralControl68400
    ctl00System.Web.UI.HtmlControls.HtmlForm692400
        ctl02System.Web.UI.LiteralControl2700
        dgArticlesSystem.Web.UI.WebControls.DataGrid38351040
            dgArticles$ctl00System.Web.UI.WebControls.ChildTable383500
                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.DataGridItem67000
                    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.TableCell65240
                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
ASPSESSIONIDCCSRSTAQMICBKBFAIOLFHMBMODDIPJHH45

Response Cookies Collection

NameValueSize

Headers Collection

NameValue
Cache-Controlno-cache
Connectionclose
Pragmano-cache
Accepttext/xml,application/xhtml+xml,text/html;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
CookieASPSESSIONIDCCSRSTAQ=MICBKBFAIOLFHMBMODDIPJHH
Hostdatawebcontrols.com
User-AgentCCBot/1.0 (+http://www.commoncrawl.org/bot.html)
x-cc-idccc03-01

Response Headers Collection

NameValue
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/xml,application/xhtml+xml,text/html;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_COOKIE:ASPSESSIONIDCCSRSTAQ=MICBKBFAIOLFHMBMODDIPJHH HTTP_HOST:datawebcontrols.com HTTP_USER_AGENT:CCBot/1.0 (+http://www.commoncrawl.org/bot.html) HTTP_X_CC_ID:ccc03-01
ALL_RAWCache-Control: no-cache Connection: close Pragma: no-cache Accept: text/xml,application/xhtml+xml,text/html;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 Cookie: ASPSESSIONIDCCSRSTAQ=MICBKBFAIOLFHMBMODDIPJHH Host: datawebcontrols.com User-Agent: CCBot/1.0 (+http://www.commoncrawl.org/bot.html) x-cc-id: ccc03-01
APPL_MD_PATH/LM/w3svc/3033/ROOT
APPL_PHYSICAL_PATHD:\hosting\member\skim123\
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_ID3033
INSTANCE_META_PATH/LM/W3SVC/3033
LOCAL_ADDR209.132.240.46
PATH_INFO/demos/DataGridEventOrder.aspx
PATH_TRANSLATEDD:\hosting\member\skim123\demos\DataGridEventOrder.aspx
QUERY_STRING 
REMOTE_ADDR38.107.191.112
REMOTE_HOST38.107.191.112
REMOTE_PORT48815
REQUEST_METHODGET
SCRIPT_NAME/demos/DataGridEventOrder.aspx
SERVER_NAMEdatawebcontrols.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/xml,application/xhtml+xml,text/html;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_COOKIEASPSESSIONIDCCSRSTAQ=MICBKBFAIOLFHMBMODDIPJHH
HTTP_HOSTdatawebcontrols.com
HTTP_USER_AGENTCCBot/1.0 (+http://www.commoncrawl.org/bot.html)
HTTP_X_CC_IDccc03-01

Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082