If you have designed SharePoint 2010 vintage web parts before, you know that you can easily determine customized made web element qualities which your web element code-behind can eat.
With the reputation of SharePoint 2010 Graphic Webpart, you are probably interested in having customized made qualities identified there too.
There are few items different about revealing your customized made qualities in a Graphic Web par
Let’s see the steps involved:
Add new or use existing Visual Web Part to your project, in our case called Webpart1
Open the Webpart1.cs file which will look something like this:
01 | [ToolboxItemAttribute( false )] |
02 | public class Webpart1: WebPart |
04 | private const string _ascxPath = @"~/_CONTROLTEMPLATES/[your ASCX].ascx" ; |
05 | protected override void CreateChildControls() |
07 | Control control = Page.LoadControl(_ascxPath); |
08 | Controls.Add(control); |
and add your custom property definition right below the CreateChildControls method, in our case the property will look something like this:
2 | Category( "Configuration" ), |
3 | Personalizable(PersonalizationScope.Shared), |
4 | WebDisplayName( "Friendly Display Name" ), |
5 | WebDescription("Values: Whatever value you need)] |
6 | public string PropertyValue { get ; set ; } |
In here, the PropertyValue is the actual variable which will be later consumed by your web part user control.
- Update your CreateChildControls logic to look like this:
1 | protected override void CreateChildControls() |
3 | Control control = Page.LoadControl(_ascxPath); |
6 | ((Webpart1UserControl)control).WebPart = this ; |
This piece here will pass in the properties every time the control is reloaded. In here, the only change you’ll need to make is to rename Webpart1UserControl to the [name of your webpart]UserControl. This will reference the ASCX used for the Visual Webpart.
- Switch to your user control … Webpart1UserControl.ascx.cs
and define public variable right below the class declaration:
1 | public Webpart1 WebPart { get ; set ; } |
Here the Webpart1 is referencing the actual web part class file (not ASCX).
That’s all, in your code you can get a hold of the web part property value by using this: this.WebPart.PropertyValue, where the PropertyValue is the name of the variable we’ve chosen earlier.
SPONSORS:
No comments:
Post a Comment