0

Is there a way to add a RowClick Event to Gridview Rows without adding a button to each one? I want to be able to click anywhere in the row and raise the SelectedIndexChanged event.

I tried the following but I need to add pages enableEventValidation="true" and I really don't want to do that.

Private Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    Try
        Select Case e.Row.RowType
            Case DataControlRowType.DataRow
                e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" & e.Row.RowIndex))
        End Select
    Catch ex As Exception

    End Try
End Sub

The RowCommands wont work either because you have to have a button.

Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
Troy Mitchel
  • 1,790
  • 11
  • 50
  • 88
  • possible duplicate of [Making an entire row clickable in a gridview](http://stackoverflow.com/questions/686240/making-an-entire-row-clickable-in-a-gridview) – Steve Wellens Oct 16 '12 at 15:28

1 Answers1

2

You did step 1 of 2. First part was registering the onclick in the RowDataBound. Now you need to handle the SelectedIndexChanged event. I'm not a VB.Net guy, so I'm not 100% sure on how to wire it up. The event handler will look like this, though:

Private Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    ' Do stuff here.
End Sub

Also, change your onclick wire up code to use Page.ClientScript.GetPostBackEventReference instead of Page.ClientScript.GetPostBackClientHyperlink.

Here is some example markup:

<asp:GridView ID="GridView1"
    runat="server"
    Width="665px"
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged">

    <HeaderStyle BackColor="#0033CC" />

    <Columns>
        <asp:CommandField ShowSelectButton="True" />
    </Columns>

</asp:GridView>
Gromer
  • 9,861
  • 4
  • 34
  • 55
  • You are correct, however I get error about enabling page events which could compromise security. – Troy Mitchel Oct 16 '12 at 15:38
  • @tszoro, check my edit, I noticed you were using a different `Page.ClientScript` method than I was in my code. – Gromer Oct 16 '12 at 15:40
  • After editing the code I get Server Error Invalid postback or callback argument. Event validation is enabled using . But it does hit the page load. – Troy Mitchel Oct 16 '12 at 15:47
  • Hmmm, I haven't seen that. My test application on my computer is all in C#, FWIW. – Gromer Oct 16 '12 at 15:51
  • If I put EnableEventValidation="false" in my page declaration then everything works great. But that is probably not a wise move for security purpose. Do you have any other suggestions for this? – Troy Mitchel Oct 16 '12 at 16:01
  • 3
    To fix the rest of it I looped the GridView rows and added gr.Attributes.Add("onclick",Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" & gr.RowIndex, True)) to each gridview row in the Protected Overrides Sub Render(writer As System.Web.UI.HtmlTextWriter) event and all works now. thanks – Troy Mitchel Oct 16 '12 at 17:09
  • 2
    Note: As an alternative for this you can also write a __doPostBack event in Javascript and get the params and eventtarget on Postback. function GridViewPostBack(row_idx) { __doPostBack(row_idx, "GridView1RowClick"); } – Troy Mitchel Oct 16 '12 at 18:15