简体   繁体   中英

How to make entire Gridview Row clickable and open a new link?

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.

How to add a hyperlink to a gridview row using .NET and a little javascript magic.


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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM