Reputation: 4695
I am loading a dynamically specified user control into a place-holder in the parent (user) control like this:
// dynamically load instance of chosen chart control
string chartClassName = ConfigurationManager.AppSettings["ChartControlClass"];
_chartControl = (IOutputChart)LoadControl(chartClassName + ".ascx");
// add to place-holder
if (chartClassName == "OutputChart_Dundas") phChart.Controls.Add((OutputChart_Dundas)_chartControl);
else if (chartClassName == "OutputChart_Microsoft") phChart.Controls.Add((OutputChart_Microsoft)_chartControl);
else if (chartClassName == "OutputChart_Telerik") phChart.Controls.Add((OutputChart_Telerik)_chartControl);
Clearly it would be nicer not to have to explicitly cast the _chartControl
variable each time -- is there a cleaner way? Each user control implements the IOutputChart
interface; however, I can't use that directly because Controls.Add()
expects a Control
object.
Upvotes: 2
Views: 1230
Reputation: 14919
I assume all of your controls derive from Control
base class. Then why don't you cast the _chartControl
to Control
and add it.
_chartControl = (Control)LoadControl(chartClassName + ".ascx");
phChart.Controls.Add(_chartControl);
Upvotes: 2
Reputation: 1500185
Can you not just convert all of these to Control
?
phChart.Controls.Add((Control)_chartControl);
Upvotes: 3