[英]How to make entire Gridview Row clickable and open a new link?
如何在ASP.NET中使整個gridview行可單擊?
這是我目前正在嘗試的方法:
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
每行包含多個模板字段,因此在這種情況下,僅向該字段添加超鏈接是不夠的。
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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.