Hi All,
In My last post we have seen how to create Custom Web part
properties by using Approach -1 http://sharepointkitchen.blogspot.in/2014/10/add-custom-webpart-properties.html, In a current post we will see how to do the
same using Approach - 2.
1.
Define the Properties in “.cs” file of your
webpart. In our case it is “CustomProp_Method2.cs”
as below.
public String ListName { get; set; }
public String ItemCount { get; set; }
In Our example, I am using 2 web part
properties so I am defining 2 properties here.
2.
Change the webpart class inheritance from
Webpart to Microsoft.SharePoint.WebPartPages.WebPart
public class CustomProp_Method2 : Microsoft.SharePoint.WebPartPages.WebPart
3.
Now , modify the CreateChildControls() as below.
protected override void CreateChildControls()
{
//Control
control = Page.LoadControl(_ascxPath);
CustomProp_Method2UserControl control = (CustomProp_Method2UserControl)Page.LoadControl(_ascxPath);
control.ListName = this.ListName;
control.ItemCount = this.ItemCount;
Controls.Add(control);
}
4.
Override the GetToolParts() Method with return type ToolPart[] class.
public override ToolPart[] GetToolParts()
{
ToolPart[] allToolParts = new ToolPart[2];
WebPartToolPart standardToolParts = new WebPartToolPart();
//It
will show error until we create a class "MyToolPart"
MyToolPart customToolParts = new MyToolPart();
allToolParts[0] =
standardToolParts;
allToolParts[1] = customToolParts;
return allToolParts;
}
5.
Create One Class named as MyToolPart.cs and add
the below code.
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomWebpartProp_M2
{
public class MyToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
{
DropDownList ddlListName;
TextBox txtItemCount;
Panel toolPartPanel;
Label lblPropertyName;
protected override void CreateChildControls()
{
toolPartPanel = new Panel();
ddlListName = new DropDownList();
ddlListName.ID = "ddlType";
//ddlListName.AutoPostBack
= true;
//ddlListName.SelectedIndexChanged
+= ddlListName_SelectedIndexChanged;
txtItemCount = new TextBox();
txtItemCount.ID = "txtItemCount";
txtItemCount.Visible = false;
ddlListName.Items.Insert(0, "Select List Name");
ddlListName.Items.Insert(1, "Top Stories");
ddlListName.Items.Insert(2, "Announcements");
ddlListName.Items.Insert(3, "Events");
ddlListName.Items.Insert(4, "Fun Work");
lblPropertyName = new Label();
lblPropertyName.ID = "lblPropertyTitle";
lblPropertyName.Visible = false;
lblPropertyName.Text = "Please select a list";
toolPartPanel.GroupingText = "SP Kitchen WebPart Properties";
toolPartPanel.Controls.Add(new LiteralControl("<table><tr><td>List
Name:</td></tr></table><br/>"));
toolPartPanel.Controls.Add(ddlListName);
toolPartPanel.Controls.Add(new LiteralControl("<table><tr><td>Item
Count:</td></tr></table><br/>"));
toolPartPanel.Controls.Add(txtItemCount);
toolPartPanel.Controls.Add(lblPropertyName);
Controls.Add(toolPartPanel);
base.CreateChildControls();
}
public override void ApplyChanges()
{
CustomProp_Method2.CustomProp_Method2
_customObj = (CustomProp_Method2.CustomProp_Method2)this.ParentToolPane.SelectedWebPart;
if (ddlListName.SelectedIndex > 0)
{
_customObj.ListName =
ddlListName.SelectedItem.Text;
if (!string.IsNullOrEmpty(txtItemCount.Text))
_customObj.ItemCount =
txtItemCount.Text;
else
_customObj.ItemCount = "5";
}
else
{
lblPropertyName.Visible = true;
lblPropertyName.Text = "Please select a List Name";
}
base.ApplyChanges();
}
}
}
Accessing the Properties in Web part:
Please use the below code to access the web
part properties.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace CustomWebpartProp_M2.CustomProp_Method2
{
public partial class CustomProp_Method2UserControl : UserControl
{
#region
Properties
public string ItemCount { get; set; }
public string ListName { get; set; }
#endregion
#region Declartions
string strListName = string.Empty;
string strItemCount = string.Empty;
#endregion
/// <summary>
/// executes when the page loads and read the custom web part
properties and assigning to labels.
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
SetProperties();
lblListName.Text =
strListName;
lblItemCount.Text = strItemCount;
}
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// Used to set the properties to variables
/// </summary>
public void SetProperties()
{
try
{
if (!string.IsNullOrEmpty(ListName))
{
strListName = ListName;
}
if (!string.IsNullOrEmpty(ItemCount))
{
strItemCount = ItemCount;
}
}
catch (Exception ex)
{
throw;
}
}
}
}
Navigate to Page and Add the Web part which
was developed earlier.
Click on Edit Page and Edit Web Part
Properties.
Scroll down the Property section there you
will find Custom Configuration section (SP Kitchen Web part Properties), here we
need to specify the List Name and Item Count.
Final out Come you can see as below.
Nice post.. Mr Kartikeya
ReplyDeleteNice Article,
ReplyDeleteBut i have one question .
Can we put a multi line text as a custom property?
Thanks Rambabu.. We can use Multi line text box as a property.
ReplyDeleteWe can use all the web controls by using this approach and we can write events also...
Thanks Mr. Kaarthikeya
ReplyDeleteIn Approach -1(previous article) we can't archive the Multi line text box as a property.
In Approach -2 only we can archive the Multi line text box(all web controls) as a property.