Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [197]

By Root 5559 0

The client HTML form element fits into the ExtraFormContent placeholder.

All in all, nested HTML forms are a nonissue—you just don’t use them. However, a common pitfall in ASP.NET development is that because of master pages you inadvertently end up with nested forms in an attempt to add a second innocent client HTML form.

Multiple Server

Tags on a Page


ASP.NET makes it quite clear: you just can’t have multiple server forms in the same Web page. Given the dynamics of page rendering, an exception is thrown if more than one HtmlForm control attempts to render. (See Figure 9-2.)

Figure 9-2. Using multiple server forms in a page throws a rendering exception.

A little-known fact is that a Web form can actually contain as many server-side forms as needed as long as only one at a time is visible. For example, a page with, say, three tags is allowed, but only one form can be actually rendered. By playing with the Visible property of the HtmlForm class, you can change the active server form during the page lifetime.

This trick doesn’t really let you have multiple active forms at the same time, but it can be helpful sometimes because it allows you to change the active server form over postback events. Let’s consider the following ASP.NET page:

Welcome

OnClick="Button1_Click" />

Step #1

OnClick="Button2_Click" />

OnClick="Button3_Click" />

Finalizing

OnClick="Button4_Click" />

As you can see, all

tags are marked as runat, but only the first one is visible. Mutually exclusive forms were a cool way of implementing wizards in old versions of ASP.NET, before an official wizard control got introduced. By toggling a form’s visibility in button event handlers, you can obtain a wizard-like behavior, as shown in Figure 9-3.

public partial class MultipleForms : System.Web.UI.Page

{

protected void Page_Load(Object sender, EventArgs e)

{

Title = "Welcome";

}

protected void Button1_Click(Object sender, EventArgs e)

{

Title = "Step 1";

step0.Visible = false;

step1.Visible = true;

}

protected void Button2_Click(Object sender, EventArgs e)

{

step0.Visible = true;

step1.Visible = false;

}

protected void Button3_Click(Object sender, EventArgs e)

{

Title = "Finalizing";

step1.Visible = false;

step2.Visible = true;

}

protected void Button4_Click(Object sender, EventArgs e)

{

Title = "Done";

step2.Visible = false;

Response.Write("

Successfully done.

");

}

}

Figure 9-3. Mutually exclusive forms.

Multiple View and Wizards


If you’re targeting ASP.NET 2.0 or newer, you might not need to resort to the preceding trick to switch between forms. You find two new controls—MultiView and Wizard—ready for the job. The MultiView control employs logic nearly identical to that of multiple exclusive forms, except that it relies on panels rather than full forms.

The MultiView control allows you to define multiple and mutually exclusive HTML panels. The control provides an application programming interface (API) for you to toggle the visibility of the various panels and ensure that exactly one is active and visible at a time. The MultiView control doesn’t provide a built-in user interface. The Wizard control is just that—a MultiView control plus some wizard-like predefined user interface (UI) blocks. I’ll cover the Wizard control in great detail later in the chapter.

Cross-Page Postings

Return Main Page Previous Page Next Page

®Online Book Reader