Online Book Reader

Home Category

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

By Root 5229 0
on the manager be set to true—which is the default case. If this property is set to false, the UpdatePanel control works like a regular panel.

Populating the Panel Programmatically


The content of an updatable panel is defined through a template property—the ContentTemplate property. Just like any other template property in ASP.NET controls, ContentTemplate can be set programmatically. Consider the following page fragment:

<%-- Left empty deliberately. Will be filled out programmatically --%>

In the PreInit event of the code-behind page, you can set the ContentTemplate programmatically, as shown here:

protected void Page_PreInit(object sender, EventArgs e)

{

// You could also read the URL of the user control from a configuration file

string ascx = "customerview.ascx";

UpdatePanel1.ContentTemplate = this.LoadTemplate(ascx);

}

You are not allowed to set the content template past the PreInit event. However, at any time before the rendering stage, you can add child controls programmatically. In ASP.NET, to add or remove a child control, you typically use the Controls property of the parent control, as shown here:

UpdatePanel1.Controls.Add(new LiteralControl("Test"));

If you try to add a child control programmatically to the Controls collection of an UpdatePanel—as in the preceding code snippet—all that you get is a run-time exception. You should use the ContentTemplateContainer property instead. The reason is that what you really want to do is add or remove controls to the content template, not to the UpdatePanel directly. That’s why Controls doesn’t work and you have to opt for the actual container of the template. The following code shows how to populate the content template programmatically:

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

{

private Label Label1;

protected void Page_Load(object sender, EventArgs e)

{

var updatePanel = new UpdatePanel();

updatePanel.ID = "UpdatePanel1";

// Define the button

var button1 = new Button();

button1.ID = "Button1";

button1.Text = "What time is it?";

button1.Click += new EventHandler(Button1_Click);

// Define the literals

var lit = new LiteralControl("
");

// Define the label

var label1 = new Label();

label1.ID = "Label1";

label1.Text = "[time]";

// Link controls to the UpdatePanel

updatePanel.ContentTemplateContainer.Controls.Add(button1);

updatePanel.ContentTemplateContainer.Controls.Add(lit);

updatePanel.ContentTemplateContainer.Controls.Add(label1);

// Add the UpdatePanel to the list of form controls

this.Form.Controls.Add(updatePanel);

}

protected void Button1_Click(object sender, EventArgs e)

{

Label1.Text = DateTime.Now.ToShortTimeString();

}

}

You can add an UpdatePanel control to the page at any time in the life cycle. Likewise, you can add controls to an existing panel at any time. However, you can’t set the content template programmatically past the page’s PreInit event.

Master Pages and Updatable Regions


You can safely use UpdatePanel controls from within master pages. Most of the time, the use of updatable panels is easy and seamless. There are a few situations, though, that deserve a bit of further explanation.

If you add a ScriptManager control to a master page, partial rendering is enabled by default for all content pages. In addition, initial settings on the script manager are inherited by all content pages. What if you need to change some of the settings (for example, add a new script file or switch on script localization) for a particular content page? You can’t have a new script manager, but you need to retrieve the original one defined on the master page.

In the content page, you can declaratively reference a ScriptManagerProxy and change some of its settings. The proxy retrieves the script manager currently in use and applies changes to it.

The ScriptManagerProxy control, though, is mostly designed to let you edit the list of scripts and services registered

Return Main Page Previous Page Next Page

®Online Book Reader