使用C#和asp.net进行过滤 [英] Filter using C# and asp.net

查看:62
本文介绍了使用C#和asp.net进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在有多个复选框列表的地方进行编码,当我从单个或多个复选框列表中选择特定值/值时,应该在我的DATALIST中显示所选值/值的相应数据。

类似于在线购物网站上进行的过滤。



输出:

只有一个复选框选中数据显示..多选不工作。



谢谢。





代码:



how to code where there are multiple checkboxlists, when I select a particular value/values from a single or multiple checkboxlists, then the corresponding data to the selected value/values should be displayed in my DATALIST.
Its similar to the filteration done on Online Shopping sites.

Output:
only one checkbox selected data is display..multiple selection is not working.

THANK YOU.


CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

namespace Filtering
{
public partial class DATALIST : System.Web.UI.Page
{


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString14"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
       if (!IsPostBack)
       {
        //CheckBoxList1.SelectedIndex = 1;
       }
}

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
          string sCheckedValue = "";

          foreach (ListItem oItem in CheckBoxList1.Items)
          {
                if (oItem.Selected)
                {
                    if (sCheckedValue == "")
                               sCheckedValue = "Selected Value : " + 
                                               oItem.Value +  
                                              " Selected Text: " + 
                                               oItem.Text;

                                // Response.Write(sCheckedValue);
                               try
                               {
                                         SqlCommand cm = new SqlCommand("", con);

                                         string param = oItem.Value;
                                         Response.Write(param + "
                                         ");
                                        cm.CommandText = "SELECT * FROM [VENUE] WHERE [location] IN (@param)";
                                         //Response.Write(cm.CommandText.ToString());
                                        cm.Parameters.Add("@param", SqlDbType.VarChar).Value = param;

                                        cm.Connection.Open();
                                        /*foreach (SqlParameter a in cm.Parameters)
                                        {
                                        Response.Write(a.ToString());
                                        }*/

                                        DataList1.DataSource = cm.ExecuteReader();
                                        DataList1.DataBind();

                                        cm.Connection.Close();


                                        }

                                        catch (Exception ex)
                                        {
                                        Response.Write(ex.Message);
                                        }

                                        finally
                                        {
                                        con.Close();
                                        }
                                }
                       }
              }
        }
}

推荐答案

如果我没错,你就是在循环内一次执行一个项目的查询。



你需要做的就是你需要的将所有选定的值附加到 StringBuilder 或其他内容中。然后一次执行包含所有这些值的sql。
If I am not wrong, you are executing the query for one item at a time inside the loop.

What you need to do is that you need to append all the selected value in a StringBuilder or something. Then execute sql with all those values at once.


这篇关于使用C#和asp.net进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆