C# 标签的清除值
按下按钮时,我会调用此代码:C# 标签的清除值,c#,asp.net,label,C#,Asp.net,Label,按下按钮时,我会调用此代码: protected void Button1_Click(object sender, EventArgs e) { if (DropDownList1.SelectedItem.ToString() =="ER00 - File Header") { using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSe
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)
{
}
}
}
}
}
这就是它的工作原理:用户输入一个ID,然后当按下按钮时,它将以SQL显示表
如果用户没有在文本框中输入任何内容,它会提示“请输入BatchID!”,但之后,它会留在文本框中,即使我已经输入了有效的ID,它也不清楚。知道原因吗?ASP.NET页面有一个名为ViewState的东西,可以在请求之间维护控件的状态。您需要清除GridView的数据源值并重新绑定它
if (String.IsNullOrEmpty(TextBox_ID.Text.ToString()))
{
lbl_NoBatchID.Text = "Please enter BatchID!";
GridView1.DataSource=null;
GridView1.DataBind();
}
else
{
如果查找成功,还需要清除错误:
catch(Exception e){
}
lbl_NoBatchID.Text = "";
我还应该注意,您的空
catch()
将吞掉您可能遇到的任何数据库或查找错误。在Else块中添加这行代码
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)
{
}
}
}
}
lbl_NoBatchID.Text = "";
您可以尝试设置lbl_NoBatchID.Text=”“。看看我的答案。