怎样用循环语句给100个TextBox的text属性赋值?
我的页面上有100个TextBox控件,同时有100个字符串变量(已经赋了值了),每个TextBox的id都和字符串变量的名字有一定的关系,如:
protected System.Web.UI.WebControls.TextBox employeeId;
protected System.Web.UI.WebControls.TextBox beginDate;
protected System.Web.UI.WebControls.TextBox updateDate;
protected System.Web.UI.WebControls.TextBox employeeName;
protected System.Web.UI.WebControls.TextBox oldName;
protected System.Web.UI.WebControls.TextBox nation;
protected System.Web.UI.WebControls.TextBox birthday;
protected System.Web.UI.WebControls.RadioButtonList sex;
protected System.Web.UI.WebControls.RadioButtonList marriage;
protected System.Web.UI.WebControls.TextBox BloodType;
protected System.Web.UI.WebControls.TextBox age;
protected System.Web.UI.WebControls.TextBox politics;
protected System.Web.UI.WebControls.TextBox health;
protected System.Web.UI.WebControls.TextBox weight;
protected System.Web.UI.WebControls.TextBox nativePlace;
protected System.Web.UI.WebControls.TextBox stature;
protected System.Web.UI.WebControls.TextBox IDCard;
protected System.Web.UI.WebControls.TextBox classDay;
protected System.Web.UI.WebControls.TextBox educationPlace;
protected System.Web.UI.WebControls.TextBox major;
protected System.Web.UI.WebControls.TextBox homeAddress;
protected System.Web.UI.WebControls.TextBox education;
protected System.Web.UI.WebControls.TextBox language;
protected System.Web.UI.WebControls.TextBox LanguageLay;public string strage="";
public string stremployeeName="";
public string strdepartmentId="";
public string strdepartmentName="";
public string strworkType="";
public string strworkAddress="";
public string strstate="";
public string strstateName="";
public string strbirthday="";
public string strsex="";
public string strbeginDate="";
public string strhomeTel="";
public string strofficeTel="";
public string strofficeAddress="";
public string strmobileTel="";
public string strhomeAddress="";
public string streducation="";
public string strmarriage="";
public string strstature="";
public string strweight="";
public string strmotherTongue="";
public string strintroduce="";
public string strpolitics="";
public string strnativePlace="";
public string streducationPlace="";
public string strmajor="";
public string strserviceLength="";
public string strIDCard="";
public string strlanguage="";
public string stremail="";
public string stremployTime="";
public string strupdateDate="";
public string stroldName="";
public string strhavePhoto="";
public string strnation="";
public string strclassDay="";
public string strhealth="";
public string strLanguageLay="";
即字符串的名字是在textBox的id名称前加上"str"
请问各位在这种有规律的情况下,如何通过循环语句将字符串变量的值一一对应的赋值给相应的textBox啊,-------------------------------
employeeId.Text=stremployeeId;
这样赋值的话太麻烦了,需要写100个语句啊

解决方案 »

  1.   

    关注,在js里面这个用eval来解决,再c#里面就不知道了
      

  2.   

    你不辛苦嗎?這麼寫用客戶端很簡單的問題,被你做得這麼麻煩文本框改成這樣的形式
    <input type="text" name="txt1" id="txt1" />
    <input type="text" name="txt2" id="txt2" />
    ..
    <input type="text" name="txt100" id="txt100" />後台取值
    string[] txts = new string[100];
    for(int i=1; i<=100; i++)
    {
     txts[i] = Request.Form["txt" + i.ToString()];
    }
    賦值也采用客戶端for(var i=1; i<100; i++)
    {
     document.all("txt" + i).value = "指定值";
    }
      

  3.   

    <input type="text" name="txt1" id="txt1" value='<%=服務器指定值%>'/>
      

  4.   

    就算可以通过循环得到TextBox的ID,那么给它们赋的值也需要循环吗?  
    给它们赋的值很难循环啦!
      

  5.   

    把100多个变量的值用Hashtable存储么.Hashtable h = new Hashtable();
    h.Add("stremployeeName","");
    h.Add("strdepartmentId","");
    ....foreach(Control ctl in this.Page.FindControl("Form1").Controls)
    {
    if(null!=h["str"+ctl.ID])
    {
    if(ctl is TextBox)
    ((TextBox)ctl).Text = h["str"+ctl.ID].ToString();
    }
    }
      

  6.   

    这100个字符串变量已经的赋值了,这些值是从数据库中的相应字段中取得的,
    可以通过循环得到TextBox的ID,给他们赋的值(相应的字符串中的内容)虽然不同,但是TextBox的ID和字符串的名字是有联系的阿
      

  7.   

    TextBox[]test=new TextBox[2]{TextBox1,TextBox2};
    string []strTest=new string[2]{"44","55"};
    for(int i=0;i<2;i++)
    {
        test[i].Text=strTest[i];
    }
    用控件数组
      

  8.   

    TextBox[]test=new TextBox[2]{TextBox1,TextBox2};string []strTest=new string[2]{"44","55"};for(int i=0;i<2;i++){    test[i].Text=strTest[i];}------------------------------
    没用过,学习
      

  9.   

    test[i].Text=strTest[i];这样也可以 ?
      

  10.   

    递归遍历所有textbox:
    private void findControls(Control control)
    {
        foreach (Control c in control.Controls)
        {
            string cType = c.GetType().Name;
            if (cType == "TextBox")
                 ((TextBox)c).Text = "str" + c.ID;
       
             if (c.Controls.Count > 0)
             {
                  findControls(c);
              }
         }
    }
      

  11.   

    回楼上的,"str" + c.ID是一个索引值,用Hashtable来保存变量吧。
      

  12.   

    使用FindControl根据控件ID遍历控件
    for (int i=0; i<100; ++)
      ((TextBox)FindControl("控件" + i.ToString())).Text = "值";
      

  13.   

    bt
    textBox的ID本来就应该用txt + (0...100)作为ID编号,不是很方便吗!