[英]How to find Controls using Web Page Object?
这是代码
private void Get_Controls()
{
_Default def = new _Default();
for (int i = 0; i < def.Controls.Count; i++)
{
}
}
我在Default.aspx中具有控件,但是Count给我0。
尝试这个:
foreach (var control in this.Controls)
{
}
尝试以下一项。
foreach (Control c in Page.Controls)
{
foreach (Control childc in c.Controls)
{
if (childc is TextBox)
{
allTextBoxValues += ((TextBox)childc).Text + ",";
}
}
}
谢谢。 亲爱的我已经应用了此代码。 为了使用foreach查找控件,我应用了10-12个循环,这比给我的控件要多。
private void Refresh()
{
foreach (Control c in Page.Controls)
{
foreach (Control childc in c.Controls)
{
foreach (Control childe in childc.Controls)
{
if (childe is DevExpress.Web.ASPxPopupControl.ASPxPopupControl)
{
foreach (Control childf in childe.Controls)
{
foreach (Control childg in childf.Controls)
{
foreach (Control childh in childg.Controls)
{
foreach (Control childi in childh.Controls)
{
foreach (Control childj in childi.Controls)
{
foreach (Control childk in childj.Controls)
{
foreach (Control childl in childk.Controls)
{
if (childl is TextBox)
{
((TextBox)childl).Text = "";
}
else if (childl is DevExpress.Web.ASPxEditors.ASPxTextBox)
{
((DevExpress.Web.ASPxEditors.ASPxTextBox)childl).Text = "";
}
else { }
}
}
}
}
}
}
}
}
}
}
}
}
而是写长代码
递归查找控件
private Control FindControlReqursive(Control parent)
{
foreach (Control control in parent.Controls)
{
Control result = FindControlReqursive(control);
if (result != null)
{
if (result is TextBox )
{
TextBox myTextBox = (TextBox)result;
myTextBox.Text = "";
}
return result;
}
}
return parent.FindControl(controlId);
}
调用
// controlId is id of the control u want to find
//eg. i m finding label inside page
TextBox myLabel=FindControlReqursive(Page);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.