Custom Web Part Basics
August 13, 2007
Submitted By: Bob Mixon - MSD2D SharePoint Community Manager and Managing Director of ShareSquared, Inc.
In last week's newsletter, I began to cover the Windows SharePoint Services 3.0 API. I'm going to continue down this path within the context of Web Parts. First I want to cover the basics of creating a Web Part base class that you can use for all your Web Part development efforts. Because my Web Part base class is somewhat large, I'll need to cover it across a couple of newsletters. In addition, remember this is what I use, and you don't necessarily have to implement it in the same way. The primary goal is to wrap up common custom Web Part needs into my own base Web Part class.
So, le'ts get started by creating our new base Web part class and defining its structure:
Create a new Microsoft Visual Studio 2005 Class Library project. Mine is named S2.Web, but you can name yours anything you wish.
Add a reference to System.Drawing, System.Web and System.Web.UI.WebControls.
Rename Class1 to WebPartBase.
Derive your new WebPartBase from the System.Web.UI.WebControls.WebParts.WebPart class.
Your BaseWebPart class module should look similar to the following:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Web;
namespace S2.Web.UI.WebControls.WebParts
\{
public class WebPartBase :
System.Web.UI.WebControls.WebParts.WebPart
\{
\}
\}
One of the primary goals our base Web Part class provides is the ability to manage error and informational logging and display. This is a common need and should be included in every Web Part you create, so why not place the core code in your base Web Part class.
Next we'll add a Label control, which will be a container for error text. In addition, we'll add a property that indicates whether or not an error has occurred., as the following code shows:
namespace S2.Web.UI.WebControls.WebParts
\{
public class WebPartBase :
System.Web.UI.WebControls.WebParts.WebPart
\{
private Label _errorLabel = null;
protected bool ErrorOccurred
\{
get\{ return( _errorLabel != null ); \}
\}
\}
\}
Next, we'll add the methods our Web Parts can call when an error occurs. I've implemented four methods with different signatures, thus allowing me to easily add error information of different types.
namespace S2.Web.UI.WebControls.WebParts
\{
public class WebPartBase :
System.Web.UI.WebControls.WebParts.WebPart
\{
…
…
protected string LogError( string msg )
\{
LogErrorInternal( msg );
return( msg );
\}
protected string LogError( string module, string proc, string msg )
\{
string imsg = String.Format( "\{0\}.\{1\} : \{2\}", module, proc, msg );
LogErrorInternal( imsg );
return( imsg );
\}
protected string LogError( Exception ex )
\{
string msg = String.Format( "\{0\} : \{1\}", ex.Source, ex.Message );
LogErrorInternal( msg );
return( msg );
\}
protected string LogError( string proc, Exception ex )
\{
string msg = String.Format( "\{0\}.\{1\} : \{2\}",
ex.Source, proc, ex.Message );
LogErrorInternal( msg );
return( msg );
\}
\}
\}
Note that in the above code, I've also made a call to an internal method named LogErrorInternal. This method takes care of creating a Label control to store our error text.
namespace S2.Web.UI.WebControls.WebParts
\{
public class WebPartBase :
System.Web.UI.WebControls.WebParts.WebPart
\{
…
…
private void LogErrorInternal( string msg )
\{
if( _errorLabel
<h1><a name="_null_br_br__errorLabel_new_Label_br__errorLabel_Height_Unit_Percentage_100_br__errorLabel_Width_Unit_Percentage_100_br__errorLabel_ForeColor_System_Drawing_Color_Red_br__errorLabel_Text_msg_br_br_else_br_br__errorLabel_Environment_NewLine_msn_br_br_br_p_p_p_p_The_next_and_last_step_is_to_override_the_OnPreRender_method_nbsp_This_method_is_the_last_event_called_before_Render_in_a_Web_Part_s_rendering_lifecycle_nbsp_It_s_here_that_we_determine_whether_an_error_has_occurred_nbsp_If_we_find_errors_have_been_logged_they_are_displayed_p_p_namespace_S2_Web_UI_WebControls_WebParts_br_br_public_class_WebPartBase_System_Web_UI_WebControls_WebParts_WebPart_br_br_hellip_br_hellip_br_protected_override_void_OnPreRender_EventArgs_e_br_br_if__errorLabel_" id="_null_br_br__errorLabel_new_Label_br__errorLabel_Height_Unit_Percentage_100_br__errorLabel_Width_Unit_Percentage_100_br__errorLabel_ForeColor_System_Drawing_Color_Red_br__errorLabel_Text_msg_br_br_else_br_br__errorLabel_Environment_NewLine_msn_br_br_br_p_p_p_p_The_next_and_last_step_is_to_override_the_OnPreRender_method_nbsp_This_method_is_the_last_event_called_before_Render_in_a_Web_Part_s_rendering_lifecycle_nbsp_It_s_here_that_we_determine_whether_an_error_has_occurred_nbsp_If_we_find_errors_have_been_logged_they_are_displayed_p_p_namespace_S2_Web_UI_WebControls_WebParts_br_br_public_class_WebPartBase_System_Web_UI_WebControls_WebParts_WebPart_br_br_hellip_br_hellip_br_protected_override_void_OnPreRender_EventArgs_e_br_br_if__errorLabel_">null )<br>\{<br>_errorLabel = new Label();<br>_errorLabel.Height = Unit.Percentage(100);<br>_errorLabel.Width = Unit.Percentage(100);<br>_errorLabel.ForeColor = System.Drawing.Color.Red;<br>_errorLabel.Text = msg;<br>\}<br>else<br>\{<br>_errorLabel += (Environment.NewLine + msn);<br>\}<br>\}<br>\}<p>\}</p>
<p>The next, and last step, is to override the OnPreRender method. This method is the last event called before Render in a Web Part's rendering lifecycle. It's here that we determine whether an error has occurred. If we find errors have been logged, they are displayed.</p>
<p>namespace S2.Web.UI.WebControls.WebParts<br>\{<br>public class WebPartBase : System.Web.UI.WebControls.WebParts.WebPart<br>\{<br>…<br>…<br>protected override void OnPreRender( EventArgs e )<br>\{<br>if( _errorLabel</p>
</a></h1>
null )
\{
base.OnPreRender(e);
\}
else
\{
this.Controls.Clear();
this.Controls.Add(_errorLabel);
\}
\}
\}
\}
About the Author
You May Also Like