我有三个文本框和一个下拉菜单,如下图所示。现在在提交按钮上单击我必须通过应用 if-else 块来检查 16 个案例。以下是我当前使用的代码:
protected void btnsearch_Click(object sender, EventArgs e)
{
DateTime date = System.DateTime.Today;
string strdate = date.ToString("dd-MM-yy");
//Response.Write("class=" + drpclass.SelectedIndex + "tchrname=" + txt_tchrname.Text + "studentcode=" + txt_studentcode.Text + "date=" + strDTentered1);
if (drpclass.SelectedIndex != 0 && txt_tchrname.Text.Length == 0 && txt_studentcode.Text.Length == 0 && txtdatesearch.Text.Length == 0)
{
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strdate + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "'";
lbl_norecord.Visible = false;
goto RC_sql;
}
else if (drpclass.SelectedIndex == 0 && txt_tchrname.Text.Length != 0 && txt_studentcode.Text.Length == 0 && txtdatesearch.Text.Length == 0)
{
//lbl_norecord.Visible = false;
classnorecord.Visible = false;
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strdate + "' and tblhomework.TshortCode='" + txt_tchrname.Text.ToString() + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex == 0 && txt_tchrname.Text.Length == 0 && txt_studentcode.Text.Length != 0 && txtdatesearch.Text.Length == 0)
{
//lbl_norecord.Visible = false;
classnorecord.Visible = false;
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strdate + "' and tblhomework.StudentsCode like'" + studentcode + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex == 0 && txt_tchrname.Text.Length == 0 && txt_studentcode.Text.Length == 0 && txtdatesearch.Text.Length != 0)
{
//lbl_norecord.Visible = false;
classnorecord.Visible = false;
DateTime DTentered1 = DateTime.ParseExact(txtdatesearch.Text, CalendarExtender1.Format, null);
//DateTime = CalendarExtender1.SelectedDate.Value;
string strDTentered1 = DTentered1.ToString("dd-MM-yy");
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strDTentered1 + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex != 0 && txt_tchrname.Text.Length != 0 && txt_studentcode.Text.Length == 0 && txtdatesearch.Text.Length == 0)
{
//lbl_norecord.Visible = false;
classnorecord.Visible = false;
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strdate + "' and tblhomework.TshortCode='" + txt_tchrname.Text.ToString() + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex != 0 && txt_tchrname.Text.Length == 0 && txt_studentcode.Text.Length != 0 && txtdatesearch.Text.Length == 0)
{
// lbl_norecord.Visible = false;
classnorecord.Visible = false;
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strdate + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "' and tblhomework.StudentsCode like'" + studentcode + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex != 0 && txt_tchrname.Text.Length == 0 && txt_studentcode.Text.Length == 0 && txtdatesearch.Text.Length != 0)
{
//lbl_norecord.Visible = false;
classnorecord.Visible = false;
DateTime DTentered1 = DateTime.ParseExact(txtdatesearch.Text, CalendarExtender1.Format, null);
//DateTime = CalendarExtender1.SelectedDate.Value;
string strDTentered1 = DTentered1.ToString("dd-MM-yy");
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strDTentered1 + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex != 0 && txt_tchrname.Text.Length != 0 && txt_studentcode.Text.Length != 0 && txtdatesearch.Text.Length == 0)
{
//lbl_norecord.Visible = false;
classnorecord.Visible = false;
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strdate + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "' and tblhomework.TshortCode='" + txt_tchrname.Text.ToString() + "' and tblhomework.StudentsCode like'" + studentcode + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex != 0 && txt_tchrname.Text.Length != 0 && txt_studentcode.Text.Length == 0 && txtdatesearch.Text.Length != 0)
{
//lbl_norecord.Visible = false;
classnorecord.Visible = false;
DateTime DTentered1 = DateTime.ParseExact(txtdatesearch.Text, CalendarExtender1.Format, null);
//DateTime = CalendarExtender1.SelectedDate.Value;
string strDTentered1 = DTentered1.ToString("dd-MM-yy");
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strDTentered1 + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "' and tblhomework.TshortCode='" + txt_tchrname.Text.ToString() + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex == 0 && txt_tchrname.Text.Length != 0 && txt_studentcode.Text.Length != 0 && txtdatesearch.Text.Length != 0)
{
//lbl_norecord.Visible = false;
classnorecord.Visible = false;
DateTime DTentered1 = DateTime.ParseExact(txtdatesearch.Text, CalendarExtender1.Format, null);
//DateTime = CalendarExtender1.SelectedDate.Value;
string strDTentered1 = DTentered1.ToString("dd-MM-yy");
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strDTentered1 + "' and tblhomework.TshortCode='" + txt_tchrname.Text.ToString() + "' and tblhomework.StudentsCode like'" + studentcode + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex == 0 && txt_tchrname.Text.Length == 0 && txt_studentcode.Text.Length != 0 && txtdatesearch.Text.Length != 0)
{
// lbl_norecord.Visible = false;
classnorecord.Visible = false;
DateTime DTentered1 = DateTime.ParseExact(txtdatesearch.Text, CalendarExtender1.Format, null);
//DateTime = CalendarExtender1.SelectedDate.Value;
string strDTentered1 = DTentered1.ToString("dd-MM-yy");
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strDTentered1 + "' and tblhomework.StudentsCode like'" + studentcode + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex == 0 && txt_tchrname.Text.Length != 0 && txt_studentcode.Text.Length != 0 && txtdatesearch.Text.Length == 0)
{
// lbl_norecord.Visible = false;
classnorecord.Visible = false;
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strdate + "' and tblhomework.StudentsCode like'" + studentcode + "' and tblhomework.TshortCode='" + txt_tchrname.Text.ToString() + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex != 0 && txt_tchrname.Text.Length == 0 && txt_studentcode.Text.Length != 0 && txtdatesearch.Text.Length == 0)
{
// lbl_norecord.Visible = false;
classnorecord.Visible = false;
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strdate + "' and tblhomework.StudentsCode like'" + studentcode + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex == 0 && txt_tchrname.Text.Length != 0 && txt_studentcode.Text.Length == 0 && txtdatesearch.Text.Length != 0)
{
// lbl_norecord.Visible = false;
classnorecord.Visible = false;
DateTime DTentered1 = DateTime.ParseExact(txtdatesearch.Text, CalendarExtender1.Format, null);
//DateTime = CalendarExtender1.SelectedDate.Value;
string strDTentered1 = DTentered1.ToString("dd-MM-yy");
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strDTentered1 + "' and tblhomework.TshortCode='" + txt_tchrname.Text.ToString() + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex != 0 && txt_tchrname.Text.Length != 0 && txt_studentcode.Text.Length != 0 && txtdatesearch.Text.Length != 0)
{
//lbl_norecord.Visible = false;
classnorecord.Visible = false;
DateTime DTentered1 = DateTime.ParseExact(txtdatesearch.Text, CalendarExtender1.Format, null);
//DateTime = CalendarExtender1.SelectedDate.Value;
string strDTentered1 = DTentered1.ToString("dd-MM-yy");
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strDTentered1 + "' and tblhomework.TshortCode='" + txt_tchrname.Text.ToString() + "' and tblhomework.StudentsCode like'" + studentcode + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "'";
goto RC_sql;
}
else if (drpclass.SelectedIndex != 0 && txt_tchrname.Text.Length == 0 && txt_studentcode.Text.Length != 0 && txtdatesearch.Text.Length != 0)
{
// lbl_norecord.Visible = false;
classnorecord.Visible = false;
DateTime DTentered1 = DateTime.ParseExact(txtdatesearch.Text, CalendarExtender1.Format, null);
//DateTime = CalendarExtender1.SelectedDate.Value;
string strDTentered1 = DTentered1.ToString("dd-MM-yy");
string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strDTentered1 + "' and tblhomework.StudentsCode like'" + studentcode + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "'";
goto RC_sql;
}
//else if (drpclass.SelectedIndex == 0 && txt_tchrname.Text.Length == 0 && txt_studentcode.Text.Length == 0 && txtdatesearch.Text.Length == 0)
//{
// //DateTime DTentered1 = DateTime.ParseExact(txtdatesearch.Text, CalendarExtender1.Format, null);
// ////DateTime = CalendarExtender1.SelectedDate.Value;
// //string strDTentered1 = DTentered1.ToString("dd-MM-yy");
// //string studentcode = "%" + txt_studentcode.Text.ToString() + "%";
// //strwhere = " DATE_FORMAT(tblhomework.DateCreated,'%d-%m-%y')='" + strDTentered1 + "' and tblhomework.StudentsCode like'" + studentcode + "' and tblhomework.ClassCode='" + drpclass.SelectedItem.Value + "'";
// Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script language=JavaScript>alert('Please fill atleast one field ');</script>");
//}
else
//{
// lbl_norecord.Text = "Please fill atleast one Field";
// lbl_norecord.Visible = true;
{
classalert.Visible = true;
classnorecord.Visible = false;
classnorecordtoday.Visible = false;
grdhomework.Visible = false;
goto RC_EXIT;
}
RC_sql:
sqlsearch = "SELECT tblhomework.ID,tblteacher.TEACHERNAME,tblclass.CLASSNAME,tblhomework.Title,tblhomework.HomeworkDetail,tblhomework.StudentsCode FROM tblhomework" +
" join tblclass on tblclass.CLASSCODE=tblhomework.ClassCode join tblteacher on tblteacher.TSHORTNAME=tblhomework.Tshortcode where " + strwhere;
ds = obj.openDataset(sqlsearch, Session["SCHOOLCODE"].ToString());
if (ds.Tables[0].Rows.Count == 0)
{
//lbl_norecord.Text = "Record Not Found";
//lbl_norecord.Visible = true; grdhomework.Visible = false;
classnorecord.Visible = true;
classnorecordtoday.Visible = false;
classalert.Visible = false;
}
else
{
grdhomework.Visible = true;
grdhomework.DataSource = ds;
grdhomework.DataBind();
blankdata();
classnorecord.Visible = false;
classnorecordtoday.Visible = false;
classalert.Visible = false;
}
RC_EXIT:
Response.Write("");
}
现在我想要某种方式,我不必制作太多 if else 条件。因为 if-else 条件随着文本框数量的增加而增加。