Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2020-11-28 12:39:33

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. its like its jumps over the "else" code and go directly to my bool method instead and show the Script fail message.

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.

<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>
Questioner
Carl Ahren
Viewed
11
Göksel ÖZER 2020-11-28 21:23:36

You can remove window.location='SignUp.aspx';. Because this code refresing your web site and else condition never will happen.