The ASP.NET codebehind feature allows you to create Web forms with Managed C++, which makes those forms accessible via the intranet or Internet. In my previous article, I explained how to configure Visual Studio .NET and have it ready for the creation of your first Managed C++ Web form by placing the Web form’s code logic into a codebehind and precompiling it into an assembly. In this article, I will show you how to create the Web form, which I will call MyName in my examples.

The pieces of the Web form
Managed C++ ASP.NET Web forms are always made up of at least two files:

  • ·        The HTML/XML interface file (.aspx)
  • ·        The Managed C++ codebehind (.h)

In general, the HTML/XML interface file provides a definition of the Web interface of the application, while the codebehind provides code logic to support that interface.

In the MyName Web form you will be developing in this article, the HTML/XML interface file called MyName.aspx (see Listing A) contains a simple interface allowing a user to enter their name prefix and name on one panel, and then click a button to have the name regurgitated on another panel with the prefix Hello.

The code in Listing A is standard ASP.NET code. The only noteworthy items are the Assembly directives and Page directives.
<%@ Assembly Name=”Greetings” %>
<%@ Page Inherits=”Greetings.MyNameForm” %>
The Assembly directive’s Name attribute associates a precompiled assembly with this Web form. The Internet Information Services (IIS) will expect to find this assembly in the bin directory off of the Web application’s root virtual directory.

Normally, you’ll find the attribute for the language to be used within the Page directive. However, because Managed C++ isn’t one of the supported imbedded languages, you don’t include the language attribute. The minimal Page directive includes only the Inherits attribute, which associates a specific class out of the assembly with this form.

Let’s continue the example (see Listing B) by examining the Managed C++ codebehind MyName.aspx.h. By convention, it is named the same as the .aspx file with the suffix of .h, but in reality you can name it just about anything you want, as long as you include the linker file in the name so that it can be compiled into a library assembly.

Just like the ASP.NET code from Listing A, Listing B contains standard Managed C++. It begins with references to the following assemblies:
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>
Next, you import the namespaces used by the codebehind:
using namespace System;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;
The namespace System::Web::UI contains classes and interfaces that allow the creation ASP.NET server controls and forms. Since the example uses ASP.NET Web form controls, it imports the namespace System::Web::UI::WebControls. If it were to use HTML server controls, it would need to import the System::Web::UI::HtmlControls namespace. It’s also possible to use both namespaces within the same Web form.

Next, you create the managed class MyNameForm within the namespace of the Greetings element; it is derived from the Page class, which the Web-form-generated class will inherit from:
namespace Greetings {
public __gc class MyNameForm : public Page {
To access the controls in the Web form, you need to create definitions of the controls. Notice that the ID attribute matches the name of the control definition and only the controls you’re accessing are defined:
Panel *pnlName;
DropDownList *ddlPrefix;
TextBox *tbName;
Button *bnClickMe;
Panel *pnlOutput;
Label *lbOutput;
ImageButton *ibnReturn;
The processing of Web Forms and controls is handled by events, which you can access using event handler delegation. The most important Web Form events are:

  • ·        Init—Triggered when the control is first initialized
  • ·        Load—Triggered when controls are loaded onto the form

The Page class provides a handy virtual member call OnInit(), which triggers the Init event. To enable the codebehind to delegate its own event handlers, you need to override this method like this:
void OnInit(EventArgs *e)
bnClickMe->Click += new EventHandler(this, bnClickMe_Click);
ibnReturn->Click += new ImageClickEventHandler(this, ibnReturn_Click);
Load += new EventHandler(this, Page_Load);
Be very careful to remember to call the Page base class version of this method or the Init event will not be triggered for the form.

In this example, the codebehind delegates an event handler to the Input button control click event and Output button control click event. It also delegates a handler to the Load event so that this class can perform its own custom loading of the Web form.

The Load event is triggered every time the Web form is processed. In this example, the Page_Load() method initializes the drop-down list of prefix names and then makes the input panel visible. Since it’s a waste to initialize the drop-down list every time the Web form is loaded, it is done only once by checking the Page class’s IsPostBack property to see if the current load is a post back or the first post, as in this example:
void Page_Load(Object *sender, EventArgs *e) {
if (!IsPostBack) {
String *prefixes [] = { S””, S”Mr”, S”Mrs”, S”Ms”, S”Dr” };
for (int i = 0; i < prefixes->Length; i++) {
ddlPrefix->Items->Add(new ListItem(prefixes[i], prefixes[i]));
} } }
The final two important methods found in the MyNameForm class are the event handlers for the clicking of the Click Me button and the Return image button controls. The logic for the Click Me button copies the selected item from the drop-down box, the input text box, and the prefix Hello into the output label and then makes the output panel visible and the input panel invisible. The Return button makes the panel visible again and resets the input controls back to empty as in this example:
void bnClickMe_Click(Object *sender, EventArgs *e) {
lbOutput->Text = String::Format(S”Hello, {0} {1}”,                                 
void ibnReturn_Click(Object *sender, ImageClickEventArgs *e) {
ddlPrefix->SelectedIndex = 0;tbName->Text = S””;
You need to be able to precompile the codebehind so the Web server can run it. You can do this by adding the codebehind MyName.aspx.h to the linker file Greetings.cpp (Listing C), which was autogenerated with the creation of the project.

Though it’s not really needed because you aren’t handling any application-level events, I’ve included a link to Global.asax.h as well as MyName.aspx.h in Listing C. Now you need to precompile the assembly.

To see if your handywork was successful, press [Ctrl][F5], to compile the Greetings assembly into the bin directory and start the Web form. In the dialog box that appears, enter http://localhost/Greetings/MyName.aspx in the edit box URL Where The Project Can Be Accessed… (Figure A).

Figure A
Executable for debugging session dialog box

Now if all went well, you should see something similar to Figure B in your Explorer Browser before you enter a name.

Figure B
Entering data in the MyName Web form

The screen should show you an output similar to Figure C after you’ve entered a name.

Figure C
Output of the MyName Web form

Everything you need
You now should be able to create your own Web forms using Managed C++ codebehinds. In my next article, I’ll examine how to set up Visual Studio .NET for debugging Managed C++ Web forms.