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. 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>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x