1110 1110 - 8 days ago 4
ASP.NET (C#) Question

How to redirect to another page from gridview edit event

I don't know am I using good approach.

I have a view page with

gridview
and edit page.

In gridview I have added:

...
<Columns>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="true"></asp:CommandField>
...


And event:

OnRowEditing="rgrContact_RowEditing"


In event method I have tried to add:

protected void rgrContact_RowEditing(object sender, GridViewEditEventArgs e)
{
var id = (int)rgrContact.DataKeys[e.RowIndex].Value;
Response.Redirect("..." + id);


But there is no
e.RowIndex
.

How to get ID of the selected row so I can pass it to edit page?

Am I using good event?

Answer

try this:

NewEditIndex returns index of row on which you click for Edit

protected void rgrContact_RowEditing(object sender, GridViewEditEventArgs e)
        {
            var id = (int)rgrContact.DataKeys[e.NewEditIndex].Value; 
            Response.Redirect("MyEditPage.aspx?MyID=" + id);

EDIT:

DataKeyNames="DataKey1,DataKey2"

You can Use Like this

string DataKey1= rgrContact.DataKeys[e.NewEditIndex].Values[0].ToString();
string DataKey2= rgrContact.DataKeys[e.NewEditIndex].Values[1].ToString();

OR

rgrContact.DataKeys[e.NewEditIndex].Values["DataKey1"];
rgrContact.DataKeys[e.NewEditIndex].Values["DataKey2"];

As Others said No Need to Use what you said another solution :

 <ItemTemplate>
  <asp:HyperLink ID="hyper" Text="this is link"
   NavigateUrl= '<%# String.Format("{0}","MYpage.aspx?id=" + Eval("MY_ID_COL")) %>' runat="server" /> 

   </ItemTemplate>