protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDL();
            BindDDL2();
            BindCBL();
            form1.Target = "_blank";
        }
    }
    private void BindDDL()
    {//绑定年份
        string sqlstr = "select distinct(Years) from Report";
        DataSet ds = db.GetDS(sqlstr);
        this.DropDownList1.DataSource = ds;
        this.DropDownList1.DataTextField = "Years";
        this.CheckBoxList1.DataValueField = "Years";
        this.DropDownList1.DataBind();
    }
    private void BindDDL2()
    {//绑定单位
        string sqlstr = "select distinct(Adddepname) from Report where Adddepname<>''";
        DataSet ds = db.GetDS(sqlstr);
        this.DropDownList2.DataSource = ds;
        this.DropDownList2.DataTextField = "Adddepname";
        this.DropDownList2.DataValueField = "Adddepname";
        this.DropDownList2.DataBind();
        //this.DropDownList2.Items.Insert(0, new ListItem("--请选择--", "-1"));
    }
    private void BindCBL()
    {//绑定多选按钮
        string sqlstr = "select distinct(Months) from Report";
        DataSet ds = db.GetDS(sqlstr);
        this.CheckBoxList1.DataSource = ds;
        this.CheckBoxList1.DataTextField = "Months";
        this.CheckBoxList1.DataValueField = "Months"; 
        this.CheckBoxList1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {//跳转按钮
        Response.Redirect("SelectTongJi.aspx?dwmc='" + DropDownList2.SelectedValue + "'&Years='"+DropDownList1.SelectedValue+"'&Months='"+CheckBoxList1.SelectedValue+"'");
    }
上面是我的第一个页面
下面是我的第二个页面
protected void Page_Load(object sender, EventArgs e)
    {
        TJ();
    }
    private void TJ()
    {
        string sqlselect = "select sum([A]) as A,sum([B]) as B, sum([C]) as C, sum([D]) as D, sum([E]) as E, sum([F]) as F, sum([G]) as G, sum([H]) as H, sum([I]) as I, sum([J]) as J, sum([K]) as K, sum([L]) as L, sum([M]) as M, sum([N]) as N, sum([O]) as O, sum([P]) as P, sum([Q]) as Q, sum([R]) as R, sum([S]) as S, sum([S]) as T, sum([T]) as T, sum([U]) as U  from Report where Years="+Request.QueryString["Years"].ToString()+" and (Months=1 or Months=2) and Adddepname="+Request.QueryString["dwmc"].ToString()+" group by Line";
        DataSet ds = db.GetDS(sqlselect);
    }
大家不用想我的SQL语句太长,我主要想让大家看的是红色的部分,多选按钮CheckBoxList1 例如我选择1和3,那么我第二个页要把值接过来。我主要就是想在第二个页面接到CheckBoxList1的值。小弟刚从学校毕业,刚上班,什么也不会,小弟在线等。

解决方案 »

  1.   

    foreach(ListItem li in checlboxlist.Items)
    {
     str+=li.Checked?li.Value+",":"";
    }
    传递到第二个页面?id=str;
    使用in () 查询数据
      

  2.   

    先判断checlboxlist中的选中项,如楼上所述
      

  3.   

    就用1楼的方法!
    在你的Button1_Click事件里写代码
    foreach(ListItem li in checlboxlist.Items)
    {
     str+=li.Checked?li.Value+",":"";
    }
    Response.Redirect("SelectTongJi.aspx?dwmc='" + DropDownList2.SelectedValue + "'&Years='"+DropDownList1.SelectedValue+"'&Months='"+str+"'");然后在你的第2个页面中将TJ函数修改成
    private void TJ()
        {
            string sqlselect = string.Format("select sum([A]) as A,sum([B]) as B, sum([C]) as C, sum([D]) as D, sum([E]) as E, sum([F]) as F, sum([G]) as G, sum([H]) as H, sum([I]) as I, sum([J]) as J, sum([K]) as K, sum([L]) as L, sum([M]) as M, sum([N]) as N, sum([O]) as O, sum([P]) as P, sum([Q]) as Q, sum([R]) as R, sum([S]) as S, sum([S]) as T, sum([T]) as T, sum([U]) as U  from Report where Years="+Request.QueryString["Years"]+" [color=#FF0000]and (Months in {0}) and Adddepname="+Request.QueryString["dwmc"]+" group by Line",Request.QueryString["Months"]);
            DataSet ds = db.GetDS(sqlselect);
        }
      

  4.   

    记住 
    Request.QueryString["dwmc"].ToString() 这种写法是后面的.ToString()是多余的,以后不要加上了,保证代码的可读性!
      

  5.   

    foreach (ListItem li in checlboxlist.Items) 这里出错:
    当前上下文中不存在名称“checlboxlist”
      

  6.   


    foreach (ListItem li in CheckBoxList.Items)//报错:错误 399 非静态的字段、方法或属性“System.Web.UI.WebControls.ListControl.Items.get”要求对象引用 D:\经保\内网\UI\UI\SearchCount\report\tongJi.aspx.cs 56 33 D:\...\UI\        {
                str += li.Checked ? li.Value + "," : "";//报错错误 400 当前上下文中不存在名称“str” D:\经保\内网\UI\UI\SearchCount\report\tongJi.aspx.cs 58 13 D:\...\UI\
     还有错误 401 “System.Web.UI.WebControls.ListItem”并不包含“Checked”的定义 D:\经保\内网\UI\UI\SearchCount\report\tongJi.aspx.cs 58 23 D:\...\UI\        }