忘记密码链接以发送随机号码

时间:2016-01-06 12:12:54

标签: c# asp.net webforms

我正在用C#开发一个简单的登录页面。我正在实现忘记密码链接。如果点击链接,我需要向相应的用户发送一封随机编号的电子邮件。以下是我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    string username = string.Empty;
    string password = string.Empty;
    string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT Name, [Password] FROM tbl_Detailstbl WHERE Email_Id = @Email"))
        {
           cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                if (sdr.Read())
                {
                    username= sdr["Name"].ToString();
                    password = sdr["Password"].ToString();
                }
            }
            con.Close();
        }
    }
    if (!string.IsNullOrEmpty(password))
    {
        MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text.Trim());
        mm.Subject = "Password Recovery";
        mm.Body = string.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", username, password);
        mm.IsBodyHtml = true;
       SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;

        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential("sender@gmail.com","asfsdfg");

        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        lblMessage.ForeColor = Color.Green;
        lblMessage.Text = "Password has been sent to your email address.";
    }
    else
    {
        lblMessage.ForeColor = Color.Red;
        lblMessage.Text = "This email address does not match our records.";
    }
}

Html代码:

<form id="form2" runat="server">
    <div>
        <fieldset style="width:380px;">
            <legend>Recover Password By Email Or User Name</legend>
            <table>
                <tr>
                    <td>UserName : </td>
                    <td>
                        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                <td colspan="2">&nbsp;&nbsp;OR</td>
                </tr>
                <tr>
                    <td>Email Id : </td>
                    <td>
                        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
                        <asp:RegularExpressionValidator ID="revEmailId" runat="server"
                            ErrorMessage="Please enter valid email address"
                            ControlToValidate="txtEmailId" Display="Dynamic"
                            ForeColor="Red" SetFocusOnError="True"
                            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Send"  onclick="Button1_Click"/>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="lblMessage" runat="server" Text="" OnClick="Button1_Click"></asp:Label>
                    </td>
                </tr>
            </table>
        </fieldset>

    </div>
</form>

我的问题是,当我尝试发送电子邮件时,我收到以下错误:

  

无法找到引用的控件ID'txtEmailId'   'revEmailId'的'ControlToValidate'属性。

3 个答案:

答案 0 :(得分:2)

您是否尝试过更改

ControlToValidate="txtEmailId" 

ControlToValidate="txtEmail"

正在向您建议错误消息?

答案 1 :(得分:0)

RegularExpressionValidator 上,属性 ControlToValidate 配置为验证名为 txtEmailId 的控件,但文本框名称为 txtEmail

更改

  

的ControlToValidate =&#34; txtEmailId&#34;

  

的ControlToValidate =&#34; txtEmail&#34;

答案 2 :(得分:0)

请更新您的“asp:RegularExpressionValidator”控件,如下所示:

<asp:RegularExpressionValidator ID="revEmailId" runat="server"
                                ErrorMessage="Please enter valid email address"
                                ControlToValidate="txtEmail" Display="Dynamic"
                                ForeColor="Red" SetFocusOnError="True"
                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>