0

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.

user1584253
  • 975
  • 2
  • 18
  • 55

1 Answers1

0

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

Manu Singh
  • 414
  • 4
  • 16
  • 1
    I don't understand the need of the user control because the code I have posted is sufficient to add custom tool part. I can set the announcement value, the problem is that when I edit the web part there are 2 buttons i.e. Ok, Apply ... I want to reflect the change to repeater when those buttons are pressed. Right now, I can see the change when I exit the editing mode. – user1584253 Sep 25 '14 at 14:02
  • In SharePoint either you can create visual webpart or webpart. Visual webpart comes with user control as well. But you can use the same code in normal webpart also with some modification. Createchildcontrol is the method where you can get the stored values. If you face an issue post your code here. I'll try to help you out. Writing entire code is not possible :) – Manu Singh Sep 25 '14 at 14:16
  • Brother, I have created a visual webpart. Where is the class that is inheriting usercontrol ? I have a abc.ascx and abc.ascx.cs (which is inheriting WebPart class) and in Page_Load event, I just get the number of announcement. – user1584253 Sep 25 '14 at 14:41
  • IF you are using SharePoint 2013, you will not get that. Still same code can be used in SharePoint 2013 also with some modification. Follow this URL http://www.ashokraja.me/articles/How-to-create-a-custom-property-and-set-a-default-value-in-a-SharePoint-2013-Visual-WebPart – Manu Singh Oct 01 '14 at 06:19