怎样用循环语句给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个语句啊
我的页面上有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个语句啊
<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 = "指定值";
}
给它们赋的值很难循环啦!
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();
}
}
可以通过循环得到TextBox的ID,给他们赋的值(相应的字符串中的内容)虽然不同,但是TextBox的ID和字符串的名字是有联系的阿
string []strTest=new string[2]{"44","55"};
for(int i=0;i<2;i++)
{
test[i].Text=strTest[i];
}
用控件数组
没用过,学习
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);
}
}
}
for (int i=0; i<100; ++)
((TextBox)FindControl("控件" + i.ToString())).Text = "值";
textBox的ID本来就应该用txt + (0...100)作为ID编号,不是很方便吗!