C# 通过反射获取控件
我需要得到一个控制[文本框,标签,按钮…等]从我的网页由它的ID使用反射 在我的例子中,我有一个所有其他系统页面都从中继承的类,这个类重写onload事件以将某些属性更改为某些控件 类似于通过名称设置textbox的可见性状态,但由于我没有直接在页面上设置控件,因为我可能在母版页的内容占位符上设置控件,所以我无法使用findcontrol方法,我认为查找控件的递归函数将花费太多时间 因此,我尝试使用refelection找到一个名为的控件,然后更改它的visable或enable状态 我使用了FieldInfo类,但不适用于我C# 通过反射获取控件,c#,.net,reflection,C#,.net,Reflection,我需要得到一个控制[文本框,标签,按钮…等]从我的网页由它的ID使用反射 在我的例子中,我有一个所有其他系统页面都从中继承的类,这个类重写onload事件以将某些属性更改为某些控件 类似于通过名称设置textbox的可见性状态,但由于我没有直接在页面上设置控件,因为我可能在母版页的内容占位符上设置控件,所以我无法使用findcontrol方法,我认为查找控件的递归函数将花费太多时间 因此,我尝试使用refelection找到一个名为的控件,然后更改它的visable或enable状态 我使用了F
FieldInfo fi = this.GetType().GetField("ControlID", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
任何帮助 你试过了吗
也许你需要用FindControl递归地寻找你的控件,然后调用它自己
private static Control FindNestedControl(string id, Control parent)
{
Control item = parent.FindControl(id);
if(item == null)
{
foreach(var child in parent.Controls)
{
item = child.FindNestedControl(id, child);
if(item != null)
{
return item;
}
}
}
return null;
}
并调用它通过以下方式传递页面的当前实例:
Control item = FindNestedControl("bob", this);
但是,这可能会很慢-因此,如果页面上有一大堆控件,请务必小心:)
另一种方法是在基类中将控件作为属性公开:
public abstract class BasePage : Page
{
#region Properties
/// Gets the textbox for editing in derived classes.
protected TextBox SomeTextBox
{
return this.textBox; }
}
#endregion
}
public abstract class BasePage : Page
{
/// <summary>
/// Called when loading the page.
/// </summary>
/// <param name="e">The event arguments.</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Do some basic setup.
// Now pass the controls to the derived classes.
this.ConfigureControl(this.mainText);
this.ConfigureControl(this.nameDropDown);
this.ConfigureControl(this.someOtherControl);
}
/// <summary>
/// Provides hook for derived classes.
/// </summary>
/// <param name="control">The core control.</param>
protected virtual void ConfigureControl(Control control)
{
}
}
最后一种方法是挂接基类中的派生类:
public abstract class BasePage : Page
{
#region Properties
/// Gets the textbox for editing in derived classes.
protected TextBox SomeTextBox
{
return this.textBox; }
}
#endregion
}
public abstract class BasePage : Page
{
/// <summary>
/// Called when loading the page.
/// </summary>
/// <param name="e">The event arguments.</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Do some basic setup.
// Now pass the controls to the derived classes.
this.ConfigureControl(this.mainText);
this.ConfigureControl(this.nameDropDown);
this.ConfigureControl(this.someOtherControl);
}
/// <summary>
/// Provides hook for derived classes.
/// </summary>
/// <param name="control">The core control.</param>
protected virtual void ConfigureControl(Control control)
{
}
}
如果没有那么多控件,我可能会使用第二个。第一种方法在处理中继器等时很有用。这里有一种非常有用的扩展方法,可以递归地获取子控件:
public static IEnumerable<Control> GetChildControls(this Control control)
{
var children = (control.Controls != null) ? control.Controls.OfType<Control>() : Enumerable.Empty<Control>();
return children.SelectMany(c => GetChildControls(c)).Concat(children);
}
公共静态IEnumerable GetChildControls(此控件)
{
var children=(control.Controls!=null)?control.Controls.OfType():Enumerable.Empty();
返回children.SelectMany(c=>GetChildControls(c)).Concat(children);
}
用法:
IEnumerable<Control> allChildren = parent.GetChildControls();
IEnumerable allChildren=parent.GetChildControls();
感谢您的尝试,但我不知道级别数,因此无法使用FindControldirectlly@AshOoO:“级别计数”?你是什么意思?意思是我可能有母版页和内容占位符,所以只使用这个。findcontrol在其他页面上不起作用。我在页面上有直接控件,其他控件可能在包含子内容的内容中>>>所以使用这个。findcontrol不适用于所有的直接库为您提供所有关于您有用的解决方案。。。但问题是要用refelection获得控件,而不是递归所有答案都是无用的,没有一个使用反射。。