Thursday, 6 September 2012

Creating a custom property in a visual web part

Following are the steps to create a custom property in a visual web part:
·         Open the .cs file of the web part and add the following snippet:
             //Custom property for getting input from the user
    [WebBrowsable(true)]
        [WebDisplayName("Enter the Property Value")]
        [WebDescription("Enter the Property Value")]
        [Personalizable(PersonalizationScope.User)]
        [DefaultValue("Please Configure the Web Part")]
        [Category("Custom Configuration")]

·         Declare a variable for the property as below:

public string stringobjname { get; set; }

·         Change the user control name to web part user control name in the method createChildControls.

Protected override void CreateChildControls ()
        {
            webpartusercontrolname control = (webpartusercontrolname) Page.LoadControl(_ascxPath);
            control.WebPart = this;
            Controls.Add(control);
        }

·         Declare a web part object in the ascx.cs file as

public <WebPartName> <objectname> { get; set; }

·         Get the property value as follows:
String propertyValue=this.<objectname>.<stringobjname>;

No comments:

Post a Comment