浏览器刷新问题 [英] Browser Refresh Issue

查看:87
本文介绍了浏览器刷新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在开发忘记密码页面。我的逻辑是,当用户输入他的注册电子邮件地址时,会向他注册的电子邮件地址发送一封电子邮件。我得到的问题是,每当我在输入电子邮件地址后刷新忘记密码网页时,都会向用户发送一封电子邮件。它应该在Buttonclick事件而不是页面刷新时发送。我的aspx页面和aspx.cs如下: -





Hello,
I am developing a Forget Password Page. My logic is that when user enters his registered email address, an email will be sent to his registered email address. The problem i am getting is that whenever i refresh forget password Webpage after entering a email address, an email is sent to the user. It should be sent on Buttonclick event instead of page refresh. My aspx page and aspx.cs are as follows:-


<%@ Page Title="Forget Password" Language="C#" MasterPageFile="~/include/master/admin.master"

  AutoEventWireup="true" CodeFile="forget_password.aspx.cs" Inherits="Admin_forget_password" %>
<pre lang="HTML">
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
  <div class="admin_login_box clearfix">
    <div class="login_area">
      <h2>
        Forget Password</h2>
      <asp:TextBox ID="txtEmail" CssClass="login_field" runat="server"></asp:TextBox>
      <div class="container mg1">
        <div class="flrght" style="padding-right: 204px">
          <asp:Button ID="btnSignIn" CssClass="sign_btn" runat="server" Text="Send"

            onclick="btnSignIn_Click" />
          <span>
            <asp:Button ID="btnCancel" CssClass="sign_btn" runat="server" Text="Back"

            onclick="btnCancel_Click" /></span>
        </div>
        <div>
          <asp:Label ID="lblMessage" runat="server" Text="Label" ForeColor="Red"></asp:Label></div>
      </div>
    </div>
  </div>
</asp:Content>









ASPX.CS





ASPX.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CloudPOS;

public partial class Admin_forget_password : System.Web.UI.Page
{
  private string m_sPageUrl = string.Empty;
  private int m_iUserId = 0;
  private string m_sUserName = string.Empty;
  private string m_sFirstName = string.Empty;

  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      m_sPageUrl = HttpContext.Current.Request.ServerVariables[StringConstantcl.HTTP_X_REWRITE_URL].ToString();
      ViewState["backurl"] = Applicationcl.GetWebsiteUrl() + Request.QueryString["return"];
      lblMessage.Visible = false;
      txtEmail.Text = "";
    }
  }
  protected void btnSignIn_Click(object sender, EventArgs e)
  {
    m_sPageUrl = HttpContext.Current.Request.ServerVariables[StringConstantcl.HTTP_X_REWRITE_URL].ToString();
    ViewState["m_sPageUrl"] = Request.QueryString["return"];
    int iCheckUserTypeId = 0;
    int iUserTypeId = 0;

    try
    {
      CommonUsercl oCommonUsercl = new CommonUsercl();
      m_sPageUrl = Stringcl.GetValue(ViewState["m_sPageUrl"]);
      if (m_sPageUrl.Contains("admin_login.aspx"))
      {
        iCheckUserTypeId = CommonUsercl.GetUserTypeId(CommonUsercl.eUserType.Client_Admin);
      }
      else if (m_sPageUrl.Contains("customer_login.aspx"))
      {
        iCheckUserTypeId = CommonUsercl.GetUserTypeId(CommonUsercl.eUserType.Client_Customer);
      }
      DataTable dtUserType = oCommonUsercl.GetUser(txtEmail.Text);
      if (dtUserType.Rows.Count <= 0)
      {
        lblMessage.Text = "User email Id does not exists!";
        lblMessage.Visible = true;
        return;
      }

      iUserTypeId = Numericcl.GetIntValue(dtUserType.Rows[0]["in_user_type_id"]);
      m_iUserId = Numericcl.GetIntValue(dtUserType.Rows[0]["in_user_id"]);
      m_sUserName = Stringcl.GetValue(dtUserType.Rows[0]["nvc_email"]);
      m_sFirstName = Stringcl.GetValue(dtUserType.Rows[0]["nvc_first_name"]);

      if (iUserTypeId != iCheckUserTypeId)
      {
        lblMessage.Text = "User email Id does not exists!";
        lblMessage.Visible = true;
        return;
      }
      else
      {
        string sName = m_sFirstName;
        string sEmail = txtEmail.Text;
        string sNewPassword = GeneratePassword(m_sUserName);
        oCommonUsercl.ChangePassword(m_iUserId, sNewPassword);
        string sEmailMessage = "<html><head></head><body>Hello " + sName + ", <br /> <br />  Your password has been reset.";
        sEmailMessage += "<br /><br />" + "New Password:-" + sNewPassword + "<br /><br />Regards<br />CloudPOS Team<br /></body></html>";
        string sSubject = "Message from CloudPOS Team - " + sName + "/" + sEmail;
        Emailcl oEmailcl = new Emailcl();
        oEmailcl.SendEmail(Emailcl.enmModule.Regular, sEmail, "", "Password Recovery", sEmailMessage, true);
        lblMessage.Text = "Your Password has been sent";
        lblMessage.Visible = true;
        txtEmail.Text = "";
      }
    }
    catch (Exception ex)
    {
      Errorcl.HandleException(ex, true);
    }
  }

  /*!  \function GeneratePassword
   *   \brief Used to generate new password.
   *   \details This function is used to Generate new password for user.
   */
  private string GeneratePassword(string sUserName)
  {
    string sStr = string.Empty;
    sStr = sUserName.Length <= 4 ? sUserName : sUserName.Substring(0, 4);
    Random oRandom = new Random();
    sStr = sStr + Stringcl.GetValue(oRandom.Next(1000, 9999));
    return sStr;
  }
  protected void btnCancel_Click(object sender, EventArgs e)
  {
    m_sPageUrl = HttpContext.Current.Request.ServerVariables[StringConstantcl.HTTP_X_REWRITE_URL].ToString();
    ViewState["m_sPageUrl"] = Request.QueryString["return"];
    ViewState["backurl"] = Applicationcl.GetWebsiteUrl() + Request.QueryString["return"];

    if (m_sPageUrl.Contains("admin_login.aspx"))
    {
      Response.Redirect("admin_login.aspx");
    }
    else if (m_sPageUrl.Contains("customer_login.aspx"))
    {
      Response.Redirect("/customer/customer_login.aspx");
    }
  }
}







请帮助。




Please help.

推荐答案

请在此处查看如何解决此问题: http://aspalliance.com/687_Preventing_Duplicate_Record_Insertion_on_Page_Refresh .4 [ ^ ]
See here how to solve this : http://aspalliance.com/687_Preventing_Duplicate_Record_Insertion_on_Page_Refresh.4[^]


原因是您刷新发送到服务器的最后信息。哪个是__doPostback中的按钮点击信息。这就是为什么你再次看到按钮触发的原因。
查看以下文章了解更多信息

检测页面刷新 [ ^ ]

在ASP.NET中检测刷新或回发 [ ^ ]

为什么在ASP.NET中刷新页面时会执行按钮点击事件? [ ^ ]
The reason for this is your refreshing the last information sent to the server. Which is the button click information in the __doPostback. This is why you are seeing the event of the button fire again.
Check below article for more information
Detecting Page Refresh[^]
Detecting Refresh or Postback in ASP.NET[^]
Why in ASP.NET is a button click event executes when page is refreshed?[^]


Hello Abhi,



以下提到的帖子/文章可以帮助您解决问题。

Hello Ab

The following mentioned posts/articles should help you resolve the issue.

  • Detecting Refresh or Postback in ASP.NET[^]
  • Detect Browser Refresh Avoid Events Fire In ASP.NET[^]


这篇关于浏览器刷新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆