各位好,小弟向大家请教个问题,是这样的,我的页面上有90多个DropDownList控件,名子分别为DropDownList1到DropDownList90,怎样用一个for语句,把这90个DropDownList的所选择项的值用一个字符串把它加起来?

解决方案 »

  1.   

    for循环里面findcontrol然后读值添加到string里面
      

  2.   

    循环整个页面
    判断控件type是否是DropDownList
    然后再累加
      

  3.   

    string strName="";
    string strValue="";
    StringBuilder sb = new StringBuilder();
    for(int i=1;i<=90;i++)
    {
      strName="DropDownList"+i.ToString();
      DropDownList ddlName=Page.FindControl(strName) as DropDownList;
      if(ddlName!=null)
        {
          strValue=ddlName.SelectedValue;
          sb.Append(strValue);
          sb.Append(";");
        }
      
    }
      

  4.   

    foreach (Control control in this.Controls)
                {
                    try
                    {
                        textBox1.Text += ((ComboBox)control).Text;
                    }
                    catch
                    { }
                }
      

  5.   

    不好意思,现在又有个问题,就是如果我的DropDownList的名称不是DropDownList1到DropDownList90,而是定义的名称,又该怎么做呢?
      

  6.   

    调用:  test(this.Controls);
    private void test(ControlCollection ct)
        {
            for (int i = 0; i < cts.Count; i++)
            {
                if (cts[i].HasControls())
                {
                    test(cts[i].Controls);
                }
                if (cts[i].GetType() == typeof(System.Web.UI.WebControls.DropDownList))
                {
                    label.text += (cts[i] as DropDownList).SelectedValue;
                }
            }
            return str;
        }
      

  7.   

    去掉最后的return  str