[英]How can i access a server side control from asp.net code behind file using reflection?
For example, if i have on the aspx page: 例如,如果我在aspx页面上:
<asp:PlaceHolder ID="tab_0" runat="server" Visible="false"></asp:PlaceHolder>
<asp:PlaceHolder ID="tab_1" runat="server" Visible="false"></asp:PlaceHolder>
and i want to access these properties in the code behind page using values from a configuration file for example 我想使用例如配置文件中的值在页面后面的代码中访问这些属性
string enabledTabs = "0,1,2,3";
if there a way i can use reflection to set them to enabled or disabled eg 如果有一种方法,我可以使用反射将其设置为启用或禁用,例如
foreach(var id in enabledTabs.Split(','))
{
// <use reflection to get the correct tab control>
// Set property of the tab
tab.Visible = true;
}
I could acheive the result i want by using a switch statement and setting the particular control property, but i'd like to use reflection to get the tab to make it cleaner. 我可以通过使用switch语句并设置特定的控件属性来实现所需的结果,但是我想使用反射来使选项卡更加整洁。
Could anyone help? 有人可以帮忙吗?
Thanks! 谢谢!
You don't need reflection. 您不需要反思。 Use Page.FindControl :
使用Page.FindControl :
foreach(var id in enabledTabs.Split(','))
{
PlaceHolder control = (PlaceHolder)this.FindControl("tab_"+id));
control.Visible = true;
}
foreach(var id in enabledTabs.Split(','))
{
// Set property of the tab
Page.FindControl("tab_" + id.ToString()).Visible = true;
}
请尝试以下操作:
Control tab = Control.FindControl("tab_"+id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.