如下:info.是实体类,txtcustomData1就是TextBox文本了,下面的意思是如果TextBox显示了,就用TextBox里的值赋值到info里,如果TextBox是隐藏的,就用DropDownList的值赋给info.现在有差不多100个这样的判断,请问大家有什么好方法解决没?for里能写吗?开始用 for写但不管用.谢谢大家了,帮忙解决下,会的把代码贴出来.被这折磨死了!!问题解决了再加分!!!!!!!!!!!!!!!!!!!!!!1 
if (txtcustomData1.Visible == true)
        {
            info.CustomData1 = this.txtcustomData1.Text.Trim();
        }
        else
        {
            info.CustomData1 = this.DropDownList1.SelectedValue.Trim();
        }
        if (txtcustomData2.Visible == true)
        {
            info.CustomData2 = this.txtcustomData2.Text.Trim();
        }
        else
        {
            info.CustomData2 = this.DropDownList2.SelectedValue.Trim();
        }
        if (txtcustomData3.Visible == true)
        {
            info.CustomData3 = this.txtcustomData3.Text.Trim();
        }
        else
        {
            info.CustomData3 = this.DropDownList3.SelectedValue.Trim();
        }
        if (txtcustomData4.Visible == true)
        {
            info.CustomData4 = this.txtcustomData4.Text.Trim();
        }
        else
        {
            info.CustomData4 = this.DropDownList4.SelectedValue.Trim();
        }

解决方案 »

  1.   

    问题说的不清
    textbox 是否在父控件内?
      

  2.   


      Control.ControlCollection cons = form1.Controls; ;
                foreach(Control con in cons)
                {
                    if (con.GetType() == (new TextBox()).GetType())
                    {                 }
                }我觉得这样可以遍历所有的textBox
      

  3.   

    遍历Page.Controls集合可以找到你要的控件, 但是你用于赋值的属性名称也实在是太土了: info.CustomData1, info.CustomData2, ....我就是想帮忙也爱莫能助啊?!
      

  4.   

    CustomData1是属性,控件声明成公共成员,都可以反射出
    或者更简单的,用vb的CallByName但是... info真的有100个属性吗?考虑修改下设计吧...
      

  5.   

    foreach (System.Windows.Forms.Control control in this.Controls)
    {
    if (control is System.Windows.Forms.TextBox)
    {
    System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control ; 
    tb.Text = String.Empty ;
    }
    }
      

  6.   

    for (int i = 0; i < this.form1.Controls.Count; i++) 

        if ((this.form1.Controls[i] is TextBox) && (this.form1.Controls[i] as TextBox).Visible) 
        { 
            Response.Write((this.form1.Controls[i] as TextBox).Text); 
        } 

    试试
      

  7.   


    FOR (int i=0,i<1000000000,i++)
    {
     string stra = "txtcustomData" + i.ToString;
    string strb = "DropDownList" + i.ToString;
    string strc = "CustomData" + i.ToString;
     TextBox TextBoxa = (TextBox)this.FindControl(stra);
     DropDownList DropDownLista = (DropDownList)this.FindControl(strb);if (TextBoxa.Visible == true)
            {
                info.strc = this.TextBoxa.Text.Trim();
            }
            else
            {
                info.strc = this.DropDownLista.SelectedValue.Trim();
            }}
      

  8.   


    for (int i=0,i<100,i++)
    {
     string stra = "txtcustomData" + i.ToString();
    string strb = "DropDownList" + i.ToString();
    string strc = "CustomData" + i.ToString();
     TextBox TextBoxa = (TextBox)this.FindControl(stra);
     DropDownList DropDownLista = (DropDownList)this.FindControl(strb);if (TextBoxa.Visible == true)
            {
                info.strc = this.TextBoxa.Text.Trim();
            }
            else
            {
                info.strc = this.DropDownLista.SelectedValue.Trim();
            }}
      

  9.   

    实体类定义一个赋值方法为所有属性赋值,或者构造函数也行。 
    传递参数的为数组 如果属性类型不一致就对象数组。赋值的时候用for循环
    类似这样
    TextBox txt;
    DropDownList ddl;
    object [] obj=new object[10];
    for(int i=0;i<10;i++)
    {
       txt=FindControl("txtcustomData"+i.ToString()) as TextBox ;
       //默认TextBox 控件加载在页面上,如果加载在别的地方 调用相应的FindControl方法
       if(txt!=null)
       {
            if(txt.Visible == true)
            {
               obj[i]=txt.Text.Trim();
            }
            else
            {
               ddl = FindControl("DropDownList"+i.ToString()) as DropDownList;
               if(ddl!=null)
               {
                   obj[i]= ddl.SelectedValue.Trim();
               }
            }
       }
       
    }
    info.SetValues(obj);info.SetValues方法 你可以这样写public void SetValues (object [] obj)
    {
        info.CustomData1 =obj[0];
        info.CustomData2 =obj[1];
        info.CustomData3 =obj[2];
        ...}
      

  10.   


    for (int i=0,i<100,i++)
    {
     string stra = "txtcustomData" + i.ToString();
    string strb = "DropDownList" + i.ToString();
    string strc = "CustomData" + i.ToString();
     TextBox TextBoxa = (TextBox)this.FindControl(stra);
     DropDownList DropDownLista = (DropDownList)this.FindControl(strb);
    if (TextBoxa!=null && DropDownLista!=null)
    {
    if (TextBoxa.Visible == true)
            {
                info.strc = this.TextBoxa.Text.Trim();
            }
            else
            {
                info.strc = this.DropDownLista.SelectedValue.Trim();
            }
    }
    }
      

  11.   


    string strc = "CustomData" + i.ToString();
    info.strc 
    你真有创意~  至少你出现在专家列表里,别想当然好么
      

  12.   

    [code=C#]for (int i = 1; i <= 10; i++)
            {
                Request.Form["txtName" + i].ToString().Trim();
                }
      

  13.   

     public class MyInfo
        {
            string ASP_ID;
            /// <summary>
            /// ID
            /// </summary>
            public string ASP_ID1
            {
                get { return ASP_ID; }
                set { ASP_ID = value; }
            }        string ASP_NAME;
            /// <summary>
            /// 名字
            /// </summary>
            public string ASP_NAME1
            {
                get { return ASP_NAME; }
                set { ASP_NAME = value; }
            }
    }info.SetValues(obj);  这错误提示,info.里不包含SetValues的定义,..info.SetValues方法 你可以这样写public void SetValues (object [] obj)
    {
        info.CustomData1 =obj[0].ToString();//后面得写.ToString()了``呵呵``
        info.CustomData2 =obj[1].ToString();
        info.CustomData3 =obj[2].ToString();
        ...}
      

  14.   


      Control.ControlCollection cons = form1.Controls; ;
                foreach(Control con in cons)
                {
                    if (con.GetType() == (new TextBox()).GetType())
                    {                 }
                }人力资源
      

  15.   


    C# code
    for (int i=0,i<100,i++)
    {
     string stra = "txtcustomData" + i.ToString();
    string strb = "DropDownList" + i.ToString();
    string strc = "CustomData" + i.ToString();<-------对象的对应属性字符 TextBox TextBoxa = (TextBox)this.FindControl(stra);
     DropDownList DropDownLista = (DropDownList)this.FindControl(strb);
    if (TextBoxa!=null && DropDownLista!=null)
    {
          string value=TextBoxa.Visible?TextBoxa.text:DropDownLista.SelectedValue
          info.GetType().GetProperty(strc).SetValue(info,value,null) <----------------反射字符为属性赋值
    }
    }
    和风的想法一样 他估计失误忘记反射了
      

  16.   

    我基础还差了点,向非哥和qqshenyunzcz学习
      

  17.   

    楼上的这么多代码肯定都可以解决楼主的问题了。但是,我想问一下,楼主怎么会设计出这么一个结构?100个Infoxxx这样的属性吗?可以考虑类不要这样来设计,用一个数组或者别的什么来索引一下,可能更好。
    另外,界面上放 100 个这样的 TextBox 或者 label 吗?这个对用户可能也是比较灾难的情况。
      

  18.   

    http://topic.csdn.net/u/20090511/23/9515b5b1-a1f9-41d9-b7a4-369aa95ce08c.html
      

  19.   

    http://topic.csdn.net/u/20090511/23/9515b5b1-a1f9-41d9-b7a4-369aa95ce08c.html
      

  20.   

    lz可以试试jquery。找的时候直接找text 的
      

  21.   

    你可以通过FindControl来找到这些控件。
      

  22.   

    肯定没有,现在.net版抢分的人太多太能抢了,我自认抢不过,而且,到了四星我就更不会抢了
      

  23.   

    如果有规律的话,可以用循环吧 for(i=0;i<X;i++)
    {
      TextBox tb=FindControl("txtcustomData1"+"i") as TextBox;
    }
    得到TEXTBOX实例你就可以随便操作了