当我单击checkboxlist2项而不选择checkboxlist1项时,按钮搜索会出错。我认为checkboxlist2编码错误,请遵循以下代码:
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
this.BindGrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
string query = "SELECT * FROM Table_RegistrationInfo WHERE true";
string condition = string.Empty;
foreach (ListItem item in CheckBoxList1.Items)
{
condition += item.Selected ? string.Format("'{0}',", item.Value) : "";
}
if (!string.IsNullOrEmpty(condition))
{
condition = string.Format(" AND Nationality in ({0})", condition.Substring(0, condition.Length - 1));
}
string condition2 = string.Empty;
foreach (ListItem item in CheckBoxList2.Items)
{
condition2 += item.Selected ? string.Format("'{0}',", item.Value) : "";
}
if (!string.IsNullOrEmpty(condition2))
{
//condition = string.Format(" where Nationality in ({0})", condition.Substring(0, condition.Length - 1));
condition2 = string.Format(" AND GivenName in ({0})", condition2.Substring(0, condition2.Length - 1));
}
SqlCommand cmd = new SqlCommand(query + condition + condition2); using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con; sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
}
}
}
}
protected void Select_Nationality(object sender, EventArgs e)
{
this.BindGrid();
}
protected void Select_GivenName(object sender, EventArgs e)
{
foreach(ListItem item in CheckBoxList1.Items)
{
if(item.Selected == true)
{
this.BindGrid();
}
}
}
我必须实现8个复选框列表以过滤到gridview。如果要实现任何其他简单的技术,我必须使用从3个表中过滤。
答案 0 :(得分:2)
如果condition
为空或空,则查询变为:
SELECT ... FROM ... AND ...
这是不正确的。
你可以通过制作你的" base"来解决它。查询如下:
SELECT ... FROM ... WHERE 1=1
然后将AND ...
之类的内容添加到其中。然后您的查询变为(只有condition
有效时:
SELECT ... FROM ... WHERE 1=1 AND condition
相当于
SELECT ... FROM ... WHERE condition
在您的情况下,请更改此内容:
string query = "SELECT * FROM Table_RegistrationInfo";
到
string query = "SELECT * FROM Table_RegistrationInfo WHERE 1=1 ";
并改变这一点:
condition = string.Format(" where Nationality in ({0})", condition.Substring(0, condition.Length - 1));
进入
condition = string.Format(" AND Nationality in ({0})", condition.Substring(0, condition.Length - 1));
答案 1 :(得分:0)
修改你的代码。
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
string query = "SELECT * FROM Table_RegistrationInfo";
string condition = string.Empty;
foreach(ListItem item in CheckBoxList1.Items)
{
condition += item.Selected ? string.Format("'{0}',", item.Value) : "";
}
if(!string.IsNullOrEmpty(condition))
{
condition = string.Format(" where Nationality in ({0})", condition.Substring(0, condition.Length - 1));
}
string condition2 = string.Empty;
foreach(ListItem item in CheckBoxList2.Items)
{
condition2 += item.Selected ? string.Format("'{0}',", item.Value) : "";
}
if(!string.IsNullOrEmpty(condition2))
{
if(!string.IsNullOrEmpty(condition))
{
condition+=" And ";
}
else {
Condition +=" Where "
}
//condition = string.Format(" where Nationality in ({0})", condition.Substring(0, condition.Length - 1));
condition2 = string.Format(" GivenName in ({0})", condition2.Substring(0, condition2.Length - 1));
}
SqlCommand cmd = new SqlCommand(query + condition + condition2);
using(SqlConnection con = new SqlConnection(conString))
{
using(SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using(DataSet ds = new DataSet())
{
sda.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
}
}
}
}