Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:-


XML
<%@ 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
C#
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.
Posted
Updated 12-Jun-14 23:49pm
v3

 
Share this answer
 
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?[^]
 
Share this answer
 
v2
Comments
Sampath Lokuge 13-Jun-14 6:15am    
+ 5 :)
Hello Abhi,

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


Forgot to mention about (PRG), a common solution to this is called Post Redirect Get (PRG), where the browser is immediately redirected to a HTTP Get page after any post. See Post Redirect Get in asp.net for a web forms implementation.

Regards,
 
Share this answer
 
v2
Comments
Sampath Lokuge 13-Jun-14 6:16am    
+5 :)
Thanks guys. The problem is solved. Thanks everyone... :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900