使用 ASP.NET 动态数据插入/编辑后返回上次查看的列表页面

发布于 2024-08-27 20:05:24 字数 312 浏览 9 评论 0原文

对于相当标准的动态数据站点,当用户编辑或插入新项目并保存时,页面会执行 Response.Redirect(table.ListActionPath),这会将用户带回表的第 1 页。

如果他们正在编辑第 10 页(或其他页面)上的某个项目并想要编辑该页面上的下一个项目,他们必须记住页码并导航回该页。

让用户返回到他们上次查看的列表页面的最佳方式是什么?

我可以设想一些使用 cookie、会话状态或查询字符串值来保留此状态并制作自己的页面模板来合并它的解决方案,但我不禁认为这一定是创建动态数据时考虑到的事情,我在这里缺少的框架中一定有一些更简单或内置的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温折酒 2024-09-03 20:05:24

这对我有用;

在 ~/DynamicData/PageTemplates/List.aspx 中连接 onpageindexchanged 事件的处理程序:

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

然后在处理程序代码中,将页面索引保存到会话中:

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

最后,在页面加载时,如果请求不是回发,并且不是asyc postback,如果referrer url是edit.aspx,并且会话中有页面索引,则设置gridview的页面索引:

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

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"];
                }
            }
        }
    }
疏忽 2024-09-03 20:05:24

如果除了列表页面之外没有进入编辑页面,那么您可以使用 UrlReferrer 获取上一页并将其保留在页面的加载中,以防不回发(因为加载后会触发按钮的事件)页面的)在视图状态或隐藏字段中,当按钮事件被触发时被调用:

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

或者您可以使用javascript,

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

否则我认为您应该将列表页面的页码发送到标题或查询字符串中的编辑页面因此,完成编辑后,您应该重定向到编辑项目的列表页面。

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.

笙痞 2024-09-03 20:05:24

使用以下代码修改 Insert.aspx.cs 文件,该代码使用其来源的整个当前 QueryString 重定向列表模式 url:

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

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);
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文