2010-12-22 58 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只在某些情况下需要。不是在页面加载时一直加载控件,只在需要时才添加。它是动态添加的。