从第一个或第二个文本框中搜索一个按钮?

时间:2015-04-11 21:42:28

标签: c# sql

以下是搜索名称或代码的代码。我想知道如何使用一个按钮来搜索名称或代码。所以我的意思是如果我将名称留空并搜索代码,代码应该出现。或者,如果我搜索名称并将代码文本框留空,则应显示名称。

  Int32 Record;           
  Record = DisplayName(txtName.Text);      
  lblResult.Text = Record + " records found";
  txtName.Text = "";


  Int32 Record;
  Record = DisplayCode(txtCode.Text);
  lblResult.Text = Record + " records found";
  txtCode.Text = "";

2 个答案:

答案 0 :(得分:1)

Int32 Record; 

Record = String.IsNullOrEmpty(txtCode.Text) ? DisplayName(txtName.Text) :   DisplayCode(txtCode.Text)      
lblResult.Text = Record + " records found";

答案 1 :(得分:0)

尝试:

Int32 Record;  
if(txtName.Text=="")
{
  if(txtCode.Text!="")
  {
  Record = DisplayCode(txtCode.Text);
  lblResult.Text = Record + " records found";
  txtCode.Text = "";
  }
  else
  {
   lblResult.Text = "enter name/code to search ";
  }
}
else
{         
  Record = DisplayName(txtName.Text);      
  lblResult.Text = Record + " records found";
  txtName.Text = "";
}