Question
-http://www.dotnetspark.com/DotNetQuestion/index.aspx?cate=9
Type Of Web Part:-
1-ASP.NET Web Parts -
The ASP.NET-style Web Parts have a dependency on [System.Web.dll]
and must inherit from the WebPart base class in the [System.Web.UI.WebControls.WebParts]
namespace.
Note:-
If you are creating your Web Part specifically for a
SharePoint site, and it will consume the SharePoint Foundation object model,
you can derive from the ASP.NET System.Web.UI.WebControls.WebParts.WebPart base
class and add a reference to the SharePoint object model in your project.
2-SharePoint-based Web Parts — These Web Parts have a
dependency on [Microsoft.SharePoint.dll ] and must inherit from the
WebPart base class in the [Microsoft.SharePoint.WebPartPages] namespace.
These Web Parts can only be used in SharePoint websites.
Custom Web Parts :-
“Create user interface elements that support both
customization and personalization.”
Customization :-Changes are seen by all site members
Personalization. changes are seen only by the user who made
them.
Custom Properties in SharePoint 2010 Visual Web Parts :-
Basic steps to add a custom property are;
1-Add a custom property in “VisualWebPart1.cs” Web Part
class
2-Make above custom property visible to
“VisualWebPart1UserControl.ascx” User Control
3-Access custom property from the User Control
Custom properties Of different controls
1-bool -> Check
box
2-enum ->
Dropdown list
3-int -> Text box
4-string -> Text
box
5-DateTime -> Text box
1. Add a custom property in “VisualWebPart1.cs” Web Part
class
My custom property will be an enum value to select control
mode between Simple, Standard and
Advanced.
public enum ControlModes
{
Simple,
Standard,
Advanced
}
/// <summary>
/// This property will be displayed as a drop-down list in
the property pane.
/// </summary>
[DefaultValue(ControlModes.Standard)]
[Description("Select a category from the dropdown
list.")]
[WebBrowsable(true)]
[Personalizable()]
public ControlModes ControlMode
{
get; set;
}
2. Make custom property visible to
“VisualWebPart1UserControl.ascx” User Control
Here we need to change “CreateChildControls” method
implementation of the “VisualWebPart1.cs”
class to pass the value of our custom property to the User
Control. Here if we pass the
WebPart class itself as a property, we can simply access
several custom properties by less
code.
User Control Property
public VisualWebPart1 WebPartControl { get; set; }
“CreateChildControls” in “VisualWebPart1.cs”
protected override void CreateChildControls()
{
VisualWebPart1UserControl control =
Page.LoadControl(_ascxPath) as
VisualWebPart1UserControl;
if (control != null)
{
control.WebPartControl = this;
}
Controls.Add(control);
}
3. Access custom property from the User Control
public VisualWebPart1.ControlModes ControlMode
{
get
{
return
this.WebPartControl.ControlMode;
}
}
Once deployed and added this Web Part to a page, we can go
to Web Part properties by selecting “Edit Web Part” option.
We can see our custom property in the Property Pane under “Miscellaneous”
section. Description attribute we added can be seen as a tooltip here.
Here is my complete code for “VisualWebPart1.cs” Web Part
class and “VisualWebPart1UserControl.ascx” User Control.
VisualWebPart1.cs
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace CustomProperty.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
// Visual Studio
might automatically update this path when you change the
// Visual Web Part
project item.
private const
string _ascxPath =
@"~/_CONTROLTEMPLATES/CustomProperty/VisualWebPart1/
VisualWebPart1UserControl.ascx";
public enum
ControlModes
{
Simple,
Standard,
Advanced
}
///
<summary>
/// This property
will be displayed as a drop-down list in the
/// property pane.
///
</summary>
[DefaultValue(ControlModes.Standard)]
[Description("Select a category from the dropdown list.")]
[WebBrowsable(true)]
[Personalizable()]
public
ControlModes ControlMode
{
get;
set;
}
protected override
void CreateChildControls()
{
VisualWebPart1UserControl control = Page.LoadControl(_ascxPath) as
VisualWebPart1UserControl;
if (control !=
null)
{
control.WebPartControl = this;
}
Controls.Add(control);
}
}
}
VisualWebPart1UserControl.ascx
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace CustomProperty.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
public
VisualWebPart1 WebPartControl { get; set; }
public VisualWebPart1.ControlModes
ControlMode
{
get
{
if
(this.WebPartControl != null)
{
//
User selected value.
return
this.WebPartControl.ControlMode;
}
else
{
// Default value.
return
VisualWebPart1.ControlModes.Standard;
}
}
}
protected void
Page_Load(object sender, EventArgs e)
{
}
}
}
Benifit custom Web Parts:-
1-Create custom properties: - that you can display
and modify in the user interface.
2-Improve performance and scalability:-. A compiled custom Web Part runs faster than
a script.
3-Implement proprietary code :- without disclosing the source code.
4-Secure and control access to content:- within the Web Part. Built-in Web Parts
enable any users who have the appropriate permissions to change content and
alter Web Part functionality. With a custom Web Part, you can determine the
content or properties to display to users, regardless of their permissions.
5-Make your Web Part connectable:-, allowing Web Parts to provide or access data
from other connectable Web Parts.
6-Interact with object models :- that are exposed in SharePoint Foundation.
For example, you can create a custom Web Part to save documents to a SharePoint
Foundation document library.
7-Control the Web Part cache :- by using built-in cache tools. For example,
you can use these tools to specify when to read, write, or invalidate the Web
Part cache.
8-Benefit from a rich development environment:- with debugging features that are provided
by tools such as Microsoft Visual Studio 2010.
9-Create a base class for other Web Parts to extend.:-For
example, to create a collection of Web Parts that have similar features and
functionality, create a
10-custom base class :- from which multiple Web Parts can inherit. This reduces the
overall cost of developing and testing subsequent Web Parts.
11-Control Web Part implementation. For example, you
can write a custom server-side Web Part that connects to a back-end database,
or you can create a Web Partthat is compatible with a broader range of Web
browsers.
Connectable Web Parts :-
"Standardized set of interfaces called connection
interfaces that allow Web Parts to exchange information with one another at run
time."
For example, the List Web Part that is built into
SharePoint Foundation can provide (send) a row of data to any other Web Part
that can consume (receive) that row, such as a Web Part that implements a form
to display the row.
No comments:
Post a Comment