【问题标题】:filter using checkboxlist使用复选框列表过滤
【发布时间】:2015-05-13 04:55:42
【问题描述】:

我想使用两个 CheckBoxList 过滤数据,结果必须显示在一个 GridView 中。我的数据存储在 SQL Server 数据库中。 我已经完成了一些代码,但它不起作用,当我检查一些框时没有任何反应。 这是我的代码

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindList();
        }
    }
    protected void gvDemo_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       gvDemo.PageIndex = e.NewPageIndex;
       BindList();
   }

   private void BindList()
   {
       string conString = ConfigurationManager.ConnectionStrings["YMMDMSConnectionString"].ConnectionString;
       string query = "SELECT ID,Client,Projet,Date,Shift,CodDefaut FROM cascad";
       string condition = string.Empty;
       foreach (ListItem item in shift.Items)
       {
           condition += item.Selected ? string.Format("'{0}',", item.Value) : "";
       }
       if (!string.IsNullOrEmpty(condition))
       {
           condition = string.Format(" where Shift in ({0})", condition.Substring(0, condition.Length - 1));
       }

       string condition2 = string.Empty;
       foreach (ListItem item in client.Items)
       {
           condition2 += item.Selected ? string.Format("'{0}',", item.Value) : "";
       }
       if (!string.IsNullOrEmpty(condition2))
       {
           condition2 = string.Format(" AND Client = {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);
                   gvDemo.DataSource = ds;
                   gvDemo.DataBind();
               }
           }
       }
   }

   protected void shift_SelectedIndexChanged(object sender, EventArgs e)
   {
       BindList();

   }

   protected void client_SelectedIndexChanged(object sender, EventArgs e)
   {

       foreach (ListItem item in shift.Items)
       {
           if (item.Selected == true)
           {
               BindList();
           }
       } 
   }

}

我错过了什么?

【问题讨论】:

  • 当你调试它时——因为我假设你当然已经调试过了——你发送到服务器的完整 SQL 查询是什么?
  • 为什么循环调用BindList?您只需调用一次(如果需要)。
  • 我有两个 ChechBoxList : shift & client 完整的查询是 : query + condition + condition2 它取决于我检查了哪些框
  • 您添加了硬编码脚本" AND Client = {0}",如果您不选择第一个复选框并选择第二个复选框,则会导致问题。我建议您在 SQL 中创建一个存储过程并接受两个可为空的参数并在 SQL 查询中处理 NULL 情况。
  • 即使我一开始选择第二个复选框列表也不会导致任何事情

标签: c# sql asp.net filtering checkboxlist


【解决方案1】:

don't really need to dispose a DataSet or DataTable(即使它实现了IDisposable,它也只是为了设计师的东西)。所以using 是多余的。如果您将已处置的DataSet 用作DataSource,这也可能会导致问题。

就这样:

DataSet ds = new DataSet())
sda.Fill(ds);
gvDemo.DataSource = ds;
gvDemo.DataBind();

除此之外,不清楚创建的sql查询是否正确。您是否验证它在 Sql-Server Management-Studio 中有效?

对了,你为什么要循环调用BindList?您只需要调用一次(如果需要)。所以这可能更合适:

bool needToDatabind = false;
foreach (ListItem item in shift.Items)
{
   if (item.Selected == true)
   {
       needToDatabind = true;
       break;
   }
} 
if(needToDatabind) BindList();

或更简洁:

var selectedItems = shift.Items.Cast<ListItem>().Where(i => i.Selected);
if(selectedItems.Any()) BindList();

【讨论】:

  • 谢谢蒂姆,但我不认为问题出在循环中我已经尝试过你的代码,但它不起作用。对于查询,我不知道如何在 Sql Server Management Studio 中验证它
  • @Lamyae:在sda.SelectCommand = cmd; 设置断点并获取您在cmd.CommandText 中找到的sql 查询。什么意思它不起作用,你得到一个错误?
  • 这个问题它没有给我任何错误,所以我无法确定它来自哪里
【解决方案2】:

确保您已将AutoPostBack="true" 添加到您的复选框中。

要添加断点,只需单击添加断点,如图所示,然后按 F5 进行调试。

希望得到帮助?

【讨论】:

  • 我必须为每个 CheckBoxList 添加 AutoPostBack="true" 吗??
  • 您在使用任何更新面板吗?你试过调试你的代码吗?
  • 我不使用更新面板,当然我调试了代码,但它不起作用并且没有给我任何错误
  • sda.SelectCommand = cmd; 设置断点并尝试查看/检查那里的sql 查询。
  • 我不知道请多解释一下
猜你喜欢
  • 2012-06-12
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 2014-03-08
  • 2015-06-14
相关资源
最近更新 更多