【问题标题】:Importing a asp.net webPart in WebForm website from .Webpart file programmatically (Not a sharePoint Project)以编程方式从 .Webpart 文件导入 WebForm 网站中的 asp.net webPart(不是 sharePoint 项目)
【发布时间】:2020-11-30 17:49:38
【问题描述】:

我们创建了几个用户控件 (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);
            }


        }

我可能遗漏了一些基本代码。如果有人知道答案,请提供帮助。谢谢。

【问题讨论】:

    标签: asp.net import webforms devexpress web-parts


    【解决方案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);
    
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2017-05-24
      • 2021-04-13
      • 2011-03-29
      • 2010-10-26
      • 2013-12-01
      • 2011-07-10
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多