填写登录表单后,我的标签消息不会出现



标签lblMsg仅在成功输入所有字段时显示,但在字段为空且用户按下注册按钮时拒绝显示红色错误消息。就像它跳过";否则";代码,然后直接转到我的bool方法,并显示Script失败消息。

public partial class SignUp : System.Web.UI.Page
{
string connectionString = @"Data Source=.;Initial Catalog=MyEShoppingDB;Integrated Security=True";

protected void txtSignup_Click(object sender, EventArgs e)
{
if (isformvalid()) 
{ 
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("UserAdd", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Password", txtPassw.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Name", txtFullName.Text.Trim());
sqlCmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
sqlCmd.ExecuteNonQuery();

Clear();
sqlCon.Close();

lblMsg.Visible = true;
lblMsg.Text = "Registration Successfully done";
lblMsg.ForeColor = System.Drawing.Color.Green;

}
}
else 
{

lblMsg.Visible = true;
lblMsg.Text = "All fields are mandatory";
lblMsg.ForeColor = System.Drawing.Color.Red;
}
}        
private bool isformvalid()
{
if (txtUserName.Text == "")
{
Response.Write("<script  >alert('Username not valid!');window.location='SignUp.aspx';</script>");
txtUserName.Focus();
return false;
}
else if (txtPassw.Text == "")
{
Response.Write("<script  >alert('Password not valid!');window.location='SignUp.aspx';</script>");
txtPassw.Focus();
return false;
}
else if (txtPassw.Text != txtCPassw.Text)
{
Response.Write("<script  >alert('Confirmed password not valid!');window.location='SignUp.aspx';</script>");
txtPassw.Focus();
return false;
}
// bygg annat exception för denna 
else if (txtEmail.Text == "")
{
Response.Write("<script  >alert('Email not valid!');window.location='SignUp.aspx';</script>");
txtEmail.Focus();
return false;
}
else if (txtFullName.Text == "")
{
Response.Write("<script  >alert('Name is not valid!');window.location='SignUp.aspx';</script>");
txtFullName.Focus();
return false;
}
return true; 
}
private void Clear()
{
txtUserName.Text = string.Empty;
txtPassw.Text = string.Empty;
txtCPassw.Text = string.Empty;
txtEmail.Text = string.Empty;
txtFullName.Text = string.Empty;
}
}

这是我将标签插入网站时的.aspx代码。

<div class="col-xs-11">
<asp:Button ID="txtSignup" class="btn btn-success" runat="server" Text="Sign Up" OnClick="txtSignup_Click" />
</div>
<asp:Label ID="lblMsg" runat="server" Text="Label" Visible="False"></asp:Label>

您可以删除window.location='SignUp.aspx';。因为这段代码重新构建了你的网站,else条件永远不会发生。

最新更新