views:

176

answers:

3

I have a web app (ASP.NET 2.0 C#), and on one of the pages I have a Gridview. The Gridview has 3 columns (Edit, ID, Name), and sorting is enabled. The Edit doesn't work in the conventional way: It uses the ID and adds it to the QueryString, and the user is taken to the Edit page. Something like this:

protected void Grid_RowEditing(object sender, GridViewEditEventArgs e)
{
   string editID = Grid.DataKeys[e.NewEditIndex].Value.ToString();  
   Response.Redirect("~/Admin/Edit_Page.aspx?EditID=" +
                     HttpUtility.HtmlDecode(editID));
}

When the page loads, the grid is not sorted in anyway. If I click edit, it works fine. But if I click Edit AFTER sorting, it passes the ID of the row that was originally there, before sorting, instead of the one that is there at present.

Why is this happening? Any ideas?

Thank you.

+1  A: 

Are you rebinding your data set?

ShaunLMason
this is something you would post in a comment
TStamper
A: 

At the page load try to bind the grid when the page is not posted back. I mean the following code in the page load:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
        PopulateGrid();
}

This is because I suspect, the grid is getting rebinded on each post back which may cause the problem.

Anindya Sengupta
Sorry...That didn't work...
zohair
A: 

If want to keep the way it is working, there may be another possible solution. If you use buttons for editing and user CommandName of the button to be "edit" and CommandArgument to be the id of the entity being edited, then it may work. I give you the code below.

Code behind:

protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        int intEditId = Convert.ToInt32(e.CommandArgument);
        Response.Redirect("~/Admin/Edit_Page.aspx?EditID=" + intEditId);

    }
 }

aspx file:

in the grid:

<asp:TemplateField>
   <ItemTemplate><asp:ImageButton CommandName="Edit"
   CommandArgument='<%# Eval("EditID") %>' runat="server" ID="lnkEdit" ImageUrl="../images/edit.gif" ToolTip="View/Edit"></asp:ImageButton></ItemTemplate></asp:TemplateField>

Hope this helps. Anindya

Anindya Sengupta