Asp.net 捕获错误后如何清除标签?

Asp.net 捕获错误后如何清除标签?,asp.net,c#-4.0,Asp.net,C# 4.0,我有一个在线表单,其中有许多文本框,包括电子邮件ID、密码和其他详细信息。当用户没有输入正确的信息时。我正在显示一条错误消息,如下代码所示。我使用的是来自SQL的返回值。我试图清除每个按钮点击的标签。有人能帮我吗 protected void Button1_Click(object sender, EventArgs e) { { { SqlConnection sqlCon = new SqlConnectio

我有一个在线表单,其中有许多文本框,包括电子邮件ID、密码和其他详细信息。当用户没有输入正确的信息时。我正在显示一条错误消息,如下代码所示。我使用的是来自SQL的返回值。我试图清除每个按钮点击的标签。有人能帮我吗

protected void Button1_Click(object sender, EventArgs e)
    {
        {

            {
                SqlConnection sqlCon = new SqlConnection(strCon);
                SqlCommand cmd = new SqlCommand("UpdateRequestAccess_Test", sqlCon);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "UpdateRequestAccess_Test";
                cmd.Connection = sqlCon;

                ----------Parameters are declared here--------

                SqlParameter rpv = new SqlParameter();
                rpv.DbType = DbType.Int32;
                rpv.Direction = ParameterDirection.ReturnValue;

                cmd.Parameters.Add(rpv);
                try
                {
                    sqlCon.Open();
                    cmd.ExecuteScalar();

                    int retValue = Convert.ToInt32(rpv.Value);


                        if (retValue == 10)
                            lblMessage.Text = "Request was sent successfully!";

                        if (retValue == 11)
                            Label2.Text = "*Email Address is already registered.";

                        if (retValue == 12)
                            Label3.Text = "*Passwords do not match.";

                        if (retValue == 13)
                            Label4.Text = "Sorry, Your application was already denied earlier.";

                        if (retValue == 14)
                            Label5.Text = "*Please select an option 'Yes' or 'No' under Select Online Tools.";

                        if (retValue == 15)
                            Label6.Text = "*Please enter the information in the text boxes above.";

                        if (retValue == 15)
                            Label7.Text = "*Please Select an option from the dropdown above.";

                }

                catch (Exception ex)
                {

                        lblMessage.Text = ex.Message;

                        Label2.Text = ex.Message;

                        Label3.Text = ex.Message;

                        Label4.Text = ex.Message;

                        Label5.Text = ex.Message;

                        Label6.Text = ex.Message;

                        Label7.Text = ex.Message;

                }

            }
        }

    }
试试

lblMessage.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Label6.Text = "";
Label7.Text = "";
试试

lblMessage.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Label6.Text = "";
Label7.Text = "";
尝试将这些行添加到代码的开头,就在try-catch语句的
try
上方

或者你也可以

Label7.Clear(); 
虽然我对语法不太确定,但我是一名vb.net程序员:)

尝试将这些行添加到代码的开头,就在try-catch语句的
try
上方

或者你也可以

Label7.Clear(); 
虽然不确定语法,但我是一名vb.net程序员:)

put ClearTextMsg();进入页面加载事件

如果在页面加载中使用If(!IsPostBack){}代码块,则将其放在页面加载的一边

完美位置是页面加载事件的第一行。像下面

protected void Page_Load(object sender, EventArgs e)
{
     ClearTextMsg();
     if (!IsPostBack)
     {
        // any valid c# statments
     }
}

// call this function
private void ClearTextMsg()
{
lblMessage.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Label6.Text = "";
Label7.Text = "";
}
放置ClearTextMsg();进入页面加载事件

如果在页面加载中使用If(!IsPostBack){}代码块,则将其放在页面加载的一边

完美位置是页面加载事件的第一行。像下面

protected void Page_Load(object sender, EventArgs e)
{
     ClearTextMsg();
     if (!IsPostBack)
     {
        // any valid c# statments
     }
}

// call this function
private void ClearTextMsg()
{
lblMessage.Text = "";
Label2.Text = "";
Label3.Text = "";
Label4.Text = "";
Label5.Text = "";
Label6.Text = "";
Label7.Text = "";
}

更新按钮点击事件,如下所示

protected void Button1_Click(object sender, EventArgs e)
{
                    lblMessage.Text = string.Empty;

                    Label2.Text = string.Empty;

                    Label3.Text = string.Empty;

                    Label4.Text = string.Empty;

                    Label5.Text = string.Empty;

                    Label6.Text = string.Empty;

                    Label7.Text = string.Empty;


    {

        {
            SqlConnection sqlCon = new SqlConnection(strCon);
            SqlCommand cmd = new SqlCommand("UpdateRequestAccess_Test", sqlCon);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "UpdateRequestAccess_Test";
            cmd.Connection = sqlCon;

            ----------Parameters are declared here--------

            SqlParameter rpv = new SqlParameter();
            rpv.DbType = DbType.Int32;
            rpv.Direction = ParameterDirection.ReturnValue;

            cmd.Parameters.Add(rpv);
            try
            {
                sqlCon.Open();
                cmd.ExecuteScalar();

                int retValue = Convert.ToInt32(rpv.Value);


                    if (retValue == 10)
                        lblMessage.Text = "Request was sent successfully!";

                    if (retValue == 11)
                        Label2.Text = "*Email Address is already registered.";

                    if (retValue == 12)
                        Label3.Text = "*Passwords do not match.";

                    if (retValue == 13)
                        Label4.Text = "Sorry, Your application was already denied earlier.";

                    if (retValue == 14)
                        Label5.Text = "*Please select an option 'Yes' or 'No' under Select Online Tools.";

                    if (retValue == 15)
                        Label6.Text = "*Please enter the information in the text boxes above.";

                    if (retValue == 15)
                        Label7.Text = "*Please Select an option from the dropdown above.";

            }

            catch (Exception ex)
            {

                    lblMessage.Text = ex.Message;

                    Label2.Text = ex.Message;

                    Label3.Text = ex.Message;

                    Label4.Text = ex.Message;

                    Label5.Text = ex.Message;

                    Label6.Text = ex.Message;

                    Label7.Text = ex.Message;

            }

        }
    }

}

更新按钮点击事件,如下所示

protected void Button1_Click(object sender, EventArgs e)
{
                    lblMessage.Text = string.Empty;

                    Label2.Text = string.Empty;

                    Label3.Text = string.Empty;

                    Label4.Text = string.Empty;

                    Label5.Text = string.Empty;

                    Label6.Text = string.Empty;

                    Label7.Text = string.Empty;


    {

        {
            SqlConnection sqlCon = new SqlConnection(strCon);
            SqlCommand cmd = new SqlCommand("UpdateRequestAccess_Test", sqlCon);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "UpdateRequestAccess_Test";
            cmd.Connection = sqlCon;

            ----------Parameters are declared here--------

            SqlParameter rpv = new SqlParameter();
            rpv.DbType = DbType.Int32;
            rpv.Direction = ParameterDirection.ReturnValue;

            cmd.Parameters.Add(rpv);
            try
            {
                sqlCon.Open();
                cmd.ExecuteScalar();

                int retValue = Convert.ToInt32(rpv.Value);


                    if (retValue == 10)
                        lblMessage.Text = "Request was sent successfully!";

                    if (retValue == 11)
                        Label2.Text = "*Email Address is already registered.";

                    if (retValue == 12)
                        Label3.Text = "*Passwords do not match.";

                    if (retValue == 13)
                        Label4.Text = "Sorry, Your application was already denied earlier.";

                    if (retValue == 14)
                        Label5.Text = "*Please select an option 'Yes' or 'No' under Select Online Tools.";

                    if (retValue == 15)
                        Label6.Text = "*Please enter the information in the text boxes above.";

                    if (retValue == 15)
                        Label7.Text = "*Please Select an option from the dropdown above.";

            }

            catch (Exception ex)
            {

                    lblMessage.Text = ex.Message;

                    Label2.Text = ex.Message;

                    Label3.Text = ex.Message;

                    Label4.Text = ex.Message;

                    Label5.Text = ex.Message;

                    Label6.Text = ex.Message;

                    Label7.Text = ex.Message;

            }

        }
    }

}

Label.Text=”“?我不太确定你有什么问题。一旦我点击按钮,如果有任何错误,它会显示消息。那很好。在我进行更改并再次单击按钮后,上一条错误消息仍将存在,因为它未从catch中清除。
Label.Text=“”?我不太确定你有什么问题。一旦我点击按钮,如果有任何错误,它会显示消息。那很好。在我进行更改并再次单击按钮后,上一条错误消息仍将存在,因为它未从catch中清除。我将尝试它并让您知道。我将尝试它并让您知道。