C# 标签的清除值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15423495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:46:49  来源:igfitidea点击:

Clear value of label

c#asp.netlabel

提问by user1954418

I have this code called when a button is pressed:

我在按下按钮时调用了此代码:

protected void Button1_Click(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.ToString() =="ER00 - File Header")
    {
        using (SqlConnection con = 
                   new SqlConnection(ConfigurationSettings.AppSettings["DBcon"]))
        {
            if (String.IsNullOrEmpty(TextBox_ID.Text.ToString()))
            {
                lbl_NoBatchID.Text = "Please enter BatchID!";
            }
            else
            {
                try
                {
                    SqlCommand sqlCommand = new SqlCommand(
                        "Select * from tbl_WinApps_FileHeader Where BatchID =" +
                        TextBox_ID.Text.ToString());

                    sqlCommand.Connection = con;
                    con.Open();
                    SqlDataReader read = sqlCommand.ExecuteReader();

                    GridView1.DataSource = read;
                    GridView1.DataBind();
                }
                catch (Exception)
                {                           
                }
            }
        }
    }
}

This is how it works: The user inputs a ID, then when the button is pressed, It will display the table in SQL.

它是这样工作的:用户输入一个 ID,然后当按钮被按下时,它会以 SQL 显示表格。

If the user did not input anything on the textbox, it prompts "Please enter BatchID!" but after that, it stays in there and it doesn't clear even if I already entered a valid ID. Any idea why that is?

如果用户未在文本框中输入任何内容,则会提示“请输入 BatchID!” 但在那之后,它会留在那里,即使我已经输入了有效的 ID,它也不会清除。知道这是为什么吗?

回答by Eric Falsken

ASP.NET pages have something called ViewState that can maintain the state of the controls between requests. You need to clear the DataSource value of the GridView and rebind it.

ASP.NET 页面有一种叫做 ViewState 的东西,它可以在请求之间维护控件的状态。您需要清除 GridView 的 DataSource 值并重新绑定它。

           if (String.IsNullOrEmpty(TextBox_ID.Text.ToString()))
            {
                lbl_NoBatchID.Text = "Please enter BatchID!";
                GridView1.DataSource=null;
                GridView1.DataBind();
            }
            else
            {

You also want to clear the error if the lookup was successful:

如果查找成功,您还想清除错误:

catch(Exception e){
}
lbl_NoBatchID.Text = "";

I should also note that your empty catch()will swallow any database or lookup errors you might get.

我还应该注意,您的空catch()将吞下您可能遇到的任何数据库或查找错误。

回答by NET Experts

   protected void Button1_Click(object sender, EventArgs e)
{
   if (DropDownList1.SelectedItem.ToString() =="ER00 - File Header")
    {

        using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["DBcon"]))
        {
            if (String.IsNullOrEmpty(TextBox_ID.Text.ToString()))
            {
                lbl_NoBatchID.Text = "Please enter BatchID!";

            }
            else
            {
                try
                {

                    SqlCommand sqlCommand = new SqlCommand("Select * from tbl_WinApps_FileHeader Where BatchID =" + TextBox_ID.Text.ToString());
                    sqlCommand.Connection = con;
                    con.Open();
                    SqlDataReader read = sqlCommand.ExecuteReader();

                        GridView1.DataSource = read;
                        GridView1.DataBind();
                    lbl_NoBatchID.Text = "";
                }
                catch (Exception)
                {                           

                }

            }
        }

    }

回答by Ankur Singhal

In your Else block add this line of code.

在您的 Else 块中添加这行代码。

lbl_NoBatchID.Text = "";