[英]Loading User Control Dynamically
I have my code as below 我的代码如下
string[] keys = { "myCustomUserControl.ascx", "myCustomUserControl.ascx.cs", "myCustomUserControl.ascx.designer.cs" };
string customUserControlName = CommonDataCalls.GetCustomUserControlName(keys);
UserControl objUserControl = (UserControl)this.LoadControl("~/UserControls/" + userControlName);
userControlPlaceHolder.Controls.Add(objUserControl);
The definition of GetCustomUserControlName is as below GetCustomUserControlName的定义如下
public string GetCustomUserControlName(string[] keys)
{
try
{
string userConrolsPhysicalPtah = System.Web.HttpContext.Current.Server.MapPath("~/UserControls/");
DataTable objDataTable = new DataTable();
foreach (string key in keys)
{
objRequestVO.addObject("ACA_KEY", key);
CResponseVO objResponseVO = (CResponseVO)objGateway.ExecuteBusinessService(CConstant.ADMIN, CConstant.ASSEMBLY_INFO, CConstant.SELECT, objRequestVO);
DataSet objDataSet = (DataSet)objResponseVO.getObject("RES_DS");
cUserTrce objGeneral = new cUserTrce();
if (!objGeneral.IsNullOrEmptyDataset(objDataSet))
{
if (objDataTable.Rows.Count == 0)
{
objDataTable = objDataSet.Tables[0].Clone();
}
objDataTable.Rows.Add(objDataSet.Tables[0].Rows[0].ItemArray);
}
}
if (objDataTable != null && objDataTable.Rows.Count == 3)
{
string containerName = "usercontrols";
foreach (DataRow dr in objDataTable.Rows)
{
string userControlFileBlobUrl = dr["ACA_ASSEMBLY_PATH"].ToString();
string userControlFileName = dr["ACA_CLASS_NAME"].ToString();
Storage.Blob blobHandler = new Storage.Blob();
Stream blobstream = blobHandler.GetBlob(userControlFileBlobUrl, containerName);
if (!(File.Exists(userConrolsPhysicalPtah + userControlFileName)))
{
MemoryStream ms = (MemoryStream)blobstream;
FileStream outStream = File.OpenWrite(userConrolsPhysicalPtah + userControlFileName);
ms.WriteTo(outStream);
outStream.Flush();
outStream.Close();
}
}
string customUserControlName = (from DataRow row in objDataTable.Rows
where row["ACA_KEY"].ToString() == keys[0]
select row["ACA_CLASS_NAME"].ToString()).First();
return customUserControlName;
}
else
{
return null;
}
}
catch
{
return null;
}
}
The mithod basically copies the user controls to the virtual path at run time . 该方法基本上在运行时将用户控件复制到虚拟路径。
In aspx.cs page I try to load it dynamically . 在aspx.cs页面中,我尝试动态加载它。
But I can see the file is getting copied to the virtual path but this. 但是我可以看到文件正被复制到虚拟路径。 Load control gives me exception saying Could not load type 'myCustomUserControl'.
加载控件给我一个例外,提示无法加载类型'myCustomUserControl'。
I am using azure web role 我正在使用Azure Web角色
What is wrong here ? 这是怎么了
I solved the bug . 我解决了这个错误。 I am just putting here for anyone to refer .
我只是在这里供任何人参考。
It's a one word change - 一个字的变化-
http://blog.kjeldby.dk/2008/11/dynamic-compilation-in-a-web-application/ http://blog.kjeldby.dk/2008/11/dynamic-compilation-in-a-web-application/
Change 更改
CodeBehind="myCustomUserControl.ascx.cs" CodeBehind =“ myCustomUserControl.ascx.cs”
to 至
CodeFile="myCustomUserControl.ascx.cs" CodeFile =“ myCustomUserControl.ascx.cs”
Thanks to @Roopesh & @Kristoffer Brinch Kjeldby 感谢@Roopesh和@Kristoffer Brinch Kjeldby
and it will start working. 它将开始工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.