动态加载用户控件

发布于 2024-12-08 10:41:06 字数 3227 浏览 0 评论 0原文

我的代码如下

 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);

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;
        }

    }

该方法基本上在运行时将用户控件复制到虚拟路径。

在 aspx.cs 页面中,我尝试动态加载它。

但我可以看到该文件正在复制到虚拟路径,但是这个。加载控件给我异常说无法加载类型“myCustomUserControl”。

我正在使用 Azure Web 角色
这里出了什么问题?

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

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 .

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'.

I am using azure web role
What is wrong here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-12-15 10:41:06

我解决了这个错误。我只是放在这里供大家参考。

这是一个字的变化 -

http://blog。 kjeldby.dk/2008/11/dynamic-compilation-in-a-web-application/

CodeBehind="myCustomUserControl.ascx.cs"

更改为

CodeFile="myCustomUserControl.ascx.cs"

感谢@Roopesh & @Kristoffer Brinch Kjeldby

它将开始工作。

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/

Change

CodeBehind="myCustomUserControl.ascx.cs"

to

CodeFile="myCustomUserControl.ascx.cs"

Thanks to @Roopesh & @Kristoffer Brinch Kjeldby

and it will start working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文