Link to home
Start Free TrialLog in
Avatar of kruegerste
kruegersteFlag for United States of America

asked on

GridView Pager Row not being seen as Pager Row

Hello,

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?

Thanks.
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="this.style.backgroundColor='#CCCCCC'" onmouseout="this.style.backgroundColor='#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="this.style.backgroundColor='#CCCCCC'" onmouseout="this.style.backgroundColor='#E5E5E5'" style="cursor:pointer;cursor:hand;">
			<td colspan="10">
				<table border="0">
				     <tr>
					<td><span>1</span></td>
					<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>
				     </tr>
				</table>
			</td>
		</tr>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CB_Thirumalai
CB_Thirumalai
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kruegerste

ASKER

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

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?
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?  
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() + "')");
        }
    }
}
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.....)?

ERROR
==================
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
        Next
 
        MyBase.Render(writer)
    End Sub

Open in new window

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.