【问题标题】:How to redirect from a grid view with the DataKey如何使用 DataKey 从网格视图重定向
【发布时间】:2012-01-05 14:29:52
【问题描述】:

我希望网格视图在单击一行时重定向,所以我为网格视图创建了 OnRowCreated,但我无法重定向到我想要的页面

<asp:GridView ID="Grid_Messagetable" runat="server" OnRowCreated="Grid_Messagetable_RowCreated" AllowPaging="False" SelectedIndex="0"
                 DataKeyNames="MsgID" ShowHeaderWhenEmpty="true"
                OnRowDataBound="MyGrid_RowDataBound" AutoGenerateColumns="False" AllowSorting="true"
                OnSorting="gridView_Sorting" Height="16px" Width="647px">     protected void Grid_Messagetable_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("onClick", "this.style.background='#eeff00'");
    }

在这里,我尝试在单击一行时设置背景颜色并且它有效,但是我如何重定向页面,我必须使用 msgID 重定向到 ResponseMetrci.aspx 页面,就像我在下面所做的那样。所以我在 url 中传递了 msgid,以便在响应指标页面中检索它。

Eval("MsgID", "ResponseMetric.aspx?MsgID={0}") %>'

我试过了

e.Row.Attributes["onClick"] = "location.href=
 'ResponseMetric.aspx?MsgID=" + DataBinder.Eval(e.Row.DataItem, "MsgID") + "'";

但我收到以下错误

Uncaught ReferenceError: redirect is not defined
(anonymous function)Messages.aspx:774
onclick

【问题讨论】:

标签: c# asp.net gridview datagrid databound


【解决方案1】:

简单的修复,只需更改您的 RowDataBound 方法,我可以看到您已经实现了包含以下 sn-p:

protected void MyGrid_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onClick"] = string.Format(
            "window.location = 'ResponseMetric.aspx?MsgID={0}';",
            DataBinder.Eval(e.Row.DataItem, "MsgID"));
    }
}

这是一个基本的工作示例,仅适用于 Google:

protected void grdTest_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onClick"] =
            "window.location = 'http://www.google.com/';";
    }
}

【讨论】:

    【解决方案2】:

    如果需要,您可以打开新窗口并关闭现有窗口。试试这个 e.Row.Attributes.Add("onClick","javascript:window.open('ResponseMetric.aspx?MsgID="+your value+"')")。

    【讨论】:

      猜你喜欢
      • 2011-12-25
      • 2014-02-12
      • 2020-01-16
      • 2015-12-22
      • 2017-10-30
      • 1970-01-01
      • 2016-02-11
      • 2019-10-02
      • 1970-01-01
      相关资源
      最近更新 更多