如何从与DataKey网格视图重定向 [英] How to redirect from a grid view with the DataKey

查看:148
本文介绍了如何从与DataKey网格视图重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想网格视图中单击某一行时重定向,所以我有OnRowCreated网格视图,我不能够重定向到我想要的页面

 < ASP:GridView控件ID =Grid_Messagetable=服务器OnRowCreated =Grid_Messagetable_RowCreatedAllowPaging =FALSE的SelectedIndex =0
                 的DataKeyNames =的MsgIDShowHeaderWhenEmpty =真
                OnRowDataBound =MyGrid_RowDataBound的AutoGenerateColumns =FALSEAllowSorting =真
                OnSorting =gridView_SortingHEIGHT =16像素WIDTH =647px>保护无效Grid_Messagetable_RowCreated(对象发件人,GridViewRowEventArgs E)
    {
        e.Row.Attributes.Add(的onClick,this.style.background ='#eeff00');
    }

在这里,我试图设置背景颜色单击某一行的时候,它的工作,但我怎么能重定向的页​​面,我要重定向到ResponseMetrci.aspx页面与MSGID,正如我在下面做的事情。于是我通过MSGID在URL中,以便我retreive,在响应指标页。

 评估(的MsgID,ResponseMetric.aspx的MsgID = {0}?)%GT;'

我想这

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

但我正在逐渐低于

错误

 未捕获的Ref​​erenceError:重定向没有定义
(匿名函数)Messages.aspx:774
点击时


解决方案

简单的修复,只是改变你的的RowDataBound 方法,我可以看到你已经实施,包括以下片段:

 保护无效MyGrid_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes [的onClick] =的String.Format(
            window.location的='ResponseMetric.aspx的MsgID = {0}?;,
            DataBinder.Eval的(e.Row.DataItem的MsgID));
    }
}

下面是一个基本的工作的例子,只是去谷歌:

 保护无效grdTest_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes [的onClick] =
            了window.location ='http://www.google.com/';;
    }
}

I want the grid view to redirect when a row is clicked, so I have the OnRowCreated for the grid view and I am not able to redirect to the page I wanted

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

Here I tried to set background color when a row is clicked and it worked but how can I redirect the page, I have to redirect to ResponseMetrci.aspx page with the msgID, Just as I am doing below. So I pass the msgid in the url so that I retreive that in the response metric page.

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

i tried this

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

but I am getting the error below

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

解决方案

Simple fix, just change your RowDataBound method which I can see you already have implemented to include the following snippet:

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

Here is a basic working example that just goes to 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/';";
    }
}

这篇关于如何从与DataKey网格视图重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆