Programming Microsoft ASP.NET 4 - Dino Esposito [142]
transformsource="transform.xsl" /> If you’re going to apply some transformation to the XML data, you can also embed it inline between the opening and closing tags of the control. The control also makes it easier to accomplish a common task: apply browser-dependent transformations to portions of the page expressed in an XML meta language. In this case, you exploit the programming interface of the control as follows: In the Page_Load event, you just check the browser capabilities and decide which transformation should be applied: void Page_Load(object sender, EventArgs e) { if (IsInternetExplorer(Request.Browser)) theXml.TransformSource = "ie5.xsl"; else theXml.TransformSource = "downlevel.xsl"; } The PlaceHolder Control After you have a placeholder, you can add controls to it. As mentioned, the placeholder does not add extra functionality, but it provides for grouping and easy and direct identification of a group of related controls. The following code demonstrates how to create a new button and add it to an existing placeholder: Button btn = new Button(); btn.Text = "Click me"; theToolbar.Controls.Add(btn); The PlaceHolder control reserves a location in the control tree and can be extremely helpful in identifying specific areas of the page to customize and extend by adding controls programmatically. Important Note that each control dynamically added to the Controls collection of a parent control is not restored on postback. If the control generates some input elements on the client, the client data is regularly posted but there will be no server-side control to handle that. To avoid this, you must “remember” that you created a certain control dynamically and re-create it while the page loads on postbacks. To remember that a certain control was added to a parent, you can create a custom entry in the view state or use a hidden field. View Controls ... ... ... You change the active view through postback events when the user clicks buttons or links embedded in the current view. To indicate the new view, you can either set the ActiveViewIndex property or pass the view object to the SetActiveView method. Figure 6-6 shows a sample page in action. You select the page from the drop-down list and refresh the view: void Page_Load(object sender, EventArgs e) {
The PlaceHolder control is one of the few controls in the WebControls namespace that isn’t derived from the WebControl class. It inherits from Control and is used only as a container for other controls in the page. The PlaceHolder control does not produce visible output of its own and is limited to containing child controls dynamically added through the Controls collection. The following code shows how to embed a placeholder control in a Web page:
ASP.NET provides two related controls to create a group of interchangeable panels of child controls. The MultiView control defines a group of views, each represented with an instance of the View class. Only one view is active at a time and rendered to the client. The View control can’t be used as a standalone component and can be placed only inside a MultiView control. Here’s an example: