GridView Pager Row not being seen as Pager Row

Posted on 2009-02-20
Last Modified: 2013-11-07

I have a gridview in which I enabled paging.  I have a OnRowDataBound event in which I detect the type of row and then add css class and postbackevent accordingly.  The problem is that the Pager row seems to be of type DataRow Alternate Row instead of Pager Row.

The first code snippet is my OnRowDataBound event where I check each row.  

Then the second snippet is actually the html source, specifically, the last two rows of the gridview. As you can see, somehow the both have the css class "alternate" which is only for alternate rows and the pager row also got the clickable row postback event.  

Anybody know why I am not able to detect the pager row separately from the data rows?

Public Sub TripFileSearch_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        If (e.Row.RowType = DataControlRowType.Pager) Then

            e.Row.CssClass = "pager"

        ElseIf (e.Row.RowType = DataControlRowType.DataRow) Then

            'Add postbackevents for clickable rows

            'Check for normal or alternate row and add css

        End If

    End Sub		


</tr><tr class="alternate" onclick="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch$ctl21$ctl00','')" onmouseover="'#CCCCCC'" onmouseout="'#E5E5E5'" style="cursor:pointer;cursor:hand;">

			<td style="width:112px;">Test Tripfile</td><td style="width:114px;">Holland America Line</td><td style="width:70px;">&nbsp;</td><td style="width:85px;">10/25/2008</td><td style="width:75px;">Cruise</td><td style="width:70px;">Booked</td><td style="width:55px;">7</td><td style="width:115px;">jackson - 200010</td><td style="width:100px;">Michelle Goryl</td><td style="width:60px;">09/04/08</td>


		</tr><tr class="alternate" onclick="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch$ctl23$ctl00','')" onmouseover="'#CCCCCC'" onmouseout="'#E5E5E5'" style="cursor:pointer;cursor:hand;">

			<td colspan="10">

				<table border="0">



					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$2')">2</a></td>

					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$3')">3</a></td>

					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$4')">4</a></td>

					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$5')">5</a></td>

					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$6')">6</a></td>

					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$7')">7</a></td>

					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$8')">8</a></td>

					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$9')">9</a></td>

					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$10')">10</a></td>

					<td><a href="javascript:__doPostBack('ctl00$MainContentPlaceHolder$gvTripFileSearch','Page$11')">...</a></td>





Open in new window

Question by:kruegerste
    LVL 12

    Accepted Solution

    Try this in the RowCreated event (DataControlRowType.Pager) instead of RowDataBound.
    LVL 4

    Author Comment

    Ok, so doing it in the RowCreated event seemed to fix that problem.  First off, why is that?  Second off, it created another issue.

    I have an invisible buttonfield in which I'm taking the postback event from it on each row and attaching it to the entire row so it is clickable.  This can't be done until the DataBound event.  See snippet of code.

    So I either need to find a way to detect the pager row in the DataBound event or find another way to make the whole row clickable.
            If (e.Row.RowType = DataControlRowType.DataRow) Then
                '' Get the LinkButton control in the first cell
                Dim _singleClickButton As LinkButton = DirectCast(e.Row.Cells(0).Controls(0), LinkButton)
                '' Get the javascript which is assigned to this LinkButton
                Dim _jsSingle As String = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "")
                '' Add this javascript to the onclick Attribute of the row
                e.Row.Attributes("onclick") = _jsSingle
                e.Row.Attributes("style") = "cursor:pointer;cursor:hand;"
            End If

    Open in new window

    LVL 12

    Expert Comment

    To make the row clickable, you can have it set on the Cells of each row, if setting on the entire row won't work, and catch it using the SelectedIndexChanging event.  If you are doing this on a sinlge column, then you can trap it in the RowCommand event, won't you?
    LVL 4

    Author Comment

    Sorry but I'm not sure exactly what you are proposing.  But I need the whole row to be clickable and the way I'm doing it is the only way I know how.  I can not use any buttons or links in a single field.  And I have searched the web and have not found a better way.  

    So if you or anybody else has a better way where I don't have to use the DataBound event, please let me know.  Maybe some specifics (code) on the method you are suggesting?  
    LVL 12

    Expert Comment

    In the rowdatabound event, can you try this.  This will initiate a postback onclick in row and the SelectedIndexChanging event will be called.

    if (e.Row.RowType == DataControlRowType.DataRow){//Datarow
        if (((e.Row.RowState & DataControlRowState.Edit) == 0)){//if it is not in edit mode
            for(int i=0;i<e.Row.Cells.Count;i++){
                  e.Row.Cells[i].Attributes.Add("onclick", "__doPostBack('" + TripFileSearch.UniqueID +
                                        "','Select$" + e.Row.RowIndex.ToString().Trim() + "')");
    LVL 4

    Author Comment

    When using the code you suggested, I get the following error.  The manually created postbacks aren't registered in the event validation.  The code snippet I use to fix this issue with the way I was creating the clickable rows is below but it isn't working with your suggestion. Any thoughts?  Also, is there a reason I'm putting the onclick event on every cell versus just applying it to the entire row (as such e.Row.Attributes.Add.....)?

    Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

        Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
            For Each row As GridViewRow In gvTripFileSearch.Rows
                If (row.RowType = DataControlRowType.DataRow) Then
                    Page.ClientScript.RegisterForEventValidation(row.UniqueID & "$ctl00")
                End If
        End Sub

    Open in new window

    LVL 4

    Author Closing Comment

    Thanks for the help, much appreciated. I moved the CSS assignments to the RowCreated event as you suggested, then I just check the css class assigned in RowDataBound before assigning postback event as I originally had.  Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now