Monday, February 22, 2010

Creating Custom properties for web parts in Web user Control for smart part templates

Creating Custom properties for web parts in Web user Control for smart part templates

Hi friends ,
As I have gone through lot of search for creating custom properties for web user control which is used in smart part template as a custom web part but unfortunately there is no such help I found. So here I am giving some of the tips by which any one can under stand the implementation of custom properties in web user control.
I am considering that you know how to use smart part template and point to the web user control

Steps for creating properties.

1. Create the local variable required for custom properties

public partial class UCContactList : System.Web.UI.UserControl
{
#region For local varibles
//for names
///



/// Used for storing first contact info.
///

private string firstContact = string.Empty;
#endregion

2.Create the custom property with required attributes.

//properties for name
[Browsable(true), //Display the property in property pane
Category("Coolsoft Details"), //Create a Customer Details category in property pane
DefaultValue(false), //Assign a default value
WebPartStorage(Storage.Personal),//Make available in both personal and shared mode
FriendlyName(" First Contact"), //The caption display in property pane
Description(" First Contact Name")]//The tool tip
public string FirstConatactName
{
get
{
return firstContact;
}
set
{
firstContact = value;
}
}


3. Use the custom property in page load method for assigning any control.. in my case I done like this..

protected void Page_Load(object sender, EventArgs e)
{

//for first contact
this.hlFirstContact.Text = this.FirstConatactName
}

that's very easy..

No comments: