我在aspx页面写了一段代码:
- HTML code
<table width="100%" border="0" cellpadding="0" cellspacing="10"><% foreach (YM.Model.SystemSetting model in systemSettingList) { %> <tr> <td style="height: 16px; width:100px" align="right"><%=model.SystemKey%>:</td> <td style="height: 16px; width:270px;" align="left"> <input id="TextBox_<%=model.SystemID %>" name="TextBox_<%=model.SystemID %>" type="text" value="<%=model.SystemValue %>" class ="box1" onfocus="this.className='box2'" onblur="this.className='box1'" style ="width:260px;" maxlength="200"/> </td> <td align="left" class="notes">注:<%=model.SystemExplain%></td> </tr><% }%></table>
如何在CS页面获取所有TextBox的值和ID
------解决方案--------------------------------------------------------
foreach (YM.Model.SystemSetting model in systemSettingList)
{
string strInputID = "TextBox_" + model.SystemID;
string strValue = Request.Param[strInputID];
}
------解决方案--------------------------------------------------------
- C# code
protected void btnClear_Click(object sender, EventArgs e) { foreach (Control ctl in this.Controls) { this.txtClear(ctl); } } #endregion private void txtClear(Control ctls) { if(ctls.HasControls()) { foreach (Control ctl in ctls.Controls) { txtClear(ctl); } } else { if (ctls.GetType().Name == "TextBox") { TextBox tb = new TextBox(); tb = (TextBox)this.FindControl(ctls.ID); //这里是清空所有的值,你想赋什么值直接改tx.Text即可. tb.Text = ""; } else if (ctls.GetType().Name == "DropDownList") { DropDownList ddl = new DropDownList(); ddl = (DropDownList)this.FindControl(ctls.ID); ddl.SelectedIndex = 1; } } }