1

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
Mark
  • 2,720
  • 15
  • 56
  • 87

2 Answers2

2

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/';";
    }
}
Kelsey
  • 47,246
  • 16
  • 124
  • 162
0

You can open new window and close existing one if you require so. Try this one e.Row.Attributes.Add("onClick","javascript:window.open('ResponseMetric.aspx?MsgID="+your value+"')").

Sudipta
  • 36
  • 2