簡體   English   中英

我如何使用反射從文件后面的asp.net代碼訪問服務器端控件?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM