简体   繁体   English

我如何使用反射从文件后面的asp.net代码访问服务器端控件?

[英]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.

相关问题 在asp.net中,要启用/禁用图像按钮,如何在asp.net代码后面的文件中访问Gridview-&gt; Itemtemplate-&gt;面板的ImageButton控件 - In asp.net, To enable/disable imagebutton, How can I access ImageButton control of Gridview->Itemtemplate->panel in asp.net code behind file 我怎样才能从asp.net后面的代码中包装一个带有div的控件? - how can I wrap a control with a div from the code behind in asp.net? 如何在不使用ASP.NET服务器端控件的情况下从C#ASP.NET中读取HTML文件类型的输入流 - How to read inputstream from HTML file type in C# ASP.NET without using ASP.NET server side control 如何从服务器端代码访问asp.net core中的目录路径? - How do I access a directory path in asp.net core from server side code? 为什么不能将焦点设置为Javascript或后面的代码中的ASP.Net TextBox控件? - Why can't I set the focus to an ASP.Net TextBox control in Javascript or from code behind? 如何使用asp.net访问服务器端的客户端文件 - how to access client side file in server side using asp.net 如何从asp.net中的服务器端代码向动态创建的文本框控件添加事件处理程序 - How to add event handlers to a dynamically created textbox control from server side code in asp.net 在另一个控件中使用asp.net访问控件的名称,而无需使用后面的代码 - asp.net access control's name in another control without using code behind 如何在代码隐藏方法中访问asp.net按钮的“文本”? - How can I access asp.net button's “Text” in a code behind method? ASP.NET无法从背后的代码访问ASP元素(用户控制) - ASP.NET cannot access asp element from code behind (User Control)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM