简体   繁体   English

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

[英]My label message won't appear after filling out login form

The label lblMsg only show when all fields are successfully entered but refuses to show red error msg when the fields are empty and the user press signup button. label lblMsg 仅在成功输入所有字段时显示,但在字段为空并且用户按下注册按钮时拒绝显示红色错误消息。 its like its jumps over the "else" code and go directly to my bool method instead and show the Script fail message.就像它直接跳过“其他”代码和 go 到我的 bool 方法并显示脚本失败消息一样。

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;
    }
}

here is the.aspx code from when i insert the label to the site.这是我将 label 插入站点时的.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>

You can remove window.location='SignUp.aspx';您可以删除window.location='SignUp.aspx'; . . Because this code refresing your web site and else condition never will happen.因为此代码会刷新您的 web 站点, else情况将永远不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM