首页新闻找找看学习计划

Panel控件遍历问题

0
[已解决问题] 解决于 2011-05-03 23:10

我在前台有

        <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

但是我后台添加了好几个控件了

求帮忙

合仔茶的主页 合仔茶 | 初学一级 | 园豆:151
提问于:2011-04-01 07:30
< >
分享
最佳答案
0

晕,给你一个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;
}
}
}
}

.NET快速开发框架 | 小虾三级 |园豆:914 | 2011-04-02 16:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册