views:

162

answers:

4

With a pretty standard Dynamic Data site, when the user edits or inserts a new item and saves, the page does a Response.Redirect(table.ListActionPath), which takes the user back to page 1 of the table.

If they were editing an item on page 10 (or whatever) and want to edit the next item on that page, they have to remember the page number and navigate back to it.

What's the best way to return the user to the list page they last viewed?

I can conceive of some solutions using cookies, session state, or query string values to retain this state and making my own Page Template to incorporate it, but I can't help thinking this must be something that was considered when Dynamic Data was created, and there must be something simpler or built-in to the framework that I'm missing here.

A: 

If the page of editing is not enetered except from the listing page then you can use the UrlReferrer to get the previous page and keep it in the load of the page in case of not postingback (because the event of the button is fired after the load of the page) in a viewstate or in hiddenfield to be called when the button event is fired :

protected void Page_Load(object sender, EventArgs e)
{
     if( !IsPostBack )
     {
         Viewstate["PrevPage"] = Request.UrlReferrer.ToString();
         // OR
         _hiddenField.value = Request.UrlReferrer.ToString();
     }
}

Or you can use javascript

<input type="button" value="Back to Previous Page" onClick="javascript: history.go(-1)">

else i think that you should send the page number of the listing page to the editing page in the header or in a querystring so after finishing editting you should redirect to the listing page at the item edited.

Ahmy
That is the type of thing I was thinking of implementing myself. I was hoping this was a common pattern or problem (returning to where you left off) that would be handled in some more elegant or out-of-the-box fashion with Dynamic Data. I am growing more disillusioned with Dynamic Data. It is starting to seem like a pretty shallow framework.
Pat James
I don't think so in .Net forms but may be in another technology i am not sure.
Ahmy
I decided to abandon Dynamic Data and use MVC 2 instead. With the new auto-generated create, list, edit views it was much easier than dealing Dynamic Data's limitations. Thanks for the suggestion Ahmy but since it isn't really a built-in Dynamic Data solution, I'm not inclined to "accept" it. Maybe this is a question where there is no solution.
Pat James
I were thinking in MVC but i hate any tool that render unused DLL that slow down from the performance unlike C++ where performance is big factor
Ahmy
More power to you for building web apps on C++ ...but that's not my thing
Pat James
+1  A: 

This is what worked for me;

In ~/DynamicData/PageTemplates/List.aspx wire-up a handler for the onpageindexchanged event:

<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource"
AllowPaging="True" AllowSorting="True" CssClass="gridview" 
onpageindexchanged="GridView1_PageIndexChanged">

Then in the hander code, save the page index to the session:

protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
    Session.Add("PrevPageIndex", GridView1.PageIndex);
}

Finally, on page load, if the request is not postback, and is not asyc postback, and if the referrer url is edit.aspx, and if there is a page index in the session, set the page index of the gridview:

if (!IsPostBack && !IsAsync)
    {
        if (Request.UrlReferrer != null)
        {                
            if (Request.UrlReferrer.ToString().Contains("Edit.aspx"))
            {
                if (Session["PrevPageIndex"] != null)
                {
                    GridView1.PageIndex = (int)Session["PrevPageIndex"];
                }
            }
        }
    }
Rafe
A: 

Thank You. For Edit this code is good. What about Insert?. How to go to last page after Insert?

Kumar
A: 

Modify the Insert.aspx.cs file with the following code, that redirects list mode url with the whole current QueryString that it came from:

   protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.Exception == null || e.ExceptionHandled)
            {
                var qs = Request.QueryString.ToString();
                if (!string.IsNullOrEmpty(qs))
                    Response.Redirect(Table.ListActionPath + "?" + qs);
                Response.Redirect(Table.ListActionPath);
            }
        }
Paulo