无法在datalist

时间:2015-08-05 15:24:34

标签: c# asp.net checkbox

我有一个数据列表,其中包含具有唯一ID的产品类别,与我目录中的每个产品相对应。但是,当我选中2个或更多复选框时,我只会显示下一页的最新数据,但不会显示所有数据。仅显示1个数据。我有什么方法可以这样做,我可以一次传递3个不同的ID并在nxt页面上显示?

void GetCheckedBox()
{
    foreach (DataListItem li in DataList1.Items)
    {
        //access the check checklist
        HtmlInputCheckBox cb = li.FindControl("FavChkBox") as HtmlInputCheckBox;
        if (cb != null && cb.Checked)
        {
            ArrayList Product = new ArrayList();

            LblText.Text += " , ";

            LblText.Text += cb.Value;
            Product.Add(cb.Value);
            string url = "CompareProducts.aspx?prodId=" + cb.Value.ToString();
            Response.Redirect(url);
        }
    }
}

CompareProducts.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Product aProd = new Product();

        SqlConnection con = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand("SELECT * FROM Products WHERE Product_ID = @ProdID", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        cmd.Parameters.AddWithValue("@ProdID", Page.Request.QueryString["ProdID"].ToString());
        con.Open();
        adp.Fill(ds, "Products");
        cmd.ExecuteNonQuery();
        con.Close();

        GridView1.DataSource = ds;
        GridView1.DataBind();
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }

1 个答案:

答案 0 :(得分:1)

我不完全确定你在说什么,但根据你在ForEach循环中看到的代码你有一个response.redirect,所以在这种情况下你只会重定向到一个ID查询字符串。您可以切换为使用会话变量,该变量包含用户选择的所有ID的列表。

foreach (DataListItem li in DataList1.Items)
{

    //access the check checklist
    HtmlInputCheckBox cb = li.FindControl("FavChkBox") as HtmlInputCheckBox;
    if (cb != null && cb.Checked)
    {
        ArrayList Product = new ArrayList();

        LblText.Text += " , ";

        LblText.Text += cb.Value;
        Product.Add(cb.Value);
    }
Session.Add("SelectedProducts", Product);
        string url = "CompareProducts.aspx?hasProducts=true;
        Response.Redirect(url);
}

然后在pageload上的CompareProducts.aspx.cs中,如果没有回发,请检查查询字符串以决定是否应该加载id。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Page.Request.QueryString["hasProducts"].ToString() == "true")
            {
                ArrayList al = Session["SelectedProducts"] as ArrayList;

                if (al == null)
                    throw new ApplicationException("A product list is required.");

                if (al.Count < 1)
                    throw new ArgumentException("No products selected");

                string inStatement = string.Empty;

                foreach (var item in al)
                {
                    inStatement += al.ToString() + ", ";
                }
                inStatement = inStatement.Substring(0, inStatement.Length - 2);

                //Product aProd = new Product();
                SqlConnection con = new SqlConnection(strcon);
                SqlCommand cmd = new SqlCommand("SELECT * FROM Products WHERE Product_ID in (" + inStatement + ")", con); 
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                con.Open();
                adp.Fill(ds, "Products");
                cmd.ExecuteNonQuery();
                con.Close();
                GridView1.DataSource = ds;
                GridView1.DataBind();
                DataList1.DataSource = ds;
                DataList1.DataBind();
            }