Reputation: 34177
How to make an entire gridview row clickable in ASP.NET?
This is what I am trying at the moment:
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", "location.href='URL.aspx'")
End If
End Sub
Each row contains multiple template fields so simply adding a hyperlink to the field is not sufficient in this instance.
Upvotes: 1
Views: 2502
Reputation: 34177
VB
Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gv.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim QueryString As String = DataBinder.Eval(e.Row.DataItem, "QueryString").ToString
Dim NavigateURL As String = (ResolveUrl("~/URL.aspx?QueryString=" + QueryString))
e.Row.Attributes.Add("onClick", String.Format("javascript:window.location='{0}';", NavigateURL))
e.Row.Style.Add("cursor", "pointer")
End If
End Sub
C#
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) {
if ((e.Row.RowType == DataControlRowType.DataRow)) {
string QueryString = DataBinder.Eval(e.Row.DataItem, "QueryString").ToString;
string NavigateURL = ResolveUrl(("~/URL.aspx?QueryString=" + QueryString));
e.Row.Attributes.Add("onClick", string.Format("javascript:window.location=\'{0}\';", NavigateURL));
e.Row.Style.Add("cursor", "pointer");
}
}
Upvotes: 1