ArrayList al = new ArrayList();
写到外面去  你这么定义是局部的
ArrayList al = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{

解决方案 »

  1.   

    已找到,但现在我在button中要引用时说我:索引超出范围。必须为非负值并小于集合大小。
     Panel panel1 = (Panel)Page.FindControl("panel1");
            for (int i = 0; i < panel1.Controls.Count; i++)
            {
                if (panel1.Controls[i] is DropDownList)
                {
                    DropDownList c1 = (DropDownList)panel1.Controls[i];
                    
                    if(c1.SelectedValue!="请选择")
                    {
                        Label ls = (Label)al[i];
                        strSql.Append("  and  "+ls.Text+'='+c1.SelectedValue);
                        Response.Write(strSql);                }
                                   
     
                }
            }
      

  2.   

    if(c1.SelectedValue!="请选择")
    {
    if(i < al.Length)
    {
    Label ls = (Label)al[i];
    strSql.Append(" and "+ls.Text+'='+c1.SelectedValue);
    Response.Write(strSql);
    }
    }
      

  3.   

    如果你在BUTTON_CLICK 里要获取你的 ArrayList,首先要把 ArrayList 放入 ViewState 之类的能在页回传时保留值的对象内,获取时再取出。
      

  4.   

    紫色阴影:这样只是个判断,达不到我要的效果,我需要button里的i和al[i]是同步的,但现在说al[i]索引超出范围,如何解决
      

  5.   

    目标:你的意思是将EnViewState=true,还是?请具体点,刚学,不太懂
      

  6.   

    按理说,al[i]的i < panel1.Controls.Count,因该不会出现索引超范围的,不懂,请指教
      

  7.   

    他说填充后
    ViewState["al"] = al; //储存arraylist
    用的时候
    ArrayList al = (ArrayList)ViewState["al"];或者
    Session["al"] = al;ArrayList al = (ArrayList)Session["al"];
      

  8.   

    这样是能看到al[i]了,但在button中的for中的索引和原来的不一样了,而且取值的方式也和原来不同了,在for语句之外还好着,怎样让和for语句中的i同步,而且不会索引超范围?
    (下面的运行时当我点滴七个dropdownlist时,al[i]会溢出)
     protected void Button1_Click(object sender, EventArgs e)
    {
            ArrayList al = (ArrayList)ViewState["al"];
            //Response.Write(al[1]);        Panel panel1 = (Panel)Page.FindControl("panel1");
            for (int i = 0; i < panel1.Controls.Count; i++)
            {
                if (panel1.Controls[i] is DropDownList)
                {
                    DropDownList c1 = (DropDownList)panel1.Controls[i];
                    
                    if (c1.SelectedValue != "请选择")
                    {
                        strSql.Append("  and  " + al[i] + '=' + c1.SelectedValue);
                    }         }
                }
            }
      

  9.   

    ===========================================================================
    已找到,但现在我在button中要引用时说我:索引超出范围。必须为非负值并小于集合大小。 
    Panel   panel1   =   (Panel)Page.FindControl( "panel1 "); 
    for   (int   i   =   0;   i   <   panel1.Controls.Count;   i++) 

        if   (panel1.Controls[i]   is   DropDownList) 
        { 
            DropDownList   c1   =   (DropDownList)panel1.Controls[i]; 
            
            if(c1.SelectedValue!= "请选择 ") 
            { 
                Label   ls   =   (Label)al[i]; 
                strSql.Append( "     and     "+ls.Text+ '= '+c1.SelectedValue); 
                Response.Write(strSql); 
            }                                 
        } 
    }
    ============================================================================1.AreEqual(panel1.Controls.Count,al.Length)?
    2.al[panel1.Controls.Count-1]可以访问吗?