【发布时间】:2011-02-28 06:13:38
【问题描述】:
在 (c#) 后面的代码中,我动态创建了一些 RadioButtonLists,其中每个 RadioButtons 都有更多。我把所有控件放到一个特定的面板上。 我需要知道的是以后如何访问这些控件,因为它们不是在 .aspx 文件中创建的(通过从工具箱中拖放)?
我试过了:
foreach (Control child in panel.Controls)
{
Response.Write("test1");
if (child.GetType().ToString().Equals("System.Web.UI.WebControls.RadioButtonList"))
{
RadioButtonList r = (RadioButtonList)child;
Response.Write("test2");
}
}
“test1”和“test2”没有出现在我的页面中。这意味着这个逻辑有问题。 有什么建议吗?
【问题讨论】:
-
您可以用多种更好的方式重写循环,其中一种是
foreach (RadioButtonList list in panel.Controls.OfType<RadioButtonList>())。仅供参考。但是使用child.GetType().ToString()...将是我想到的最后一种方法。例如,if可能会说if (child is RadioButtonList) -
同意前面的评论,而且……你真的需要掌握一些 C# 知识,比如“is”关键字……
-
感谢您的建议。虽然我的代码没有错,但更有意义。
标签: c# asp.net radio-button dynamic