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

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

(c# - My label message won't appear after filling out login form)

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

标签lblMsg仅在成功输入所有字段时显示,但在字段为空并且用户按下注册按钮时拒绝显示红色错误消息。就像跳过“ else”代码一样,直接转到我的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;
    }
}

这是当我将标签插入网站时的.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>
Questioner
Carl Ahren
Viewed
0
Göksel ÖZER 2020-11-28 21:23:36

你可以删除window.location='SignUp.aspx';因为此代码引用了你的网站和else条件,所以永远不会发生。