我正在用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"> 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> </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'属性。
答案 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>