GridViewRow - OnClick

本文关键字:OnClick GridViewRow | 更新日期: 2023-09-27 18:06:29

我想重定向到另一个页面当我点击一个GridViewRow

我已经试过了(没有成功):

[RowName].Attributes["onclick"] = "HttpContext.Current.Response.Redirect('[PageName].aspx')";

怎么做?

谢谢!

GridViewRow - OnClick

不能绑定响应。直接重定向到onclick属性,只能重定向到函数。但是不能将函数绑定到行(单击)。使用Javascript更容易。它不需要PostBack。

将javascript的onclick函数绑定到gridview的OnRowDataBound事件中的一行。

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "goUrl('" + DataBinder.Eval(e.Row.DataItem, "url").ToString() + "')");
        }
    }

然后是一个JavaScript函数

<script type="text/javascript">
    function goUrl(url) {
        location.href = url;
    }
</script>

谢谢!

一开始不起作用,所以我把代码改成了:

e.Row.Attributes.Add("onclick", "goUrl()");
<script>
function goUrl() {
    location.href = "[PageName].aspx";
}
</script>