2010-12-22 24 views
0

我有一個aspx頁面,它有一個CodeBehind,它引用一個帶有函數的頁面aspx.cs,並且在函數中它引用了具有以下代碼的Inherits。任何想法這是指什麼?ASP.Net:LoadControl在做什麼?

Control ctrl = LoadControl(System.Configuration.ConfigurationManager.AppSettings["CandidateShortControl"]); 
ctrl.ID = "AccountControl"; 
pnlContainer.Controls.Add(ctrl); 

- 編輯 - 會在哪裏一個可能會去在代碼中找到這個AccountControl?或CandidateShortControl?或者就像在問大海撈針在哪裏?

+0

我正在查看的代碼中的頁面是它所引用的aspx和aspx.cs。不是說這是正確的,而是我的例子中的每一個信息。 – David 2010-12-22 13:24:53

回答

4

LoadControll方法允許您以動態方式加載控件。

在您給出的示例中,它看起來像存儲在AppSettings文件中的控件的名稱(「mycontrolname.ascx」或其他)。

一旦控制加載,可以添加到頁面和你給的情況下,添加到名爲pnlContainer

面板控制代碼的擴展版本可能看起來像:

// Obtain our control name from the AppSettings file 
string controlName = System.Configuration.ConfigurationManager.AppSettings["CandidateShortControl"]; 

// Load the control into a variable 
Control ctrl = LoadControl(controlName); 

// Give our loaded control a unique ID 
ctrl.ID = "AccountControl"; 

// Add the loaded control to a panel control in our page 
pnlContainer.Controls.Add(ctrl); 
+1

Prolly .ascx;) – StuartLC 2010-12-22 13:15:32

0

添加到設置的控件? (Web.config)

0

這裏的要點是UserControl只在某些情況下需要。不是在頁面加載時一直加載控件,只在需要時才添加。它是動態添加的。