Sharepoint 2013: How to add custom tool part

1.5k Views Asked by At

I have a web part containing a repeater control which display the announcements. I want to make a custom tool part which enables a user to limit the number of rows of repeater. I have created a text box that takes the number of news to display as an input. Now I am confused that how do I bind its event with the 'Ok' button i.e. when the button is pressed the code should render the input and make the repeater accordingly. Here is my code:

[WebBrowsable(true),
        WebDisplayName("Number of announcement to display"),
        WebDescription("Controls number of announcement"),
        Category("Content Control"),
        Personalizable(PersonalizationScope.Shared)]
        public int NumberofAnnouncement
        {
            get;
            set;
        } 

How do I bind the event ? I need some assistance.

1

There are 1 best solutions below

4
On

Ok. I'm assuming that you have created a visual webpart and your code is added like this in webpart class.

[ToolboxItemAttribute(false)]
public class addCustomToolPart : WebPart
{
private bool _intNumberofAnnouncement=10;

[WebBrowsable(true),
    WebDisplayName("Number of announcement to display"),
    WebDescription("Controls number of announcement"),
    Category("Content Control"),
    Personalizable(PersonalizationScope.Shared)]
    public int NumberofAnnouncement { get { return _intNumberofAnnouncement; } set { _intNumberofAnnouncement = value; } }
protected override void CreateChildControls()
    {
        webpartusercontrolclass control = (webpartusercontrolclass)Page.LoadControl(_ascxPath);
        control.addCustomToolPart = this;
        Controls.Add(control);
    }
}

Now in the UserControl Class add below code

     public partial class webpartusercontrolclass : UserControl
{
    public wpCustomToolPart addCustomToolPart { get; set; }
    public int  intNumberofAnnouncement { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.intNumberofAnnouncement = addCustomToolPart.NumberofAnnouncement;  
    }
}

Default value for the numberofannouncements will be 10. And can be updated through edit webpart properties. Hope this will help you