程序go 2016-04-25 08:22 采纳率: 100%
浏览 40

ASP.NET防止浏览器“退回”

I'm trying to find a solution to a problem I'm facing with an ASP.NET application (C# / MySQL Backend).

The application uses webforms in a multi step wizard to insert data into a SQL database, the various steps are javascript driven so no 'submit' is done until the 'last' step. In certain circumstances anyway if the user click the browser 'back' button just after the 'last' step the form is resubmitted and the SQL INSERT is triggered again creating dupes of the original record.

I tought to use an Ajax based mechanism to track the steps of the form until the wizard reach the 'last' step. After that trough the same Ajax function I will prevent the resubmit of the form preventing dupes.

I don't want to use javascript tricks to disable the back button for obvious reasons.

The main problem of this solution is that I don't like it so much and it will also be quite difficult to implement since wizards on the application form are quite articulated and this may cause some problem.

Is there a 'best practice' to prevent such issues without building complex infrastructures?

I'm not posting code because I think it will be misleading and unnecessary.

Best regards, Mike

  • 写回答

1条回答 默认 最新

  • weixin_33712881 2016-05-06 15:43
    关注

    I found a solution to my problem. I think this can be a reasonably good practice for people facing the same problem. Of course it's not perfect but maybe someone else can refine this as the 'perfect solution'.

    First of all in the .aspx code of the page launching the form that was duplicating records I placed this:

    <asp:LinkButton
        ID="_btnForm1"
        runat="server"
        OnCommand="Link_Command"
        CommandArgument='<%#Eval("id","~/Form1.aspx?id={0}&step=Exit")%>'>
        Form1
    </asp:LinkButton>
    

    In the backend code I created an hanlder for the Link_Button command:

    protected void Link_Command(object sender, CommandEventArgs e)
    {
        Session["key"] = Guid.NewGuid().ToString().Replace("-", string.Empty);
    
        Response.Redirect(e.CommandArgument.ToString());
    }
    

    In short it generates an unique hash code storing it as a Session value.

    In the form managing records I checked for the Session["key"] value to be present and not null. If the key is present the code hasn't reached it's critical part then the forms should be resubmitted without problems.

    When the code reaches the critical part (just before data insertion into database) I cleared up the Session["key"] value:

    Session.Remove("key");
    

    Then if the user tries to resubmit the form after the critical part the Session key isn't present anymore and the code could trap the 'anomaly' and show up an error message or redirect over a different page:

    if (Session["key"] == null)
    {
          Response.Redirect("Error.aspx?script=" + HttpUtility.UrlEncode(Request.Path));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测