ASP.Net: What is this LoadControl doing?
I have a aspx page which has a CodeBehind which references a page aspx.cs with functions and in the function it references off of Inherits which has the following code. Any idea what this is referring to?
Control ctrl = LoadControl(System.Configuratio开发者_Python百科n.ConfigurationManager.AppSettings["CandidateShortControl"]);
ctrl.ID = "AccountControl";
pnlContainer.Controls.Add(ctrl);
-- Edit -- Where would one might go in the code to find this AccountControl? or CandidateShortControl? Or is that like asking where is the needle in a haystack?
The LoadControll method allows you to dynamiocally load a control.
In the example you've given it looks like the name of the control ("mycontrolname.ascx" or whatever) is stored in the AppSettings file.
Once the control is loaded it can be added to the page and in the case you've given, added to a panel control named pnlContainer
An expanded version of your code might look like:
// 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);
A control which is added to the settings ? (Web.config)
The point here is that the UserControl is only needed in certain situations. Instead of loading the control all the time when the page loads, it is only added when it is needed. It's added dynamically.
精彩评论