我在前台有
<asp:Panel ID="CommodityType" runat="server" CssClass="comtype" > </asp:Panel>
后台动态添加控件
DataSet xmlcomtype = new DataSet(); xmlcomtype.ReadXml(Server.MapPath(@"~/XML/CommodityType.xml")); foreach(DataRow drtype in xmlcomtype.Tables[0].Rows) { CheckBox cb = new CheckBox(); cb.Text = drtype["Type"].ToString(); CommodityType.Controls.Add(cb); }
后台遍历控件
foreach (Control c in CommodityType.Controls) { if (c.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox")) { CheckBox cb = (CheckBox)c; if (cb.Checked) ct.CommodityType += "," + cb.Text; } } if (!string.IsNullOrEmpty(ct.CommodityType)) ct.CommodityType = ct.CommodityType.Substring(1);
怎么我遍历不出控件的,输出控件数量
ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('" + CommodityType .Controls.Count.ToString()+ "');</script>");
输出值为1
但是我后台添加了好几个控件了
求帮忙
1 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
晕,给你一个Winform的例子。我项目中实际用的,你照着比就可以了。
/// <summary>
/// 清除容器里面某些控件的值
/// </summary>
/// <param name="parContainer">容器控件</param>
public static void ClearCntrValue(Control parContainer)
{
for (int index = 0; index < parContainer.Controls.Count; index++)
{
//如果是容器类控件,递归调用自己
if (parContainer.Controls[index].HasChildren)
{
ClearCntrValue(parContainer.Controls[index]);
}
else
{
switch (parContainer.Controls[index].GetType().Name)
{
case "Label":
break;
case "ComboBox":
((ComboBox)(parContainer.Controls[index])).Text = "";
break;
case "TextBox":
((TextBox)(parContainer.Controls[index])).Text = "";
break;
case "RichTextBox":
((RichTextBox)(parContainer.Controls[index])).Text = "";
break;
case "MaskedTextBox":
((MaskedTextBox)(parContainer.Controls[index])).Text = "";
break;
case "RadioButton":
((RadioButton)(parContainer.Controls[index])).Checked = false;
break;
case "CheckBox":
((CheckBox)(parContainer.Controls[index])).Checked = false;
break;
}
}
}
}
- 1 回答
- 0 关注
- 493 浏览
添加回答
举报
0/150
提交
取消