Recipe 11.4. Creating a Custom Web Part


Problem

You need some functionality for your web parts that you cannot get with user controls or standard ASP.NET server controls, such as the ability to build them into a separate assembly for sharing with other applications.

Solution

Create a class that inherits from the WebPart class and implements the required functionality. Then, use the class with the Web Part control set in the same manner as you would other controls.

Use the .NET language of your choice to:

  1. Create a new class inheriting from the WebPart class.

  2. Override the CreateChildControls method and create the controls required for the web part.

Use the custom web part with the Web Part control set in the same manner as you would any other control (see Recipe 11.1 for details).

The application we have implemented to demonstrate the solution is shown in Examples 11-12, 11-13, 11-14, 11-15 through 11-16. Examples 11-12 (VB) and 11-13 (C#) show the class to implement the custom web part. Examples 11-14, 11-15 through 11-16 show the .aspx and code-behind for a page that demonstrates using the custom web part.

Discussion

For most applications, user controls and standard ASP.NET server controls provide the best option for use as web parts. The primary reasons are their reusability and, in the case of user controls, the efficiency of developing them. On the other hand, developing custom web parts is akin to developing custom server controls (see Chapter 6). Since the entire user interface is generated programmatically, they can be time-consuming to develop and maintain. There are times, however, when a custom web part is the better solution. Table 11-1 lists some of the differences in custom web parts and user controls when used as web parts.

Table 11-1. When to create custom web parts

Feature

Custom web part

User control

Can be built into a separate assembly for sharing with other applications or installation into the GAC

Yes

No

Verbs can be extended

Yes

No

Can be installed in the Visual Studio toolbox

Yes

No

Designer support available in Visual Studio

No

Yes


In our application that implements this solution, we start with the example presented in Recipe 11.1 and add a custom web part by creating a class that inherits from the WebPart class. For purposes of comparison, our custom web part implements the same functionality as the user control we created in Recipe 11.1that is, to display a list of books.

In the class, we implemented a CreateChildControls method that overrides the CreateChildControls method in the WebPart class and creates the controls required for our web part (a GridView and a SqlDataSource).

The first step is to clear the controls collection. This is needed to ensure that only the controls that exist in the web part are the ones created in the CreateChildControls method.

 

Controls.Clear()

Controls.Clear();

The next step is to create the GridView control and set all of the properties required to define the user interface. As with custom server controls, all user interface definition is done programmatically.

 

gvData = New GridView() gvData.AllowPaging = True gvData.AllowSorting = True gvData.AutoGenerateColumns = False gvData.BorderColor = ColorTranslator.FromHtml("#000080") gvData.BorderStyle = WebControls.BorderStyle.Solid gvData.BorderWidth = 2 gvData.Caption = "" gvData.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.Width = 600 gvData.PageSize = 5 gvData.PagerSettings.Mode = PagerButtons.Numeric gvData.PagerSettings.PageButtonCount = 5 gvData.PagerSettings.Position = PagerPosition.Bottom gvData.PagerStyle.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.PagerStyle.CssClass = "pagerText" gvData.HeaderStyle.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.HeaderStyle.CssClass = "tableHeader" gvData.RowStyle.CssClass = "tableCellNormal" gvData.AlternatingRowStyle.CssClass = "tableCellAlternating"

gvData = new GridView(); gvData.AllowPaging = true; gvData.AllowSorting = true; gvData.AutoGenerateColumns = false; gvData.BorderColor = ColorTranslator.FromHtml("#000080"); gvData.BorderStyle = BorderStyle.Solid; gvData.BorderWidth = 2; gvData.Caption = ""; gvData.HorizontalAlign = HorizontalAlign.Center;

gvData.Width = 600; gvData.PageSize = 5; gvData.PagerSettings.Mode = PagerButtons.Numeric; gvData.PagerSettings.PageButtonCount = 5; gvData.PagerSettings.Position = PagerPosition.Bottom; gvData.PagerStyle.HorizontalAlign = HorizontalAlign.Center; gvData.PagerStyle.CssClass = "pagerText"; gvData.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; gvData.HeaderStyle.CssClass = "tableHeader"; gvData.RowStyle.CssClass = "tableCellNormal"; gvData.AlternatingRowStyle.CssClass = "tableCellAlternating";

Next, you need to create the columns in the GridView, set the column properties, and add the columns to the GridView. The properties that need to be set include the data field used to populate the column, the header text, the sort expression, and optionally any formatting and styles.

 

gridColumn = New BoundField() gridColumn.DataField = "Title" gridColumn.HeaderText = "Title" gridColumn.SortExpression = "Title" gvData.Columns.Add(gridColumn) gridColumn = New BoundField() gridColumn.DataField = "PublishDate" gridColumn.HeaderText = "Publish Date" gridColumn.SortExpression = "PublishDate" gridColumn.DataFormatString = "{0:MMM dd, yyyy}" gridColumn.ItemStyle.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.Columns.Add(gridColumn) gridColumn = New BoundField() gridColumn.DataField = "ListPrice" gridColumn.HeaderText = "List Price" gridColumn.SortExpression = "ListPrice" gridColumn.DataFormatString = "{0:C2}" gridColumn.ItemStyle.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.Columns.Add(gridColumn)

gridColumn = new BoundField(); gridColumn.DataField = "Title"; gridColumn.HeaderText = "Title"; gridColumn.SortExpression = "Title"; gvData.Columns.Add(gridColumn); gridColumn = new BoundField(); gridColumn.DataField = "PublishDate"; gridColumn.HeaderText = "Publish Date"; gridColumn.SortExpression = "PublishDate"; gridColumn.DataFormatString = "{0:MMM dd, yyyy}"; gridColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center; gvData.Columns.Add(gridColumn);

gridColumn = new BoundField(); gridColumn.DataField = "ListPrice"; gridColumn.HeaderText = "List Price"; gridColumn.SortExpression = "ListPrice"; gridColumn.DataFormatString = "{0:C2}"; gridColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center; gvData.Columns.Add(gridColumn);

In our example, we add an icon in the column header to indicate which is the sorted column and its sort order (see Recipe 2.14 for a full explanation of this technique). To do this, we must "wire" the event handler for the RowCreated event to our event handler method (gvData_RowCreated).

 

AddHandler gvData.RowCreated, AddressOf gvData_RowCreated

gvData.RowCreated += new GridViewRowEventHandler(gvData_RowCreated);

At this point, the GridView is complete and needs to be added to the Controls collection of our web part:

 

Me.Controls.Add(gvData)

this.Controls.Add(gvData);

Now, you need to create the SqlDataSource control that will retrieve the book data from a database:

 

dSource = New SqlDataSource() dSource.ConnectionString = ConfigurationManager. _ ConnectionStrings("dbConnectionString").ConnectionString dSource.DataSourceMode = SqlDataSourceMode.DataSet dSource.ProviderName = "System.Data.OleDb" dSource.SelectCommand = "SELECT Title, PublishDate, ListPrice " & _ "FROM Book " & _ "ORDER BY Title" dSource.ID = "dSource"

dSource = new SqlDataSource(); dSource.ConnectionString = ConfigurationManager. ConnectionStrings["dbConnectionString"].ConnectionString; dSource.DataSourceMode = SqlDataSourceMode.DataSet; dSource.ProviderName = "System.Data.OleDb"; dSource.SelectCommand = "SELECT Title, PublishDate, ListPrice " + "FROM Book " + "ORDER BY Title"; dSource.ID = "dSource";

The next step is to connect the GridView and the SqlDataSource to populate the GridView from the SqlDataSource during data binding. This is done by setting the DataSourceID of the GridView to the ID of the SqlDataSource.

 

gvData.DataSourceID = dSource.ID

gvData.DataSourceID = dSource.ID;

The GridView's built-in sorting and paging will work only if the DataSourceID property of the GridView is set to the ID of the data source. If the DataSource property is used instead, you will have to implement all of the same event handlers that are required to perform sorting and paging with a DataGrid (see Recipe 2.11).


As with the GridView, you need to add the SqlDataSource to the Controls collection of the web part:

 

Me.Controls.Add(dSource)

this.Controls.Add(dSource);

By default, the SortExpression property of the GridView is set to an empty string, and the SortDirection property is set to Ascending. To set the sort column and sort order to meet your needs, you will need to call the Sort method of the GridView, passing the desired sort expression and sort order in the process. This is required since the SortExpression and SortDirection properties are read-only and cannot be set directly. In our application, we set the initial sort column to the Title column and the sort order to Ascending.

 

If (gvData.SortExpression.Length = 0) Then gvData.Sort(gvData.Columns(0).SortExpression, _ SortDirection.Ascending) End If

if (gvData.SortExpression.Length == 0) { gvData.Sort(gvData.Columns[0].SortExpression, SortDirection.Ascending); }

The CreateChildControls method is executed when the page using the web part is initially displayed, as well as when postbacks occur. When a postback occurs as a result of a sort request, the SortExpression is set and is required to sort the data by the column the user clicked. In this scenario, the call should not be made to the Sort method in your code. If the call is made, the sorting will not work correctly.


Now that you have the custom web part implemented, you need to use it in a page, which is done in the same manner as standard controls and user controls (see Recipes 11.1 and 11.2). In our example, we replaced the book data user control in the <WebPartsTemplate> element of the DeclarativeCatalogPart with our custom web part.

 

<asp:CatalogZone runat="server" EmptyZoneText="No Catalog Items" HeaderCloseVerb-Visible="false" Css Padding="6" >

<ZoneTemplate> <asp:DeclarativeCatalogPart runat="server" Title="Available Parts" > <WebPartsTemplate> <ASPNetCookbook:CvilleWeather runat="server" Title="Weather" /> <asp:Calendar runat="server" Title="Calendar" /> <ASPNetCookbook:CH11CustomWebPartVB runat="server" Title="Book Data" /> </WebPartsTemplate> </asp:DeclarativeCatalogPart> </ZoneTemplate> </asp:CatalogZone>

<asp:CatalogZone runat="server" EmptyZoneText="No Catalog Items" HeaderCloseVerb-Visible="false" Css Padding="6" > <ZoneTemplate> <asp:DeclarativeCatalogPart runat="server" Title="Available Parts" > <WebPartsTemplate> <ASPNetCookbook:CvilleWeather runat="server" Title="Weather" /> <asp:Calendar runat="server" Title="Calendar" /> <ASPNetCookbook:CH11CustomWebPartCS runat="server" Title="Book Data" /> </WebPartsTemplate> </asp:DeclarativeCatalogPart> </ZoneTemplate> </asp:CatalogZone>

This recipe provides a simple example of a custom web part. Refer to Recipe 11.4, Recipe 11.5, and the WebPart class documentation in the MSDN Library for more information on other things you can do with custom web parts.

See Also

Recipes 2.11, 2.14, 11.1, 11.2, 11.4, 11.5, and the WebPart documentation in the MSDN Library

Example 11-12. Custom web part control (.vb)

 Option Explicit On  Option Strict On Imports System.Drawing  Imports System.Configuration Imports System.Data  Imports System.Data.SqlClient Namespace ASPNetCookbook.VBExamples  ''' <summary>  ''' This class provides a custom web part that displays book data  ''' </summary>  Public Class CH11CustomWebPartVB Inherits WebPart Private gvData As GridView '''*********************************************************************** ''' <summary> ''' This routine creates the child controls (GridView and SqlDataSource) ''' for the web part ''' </summary> Protected Overrides Sub CreateChildControls() Dim gridColumn As BoundField Dim dSource As SqlDataSource 'clear the controls collection Controls.Clear() 'create the GridView control and set the applicable properties 'to display the book data gvData = New GridView() gvData.AllowPaging = True gvData.AllowSorting = True gvData.AutoGenerateColumns = False gvData.BorderColor = ColorTranslator.FromHtml("#000080") gvData.BorderStyle = WebControls.BorderStyle.Solid gvData.BorderWidth = 2 gvData.Caption = "" gvData.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.Width = 600 gvData.PageSize = 5 gvData.PagerSettings.Mode = PagerButtons.Numeric gvData.PagerSettings.PageButtonCount = 5 gvData.PagerSettings.Position = PagerPosition.Bottom gvData.PagerStyle.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.PagerStyle.CssClass = "pagerText" gvData.HeaderStyle.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.HeaderStyle.CssClass = "tableHeader" gvData.RowStyle.CssClass = "tableCellNormal" gvData.AlternatingRowStyle.CssClass = "tableCellAlternating" 'create the columns in the GridView gridColumn = New BoundField() gridColumn.DataField = "Title" gridColumn.HeaderText = "Title" gridColumn.SortExpression = "Title" gvData.Columns.Add(gridColumn) gridColumn = New BoundField() gridColumn.DataField = "PublishDate" gridColumn.HeaderText = "Publish Date" gridColumn.SortExpression = "PublishDate" gridColumn.DataFormatString = "{0:MMM dd, yyyy}" gridColumn.ItemStyle.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.Columns.Add(gridColumn) gridColumn = New BoundField() gridColumn.DataField = "ListPrice" gridColumn.HeaderText = "List Price" gridColumn.SortExpression = "ListPrice" gridColumn.DataFormatString = "{0:C2}" gridColumn.ItemStyle.HorizontalAlign = WebControls.HorizontalAlign.Center gvData.Columns.Add(gridColumn) 'add the event handler for the GridView RowCreated event AddHandler gvData.RowCreated, AddressOf gvData_RowCreated 'add the GridView to the custom web part controls Me.Controls.Add(gvData) 'create a SQL data source and set its properties to get the book data  'from a database       dSource = New SqlDataSource()  dSource.ConnectionString = ConfigurationManager. _ ConnectionStrings("dbConnectionString").ConnectionString  dSource.DataSourceMode = SqlDataSourceMode.DataSet  dSource.ProviderName = "System.Data.OleDb"  dSource.SelectCommand = "SELECT Title, PublishDate, ListPrice " & _ "FROM Book " & _ "ORDER BY Title" dSource.ID = "dSource" 'set the data source ID for the GridView  'NOTE: The DataSourceID must be used instead of the DataSource if the  '    automatic sorting/paging in GridView are to be used.  gvData.DataSourceID = dSource.ID 'add the SQL data source to the custom web part controls Me.Controls.Add(dSource) 'perform the initial sort on the first column in ascending order  'if no sort expression is currently set (this is the case when the  'control is initially created)  If (gvData.SortExpression.Length = 0) Then gvData.Sort(gvData.Columns(0).SortExpression, _  SortDirection.Ascending) End If End Sub 'CreateChildControls '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the GridView's row created ''' event. It is responsible for setting the icon in the header row to ''' indicate the current sort column and sort order ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Protected Sub gvData_RowCreated(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)  Dim index As Integer  Dim col As DataControlField = Nothing  Dim image As HtmlImage = Nothing If (e.Row.RowType = DataControlRowType.Header) Then  'loop through the columns in the gridview updating the header to  'mark which column is the sort column and the sort order  For index = 0 To gvData.Columns.Count - 1 col = gvData.Columns(index) 'check to see if this is the sort column If (col.SortExpression.Equals(gvData.SortExpression)) Then  'this is the sort column so determine whether the ascending or 'descending image needs to be included             image = New HtmlImage()  image.Border = 0  If (gvData.SortDirection = SortDirection.Ascending) Then image.Src = "images/sort_ascending.gif"  Else  image.Src = "images/sort_descending.gif"  End If 'add the image to the column header  e.Row.Cells(index).Controls.Add(image)  End If 'If (col.SortExpression = sortExpression)  Next index  End If 'If (gvData.SortExpression.Equals(String.Empty))  End Sub 'gvData_RowCreated  End Class 'CH11CustomWebPartVB  End Namespace 

Example 11-13. Custom web part control (.cs)

 using System;  using System.Drawing;  using System.Configuration;  using System.Data;  using System.Data.SqlClient; using System.Web.UI.HtmlControls;  using System.Web.UI.WebControls;  using System.Web.UI.WebControls.WebParts; namespace ASPNetCookbook.CSExamples {  /// <summary>  /// This class provides a custom web part that displays book data  /// </summary> public class CH11CustomWebPartCS : WebPart  { private GridView gvData = null; ///*********************************************************************** /// <summary> /// This routine creates the child controls (GridView and SqlDataSource) /// for the web part /// </summary> protected override void CreateChildControls() { BoundField gridColumn; SqlDataSource dSource; // clear the controls collection Controls.Clear(); // 'create the GridView control and set the applicable properties // to display the book data gvData = new GridView(); gvData.AllowPaging = true; gvData.AllowSorting = true; gvData.AutoGenerateColumns = false; gvData.BorderColor = ColorTranslator.FromHtml("#000080"); gvData.BorderStyle = BorderStyle.Solid; gvData.BorderWidth = 2; gvData.Caption = ""; gvData.HorizontalAlign = HorizontalAlign.Center; gvData.Width = 600; gvData.PageSize = 5; gvData.PagerSettings.Mode = PagerButtons.Numeric; gvData.PagerSettings.PageButtonCount = 5; gvData.PagerSettings.Position = PagerPosition.Bottom; gvData.PagerStyle.HorizontalAlign = HorizontalAlign.Center; gvData.PagerStyle.CssClass = "pagerText"; gvData.HeaderStyle.HorizontalAlign = HorizontalAlign.Center; gvData.HeaderStyle.CssClass = "tableHeader"; gvData.RowStyle.CssClass = "tableCellNormal"; gvData.AlternatingRowStyle.CssClass = "tableCellAlternating"; // create the columns in the GridView gridColumn = new BoundField(); gridColumn.DataField = "Title"; gridColumn.HeaderText = "Title"; gridColumn.SortExpression = "Title"; gvData.Columns.Add(gridColumn); gridColumn = new BoundField(); gridColumn.DataField = "PublishDate"; gridColumn.HeaderText = "Publish Date"; gridColumn.SortExpression = "PublishDate"; gridColumn.DataFormatString = "{0:MMM dd, yyyy}"; gridColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center; gvData.Columns.Add(gridColumn); gridColumn = new BoundField(); gridColumn.DataField = "ListPrice"; gridColumn.HeaderText = "List Price"; gridColumn.SortExpression = "ListPrice"; gridColumn.DataFormatString = "{0:C2}"; gridColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center; gvData.Columns.Add(gridColumn); // add the event handler for the GridView RowCreated event gvData.RowCreated += new GridViewRowEventHandler(gvData_RowCreated); // add the GridView to the custom web part controls this.Controls.Add(gvData); // create a SQL data source and set its properties to get the book data  // from a database       dSource = new SqlDataSource();  dSource.ConnectionString = ConfigurationManager. ConnectionStrings["dbConnectionString"].ConnectionString;  dSource.DataSourceMode = SqlDataSourceMode.DataSet;  dSource.ProviderName = "System.Data.OleDb";  dSource.SelectCommand = "SELECT Title, PublishDate, ListPrice " + "FROM Book " + "ORDER BY Title"; dSource.ID = "dSource"; // set the data source ID for the GridView  // NOTE: The DataSourceID must be used instead of the DataSource if the  //  automatic sorting/paging in GridView are to be used.  gvData.DataSourceID = dSource.ID; // add the SQL data source to the custom web part controls this.Controls.Add(dSource); // perform the initial sort on the first column in ascending order  // if no sort expression is currently set (this is the case when the  // control is initially created) if (gvData.SortExpression.Length == 0) { gvData.Sort(gvData.Columns[0].SortExpression, SortDirection.Ascending); } } // CreateChildControls ///*********************************************************************** /// <summary> /// This routine provides the event handler for the GridView's row created /// event. It is responsible for setting the icon in the header row to /// indicate the current sort column and sort order /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void gvData_RowCreated(Object sender,   System.Web.UI.WebControls.GridViewRowEventArgs e) { DataControlField col = null; HtmlImage image = null; if (e.Row.RowType == DataControlRowType.Header) {  // loop through the columns in the gridview updating the header to  // mark which column is the sort column and the sort order  for (int index = 0; index < gvData.Columns.Count; index++)  { col = gvData.Columns[index]; // check to see if this is the sort column  if (col.SortExpression.Equals(gvData.SortExpression))  { // this is the sort column so determine whether the ascending or // descending image needs to be included image = new HtmlImage(); image.Border = 0; if (gvData.SortDirection == SortDirection.Ascending) { image.Src = "images/sort_ascending.gif";  }  else  { image.Src = "images/sort_descending.gif";  } // add the image to the column header e.Row.Cells[index].Controls.Add(image); } // if (col.SortExpression.Equals(gvBooks.SortExpression)) } // for index } // if (e.Row.RowType == DataControlRowType.Header) } //gvData_RowCreated } // CH11CustomWebPartCS  } 

Example 11-14. Using the custom web part (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false"  CodeFile="CH11UsingCustomWebPartVB.aspx.vb"  Inherits="ASPNetCookbook.VBExamples.CH11UsingCustomWebPartVB"  Title="Using a Custom Web Part" %> <%@ Register TagPrefix="ASPNetCookbook" TagName="CvilleWeather"  src="/books/1/505/1/html/2/~/CH11CVilleWeatherVB.ascx" %> <%@ Register TagPrefix="ASPNetCookbook"  Namespace="ASPNetCookbook.VBExamples" Assembly="__Code" %> <asp:Content  runat="server" ContentPlaceHolder>  <div align="center" > Using a Custom Web Part (VB)  </div>  <asp:WebPartManager  runat="server" Personalization-Enabled="true" />  <table width="90%" align="center" border="1" cellpadding="4" cellspacing="0"> <tr> <td align="right"> <asp:LinkButton  runat="server"     Text="Customize"     Css     OnClick="btnCustomize_Click" />&nbsp;&nbsp; <asp:LinkButton  runat="server"    Text="Reset"    Css    OnClick="btnReset_Click" /> </td> </tr> <tr> <td> <asp:WebPartZone  runat="server"    EmptyZoneText="No Content Selected"    Height="10" HeaderText="Zone 1"    LayoutOrientation="Horizontal"    Css    Padding="6" /> </td> </tr> <tr> <td> <asp:WebPartZone  runat="server"    EmptyZoneText="No Content Selected"    Height="10" HeaderText="Zone 2"    LayoutOrientation="Horizontal"    Css    Padding="6" /> </td> </tr> <tr> <td> <asp:CatalogZone  runat="server"    EmptyZoneText="No Catalog Items"    HeaderCloseVerb-Visible="false"    Css    Padding="6" > <ZoneTemplate>  <asp:DeclarativeCatalogPart  runat="server"     Title="Available Parts" >  <WebPartsTemplate> <ASPNetCookbook:CvilleWeather   runat="server"   Title="Weather" /> <asp:Calendar  runat="server"   Title="Calendar" /> <ASPNetCookbook:CH11CustomWebPartVB  runat="server"     Title="Book Data" /> </WebPartsTemplate>  </asp:DeclarativeCatalogPart>  </ZoneTemplate>  </asp:CatalogZone>  </td>  </tr>  </table>  </asp:Content> 

Example 11-15. Using the custom web part code-behind (.vb)

 Option Explicit On  Option Strict On Imports System Namespace ASPNetCookbook.VBExamples  ''' <summary>  ''' This class provides the code-behind for  ''' CH11UsingCustomWebPartVB.aspx  ''' </summary>  Partial Class CH11UsingCustomWebPartVB Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the customize button ''' click event. It is responsible for placing the web part manager ''' in catalog mode. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Protected Sub btnCustomize_Click(ByVal sender As Object, _  ByVal e As System.EventArgs) wpm1.DisplayMode = WebPartManager.CatalogDisplayMode End Sub 'btnCustomize_Click '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the reset button ''' click event. It is responsible for resetting the web part manager ''' personalization data. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Protected Sub btnReset_Click(ByVal sender As Object, _  ByVal e As System.EventArgs)       wpm1.Personalization.ResetPersonalizationState()  End Sub 'btnReset_Click  End Class 'CH11UsingCustomWebPartVB  End Namespace 

Example 11-16. Using the custom web part code-behind (.cs)

 using System;  using System.Web.UI.WebControls.WebParts; namespace ASPNetCookbook.CSExamples {  /// <summary>  /// This class provides the code-behind for  /// CH11UsingRegularContolsAsWebPartsCS.aspx  /// </summary>  public partial class CH11UsingCustomWebPartCS : System.Web.UI.Page  { ///*********************************************************************** /// <summary> /// This routine provides the event handler for the customize button /// click event. It is responsible for placing the web part manager /// in catalog mode. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void btnCustomize_Click(Object sender,   System.EventArgs e) { wpm1.DisplayMode = WebPartManager.CatalogDisplayMode; } // btnCustomize_Click ///*********************************************************************** /// <summary> /// This routine provides the event handler for the reset button /// click event. It is responsible for resetting the web part manager /// personalization data. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void btnReset_Click(Object sender,   System.EventArgs e)  {       wpm1.Personalization.ResetPersonalizationState();  } // btnReset_Click  } // CH11UsingCustomWebPartCS  } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net