以编程方式从 .Webpart 文件导入 WebForm 网站中的 asp.net webPart(不是 sharePoint 项目)
Importing a asp.net webPart in WebForm website from .Webpart file programmatically (Not a sharePoint Project)
我们已经创建了几个呈现到 Web 部件区域的用户控件 (Ascx)。同样,我们有一个包含在所有 .aspx 页面中实现的 WebPartManager 的控件。
我正在开发一项功能,需要使用文件上传控件在任何页面上导入生成的 .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。
处理方法是:
- 创建一个新的 .WebPart 文件并克隆上传文件的内容。
- Re-direct 到触发 Page_Load 事件的同一页面
- 在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);
}
}
我们已经创建了几个呈现到 Web 部件区域的用户控件 (Ascx)。同样,我们有一个包含在所有 .aspx 页面中实现的 WebPartManager 的控件。
我正在开发一项功能,需要使用文件上传控件在任何页面上导入生成的 .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。
处理方法是:
- 创建一个新的 .WebPart 文件并克隆上传文件的内容。
- Re-direct 到触发 Page_Load 事件的同一页面
- 在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);
}
}