locked
Gridview OnClick event - detect column RRS feed

  • Question

  • User-1280276638 posted

    I am using some sample code from https://www.aspsnippets.com/Articles/Add-Row-Click-event-to-GridView-Rows-in-ASPNet.aspx to make my Gridview rows clickable, for the purpose of clicking through to another page.

    As I am also using Bootstrap and Footable, I really don't want column(0) to be triggering the click-through below, because when in narrow screen display modes, column 0 contains a clickable '+' symbol as part of Footable to allow for expansion of hidden columns. My code is below:

        Protected Sub OnRowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)
    
            If e.Row.RowType = DataControlRowType.DataRow Then
                e.Row.Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(ps_sfaGroupGV, "Select$" & e.Row.RowIndex)
                e.Row.Attributes("style") = "cursor:pointer"
            End If
    
        End Sub
        Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
    
            Dim link As String = "http://resp2.ttfcessex.co.uk/ps_sfaItem.aspx?i=" & ps_sfaGroupGV.SelectedRow.Cells(1).Text
            Response.Redirect(link)
    
        End Sub

    I figure that if I can detect that column 0 has been clicked, I can suppress the Response.Redirect (as the Footable expansion in mobile display mode is working correctly). But the EventArgs seem to be passing row details over only. Is there any way that I can accomplish this please?

    Thanks

    John

    Monday, November 13, 2017 10:32 PM

All replies

  • User2103319870 posted

    I really don't want column(0) to be triggering the click-through below, because when in narrow screen display modes, column 0 contains a clickable '+' symbol as part of Footable to allow for expansion of hidden columns

    Change your RowDatabound code like below. Logic here is to loop thorough all cells except the first cell in gridview

    Protected Sub OnRowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)
    
            If e.Row.RowType = DataControlRowType.DataRow Then
                'loop through each cells in gridview except the first column
                For i As Integer = 1 To e.Row.Cells.Count - 1
                    e.Row.Cells(i).Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(ps_sfaGroupGV, "Select$" & e.Row.RowIndex)
                    e.Row.Attributes("style") = "cursor:pointer"
                Next
            End If
    
        End Sub

    Tuesday, November 14, 2017 1:29 AM
  • User-1838255255 posted

    Hi dublevay,

    According to your description, i make a modify based on this site code(https://www.aspsnippets.com/Articles/Add-Row-Click-event-to-GridView-Rows-in-ASPNet.aspx).

    Sample Code:

    <head id="Head1" runat="server">
        <title></title>
        <style type="text/css">
            body
            {
                font-family: Arial;
                font-size: 10pt;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
            runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" 
            OnSelectedIndexChanged="OnSelectedIndexChanged">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
                <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
            </Columns>
        </asp:GridView>
        <asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>
        </form>
    </body>
    
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            If Not Me.IsPostBack Then
                Dim dt As New DataTable()
                dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
                dt.Rows.Add(1, "John Hammond", "United States")
                dt.Rows.Add(2, "Mudassar Khan", "India")
                dt.Rows.Add(3, "Suzanne Mathews", "France")
                dt.Rows.Add(4, "Robert Schidner", "Russia")
                GridView1.DataSource = dt
                GridView1.DataBind()
            End If
        End Sub
    
        Protected Sub OnRowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
                For i As Integer = 0 To e.Row.Cells.Count - 1
                    If i <> 0 Then
                        e.Row.Cells(i).Attributes("onclick") = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" & e.Row.RowIndex)
                        e.Row.Cells(i).Attributes("style") = "cursor:pointer"
                    End If
                Next
            End If
        End Sub
    
        Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
            Dim index As Integer = GridView1.SelectedRow.RowIndex
            Dim name As String = GridView1.SelectedRow.Cells(0).Text
            Dim country As String = GridView1.SelectedRow.Cells(1).Text
            Dim message As String = "Row Index: " & index & "\nName: " & name + "\nCountry: " & country
            ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('" + message + "');", True)
        End Sub

    Result:

    Best Regards,

    Eric Du

    Tuesday, November 14, 2017 6:49 AM
  • User-1280276638 posted

    That's great - many thanks to both of you.

    Tuesday, November 14, 2017 9:18 PM