以编程方式从.Webpart文件在WebForm网站中导入asp.net WebPart不是sharePoint项目

问题描述

我们已经创建了多个用户控件(Ascx),并将其渲染到Webpart区域。同样,我们有一个包含WebPartManager的控件,该控件已在所有.aspx页中实现。

我正在开发一项功能,该功能需要使用文件上传控件在任何页面上导入生成的.webpart文件。 我正在使用devExpress fileUpload控件,并且在FileUploadComplete事件执行以下代码时。 该代码可以正常运行,但在指定区域中也不会显示任何Web部件。这是问题所在。

 protected void WebPartUploadControl_FileUploadComplete(object sender,FileUploadCompleteEventArgs e)
        {
            string ErrorMessge = string.Empty;
            if (e.UploadedFile.IsValid)
            {
                var xmlReader = XmlReader.Create(e.UploadedFile.FileContent);
                xmlReader.Read();

                System.Web.UI.WebControls.WebParts.WebPart webPart = wpManager.ImportWebPart(xmlReader,out ErrorMessge);

                       wpManager.AddWebPart(webPart,wpManager.Zones["Zone5"],1);
            }


        }

我可能缺少一些基本代码。如果有人知道答案,请帮忙。谢谢。

解决方法

我终于弄清楚了这个问题。万一将来有人遇到类似问题,这将有所帮助。

因此,这里的问题是Devexpress File-Upload控件是一个ajax控件,它进行了部分回发,因此不会更新超出其范围的页面的CatalogZone。

处理方法是:

  1. 创建一个新的.WebPart文件并克隆上载文件的内容。
  2. 重定向到引发Page_Load事件的同一页面
  3. 在page_Load事件中执行以上代码,以便导入。

下面是解释这一点的代码:

WebPartUploadControl_FileUploadComplete

        protected void WebPartUploadControl_FileUploadComplete(object sender,FileUploadCompleteEventArgs e)
        {

String WebPartFilePath = Server.MapPath("DirectoryWhereYouWantTosaveCloneFile");
            String WebPartFileName = "NameOfYourCloneFile.WebPart";
            
            string FileContent = string.Empty;

            Creating Directory to store data of uploaded file(.webPart).

            Session["ImportWebPartFilePath"] = $"{WebPartFilePath}/{WebPartFileName}";
            if (!Directory.Exists(WebPartFilePath))
            {
                Directory.CreateDirectory(WebPartFilePath);
            }

            Reading Uploaded file Data

            using (StreamReader sr = new StreamReader(e.UploadedFile.FileContent))
            {
                FileContent = sr.ReadToEnd();
            }

            //Copying File Data to the newly Created file 

            if (!File.Exists(Session["ImportWebPartFilePath"].ToString()))
            {
                File.AppendAllText(WebPartFilePath + "/" + WebPartFileName,FileContent);
            }

            e.CallbackData = "Page Settings Imported Successfully.";

            // Response.Redirect(Request.RawUrl) does not work in while ajax callback in 
               devexpress
            // Creating a callback to current page to trigger Page_Load event.

            DevExpress.Web.ASPxWebControl.RedirectOnCallback(this.Request.Path);
}

Page_Load

if (Session["ImportWebPartFilePath"] != null)
            {
                //Import Webpart Settings
                ImportWebPartsToCurrentPage(Session["ImportWebPartFilePath"].ToString());
                File.Delete(Session["ImportWebPartFilePath"].ToString());
                Session["ImportWebPartFilePath"] = null;

            }

ImportWebPartsToCurrentPage

 private void ImportWebPartsToCurrentPage(String FilePath)
        {
            string ErrorMessge = string.Empty;
            //Extracting All WebParts in the file
            XDocument WebPartXml = XDocument.Load(FilePath);

            //Spliting the each webpart.
            var WebPartDescriptions = WebPartXml.Root.Elements();
            try
            {
                foreach (var WebPartDescription in WebPartDescriptions)
                {
                    var xmlReader = XmlReader.Create(new StringReader(WebPartDescription.ToString()));
                    xmlReader.Read();
                    // Adding Webpart to page Catalog.
                    System.Web.UI.WebControls.WebParts.WebPart webPart = wpManager.ImportWebPart(xmlReader,out ErrorMessge);
                    //Adding webpart to the page.
                    if (!wpManager.WebParts.Contains(webPart))
                    {
                        wpManager.AddWebPart(webPart,wpManager.Zones["ZoneName"],0);
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex);

            }
        }

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...