visual webpart custom properties not working
-
06-10-2020 - |
Question
I am trying to create visual webpart custom properties but it is not appearing.
my code:
<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="VisualWebPartProject2016.R9ToDoWebPart.R9ToDoWebPart, $SharePoint.Project.AssemblyFullName$" />
<importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">My To Do List</property>
<property name="Description" type="string">My Visual Web Part</property>
<property name="RootR9Url" type="string">http://w2k12sp04:44444</property>
</properties>
</data>
</webPart>
</webParts>
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace VisualWebPartProject2016.R9ToDoWebPart
{
[ToolboxItemAttribute(false)]
public class R9ToDoWebPart : WebPart
{
private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/VisualWebPartProject2016/R9ToDoWebPart/R9ToDoWebPartUserControl.ascx";
private string _r9Url = String.Empty;
[WebBrowsable(true),
WebDisplayName("RMT+ rooturl"),
WebDescription("RMT+ rooturl"),
Personalizable(PersonalizationScope.Shared),
Category("RmtPlus")]
public string RootR9Url
{
get
{
return _r9Url;
}
set
{
_r9Url = value;
}
}
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
Controls.Add(control);
((R9ToDoWebPartUserControl)control).RootUrl = RootR9Url;
}
}
}
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace VisualWebPartProject2016.R9ToDoWebPart
{
public partial class R9ToDoWebPartUserControl : UserControl
{
private string _rootUrl = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
}
public string RootUrl
{
get
{
return _rootUrl;
}
set
{
_rootUrl = value.TrimEnd(new char[]{'/'});
}
}
}
}
Solution
It seems [ToolboxItemAttribute(false)]
is missing on your partial class. Also ordering may be a issue but I am not sure about that. I am giving you a fully functional code snippet where I implemented webpart custom properties. Please update your code as per my below code. Let me know whether it works or not.
using System;
using System.ComponentModel;
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace Survey.WP_Survey {
[ToolboxItemAttribute(false)]
public partial class WP_Survey: WebPart {
private string _r9Url = String.Empty;
[WebBrowsable(true)]
[WebDescription("RMT+ rooturl")]
[WebDisplayName("RMT+ rooturl")]
[Personalizable(PersonalizationScope.Shared)]
public string RootR9Url {
get {
return _r9Url ;
}
set {
_r9Url = value;
}
}
public WP_Survey() {}
protected override void OnInit(EventArgs e) {
base.OnInit(e);
InitializeControl();
}
protected void Page_Load(object sender, EventArgs e) {
try {
CreateQuestion();
} catch (Exception ex) {}
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange