One Option to Writing a Base Web User Control

Technorati Tags:

Today I was looking into writing a base web user control to act as an abstract class for other controls. The reason is I had several forms I needed to build and all share the same set of arguments. I looked at creating an interface, but it didn’t cut it for me.  See this reference for more on choosing abstract classes versus interfaces – http://msdn.microsoft.com/en-us/library/scsyfw1d(VS.71).aspx 

Here’s an example of what I was trying to do.

1. Create my base user control. Notice the use of a Virtual method. This will allow me to override the method in my derived class without needing an abstract access modifier declaration on the class itself. Abstract modifiers are not supported on partial classes.

   1: <%@ Control Language="C#" AutoEventWireup="true" 
   2:     CodeBehind="BaseUserControl.ascx.cs" 
   3:     Inherits="Webeccentric.web.UserControls.BaseUserControl" %>
   1: namespace Webeccentric.web.UserControls
   2: {
   3:     public partial class BaseUserControl : System.Web.UI.UserControl
   4:     {
   5:         int _reviewMonth;
   6:         int _reviewYear;
   7:  
   8:         public int ReviewMonth { get { return _reviewMonth; } set { _reviewMonth = value; } }
   9:         public int ReviewYear { get { return _reviewYear; } set { _reviewYear = value; } }
  10:  
  11:         public virtual void Save(){}
  12:     }
  13: }

2. Create a user control that will inherit from my base user control

   1: <%@ Control Language="C#" AutoEventWireup="true" 
   2:     CodeBehind="Review.ascx.cs" 
   3:     Inherits="Webeccentric.web.UserControls.Review" %>
   4:     <div>
   5:         <asp:Label ID="lblMonth" runat="server" Text="" />
   6:     </div>
   7:     <div>
   8:         <asp:Label ID="lblYear" runat="server" Text="" />
   9:     </div>
  10:     <div>
  11:         <asp:Button id="btnSubmit" runat="server" Text="Click Me" 
  12:             onclick="btnSubmit_Click" />
  13:     </div>
   1: namespace Webeccentric.web.UserControls
   2: {
   3:     public partial class Review : BaseUserControl
   4:     {
   5:         protected override void OnLoad(EventArgs e)
   6:         {
   7:             lblMonth.Text = ReviewMonth.ToString();
   8:             lblYear.Text = ReviewYear.ToString();
   9:         }
  10:  
  11:         public override void Save()
  12:         {
  13:             //  Implement the Save operation here
  14:             //  ...
  15:         }
  16:  
  17:         protected void btnSubmit_Click(object sender, EventArgs e)
  18:         {
  19:             Save();
  20:         }
  21:     }
  22: }

3. Create a Page to test the concept and load the control in it

   1: <form id="form1" runat="server">
   2: <div>
   3:     <asp:Panel ID="pnlUserControl" runat="server"></asp:Panel>
   4: </div>
   5: </form>
   1: public partial class _Default : System.Web.UI.Page
   2:     {
   3:         protected void Page_Load(object sender, EventArgs e)
   4:         {
   5:  
   6:         }
   7:  
   8:         protected override void CreateChildControls()
   9:         {
  10:             base.CreateChildControls();
  11:  
  12:             //  Load custom user control with some initial values
  13:             Review review = (Review)Page.LoadControl(@"/UserControls/Review.ascx");
  14:             review.ReviewMonth = 10;
  15:             review.ReviewYear = 2010;
  16:  
  17:             pnlUserControl.Controls.Add(review);
  18:         }
  19:     }

4. That’s it. This should work now.

Hope this helps.

Advertisements

About jharbieh

I'm an IW Solutions Architect with background in requirements gathering, planning, design, architecture, and development (not necessarily in the right order). Currently, my focus is on the Microsoft Cloud, Productivity and Collaboration space. Hope you enjoy what I write about here. Thanks for visiting. Johnny Harbieh
This entry was posted in General ASP.NET Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s