![](/img/trans.png)
[英]In asp.net, To enable/disable imagebutton, How can I access ImageButton control of Gridview->Itemtemplate->panel in asp.net code behind file
[英]How can i access a server side control from asp.net code behind file using reflection?
例如,如果我在aspx頁面上:
<asp:PlaceHolder ID="tab_0" runat="server" Visible="false"></asp:PlaceHolder>
<asp:PlaceHolder ID="tab_1" runat="server" Visible="false"></asp:PlaceHolder>
我想使用例如配置文件中的值在頁面后面的代碼中訪問這些屬性
string enabledTabs = "0,1,2,3";
如果有一種方法,我可以使用反射將其設置為啟用或禁用,例如
foreach(var id in enabledTabs.Split(','))
{
// <use reflection to get the correct tab control>
// Set property of the tab
tab.Visible = true;
}
我可以通過使用switch語句並設置特定的控件屬性來實現所需的結果,但是我想使用反射來使選項卡更加整潔。
有人可以幫忙嗎?
謝謝!
您不需要反思。 使用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.