Creating Custom Web Controls in Managed C++, Part 1

A common misunderstanding is that Managed C++ and ASP.NET Web applications are mutually exclusive. Nothing could be further from the truth. Yes, MC++ doesn’t have all the helpful drag-and-drop GUI IDE integration of C# or Visual Basic .NET, but that is not unusual for a C++ developer who always seems to have to do things the hard way.

One area where MC++ and the other .NET languages are on level ground in ASP.NET Web applications is custom Web controls, as there is no fancy IDE integration—just good old hand coding. So, this first installment of a three-part series on creating these custom Web controls in MC++ starts there.

Before you can do any work on custom Web controls, you should probably create an ASP.NET Web Form in which to display them. Custom Web controls don’t work on a standalone basis. Instead, they are implemented within a Web Form. To simplify things, I use a C# Web Form to house the MC++ custom Web controls created in this article. (In a future article, I’ll cover how to develop the actual ASP.NET Web application using only MC++, but if you can’t wait, my book Managed C++ and .NET Development shows you how in great detail.)

Prep Work

Open up Visual Studio .NET 2003 and create a new C# ASP.NET Web Application with the name CustomControls and then change the page layout to FlowLayout. Obviously, you can call it whatever you like in the future, and GridLayout page layout works fine as well.

Figure 1: Create a New C# ASP.NET Web Application Named CustomControls

I’ll use this Web Form shortly, but start working in Managed C++.

There is really nothing magical about custom Web controls. In fact, they are just standard Managed Classes within a .NET Class Library. So, go ahead and add a MC++ .NET Class Library called CustomControlsLib. (I put the library in the same directory structure as the Web application to make things easier to zip up later, but you can place it anywhere.)

Figure 2: Add a MC++ .NET Class Library Called CustomControlsLib

Once I have a Managed C++ .NET Class Library, I usually remove the extra files from the solution (resource files, resource.h, and ReadMe.txt). I find they just clutter up things. I normally would delete the CustomControlsLib.* files as well, but I’m going to leave them because I am going to use them for a little prep work.

The only advantage I see that C# and VB.NET have over MC++ when it comes to custom Web controls is that they offer templates. I level that playing field somewhat by creating a pair of code snippets and then putting them in the General Tab on the toolbox. When you create these snippets, just code them in the appropriate CustomControlLib file and then drag and drop the code onto the general tab of the toolbar. Once the code snippets are in the toolbar, rename them to something more appropriate.

List 1: CustomControlsLib.h

#pragma once

using namespace System;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;
using namespace System::ComponentModel;

namespace CustomControlsLib
   ToolboxData("<{0}:WebCustomControl runat=server></{0}
   public __gc class WebCustomControl :
                     public System::Web::UI::WebControls::WebControl
      String *text;

      Bindable(true), Category("Appearance"), DefaultValue("")]
      __property String *get_Text();

      Bindable(true), Category("Appearance"), DefaultValue("")]
      __property void set_Text(String *value);

      void Render(HtmlTextWriter *output);

Listing 2: CustomControlsLib.cpp

#include "stdafx.h"

#include "CustomControlsLib.h"

using namespace CustomControlsLib;

String * WebCustomControl::get_Text()
   return text;

void WebCustomControl::set_Text(String *value)
   text = value;

void WebCustomControl::Render(HtmlTextWriter *output)

Believe it or not, one more step and you have created your first Managed C++ Custom Web Control. Notice above that CustomControlsLib.h references the System::Web::… namespaces. These require the System.Web assembly. Once you add that reference, compile everything.

Done! You now have your first custom Web control under your belt. Now, you need to add it to the CustomControls Web Form.

More by Author

Must Read