简体   繁体   中英

Sharepoint 2013: How to add custom tool part

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 ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM