Use the PlaceHolder control in ASP. NET

Source: Internet
Author: User

BKJIA recommended: ASP. NET video tutorial

Your ASP. some parts of the user interface of the NET application are often unchanged-for example, some "decorative" parts: GIF images at the header, horizontal toolbar or navigation bar, left-side links, and so on. You can continue to use them without recreating each page.

In the past, a popular ASP technology was to create an include file for each major part of a page, and then create an overall HTML table. The table unit contains each include file. This method is feasible, but not beautiful and prone to errors. Fortunately, ASP. NET allows you to solve this problem in a better way, using a Visual Studio. NET PlaceHolder Web control. This control contains other controls as a collection. You can clear the collection or add it at runtime. For example, you can add a Web user control and a modular Web page at runtime ). I will describe how to combine PlaceHolder Web controls with Web user controls to provide a method for building ASP. NET applications.

First, create a new ASP. NET Web application in VS. NET. C # is used in the sample code #. Create a Web form and save the file with the ASPX extension. I call it my main. aspx. This page is the only Web page in your application, because you use the Web user control ASCX file) to develop all the page "content ".

On the main. aspx page, create the decoration content of your site. Add a PlaceHolder control where you want to add rich content to the site. Then, add the following code to the Page Load event of your Page:

private void Page_Load(object sender, System.EventArgs e) { string p = Request.QueryString.Get("p"); PlaceHolder1.Controls.Clear(); if (p == null) PlaceHolder1.Controls.Add(LoadControl ("modules/" + "Default" + ".ascx")); else { PlaceHolder1.Controls.Add(LoadControl ("modules/" + p + ".ascx")); } } 

With this code, you can see a query string parameter p containing the name of the Web user control page ASCX in the project module folder. You do not need to put all ASCX files in a separate folder, but this can further isolate the application.

Once you get the string, clear the PlaceHolder's Controls set and any ASCX that you may have previously displayed. Then, check the code to see if you need to load a default ASCX. Finally, if the p query string is part of the request, the page represented by the p query string will be loaded.

You do not need to build code to display multiple Web pages on your site. Add a new Web user control page in your VS. NET project. Once you add this page, you can design and write code as the only page in your application. You don't even need to know that it will be stored in main. run on the aspx page. Of course, if the page needs to interact with the home page, the situation is not that simple, but here I am not going to talk about this problem.


Figure 1. Keep the constant element of your Web GUIs

Imagine that you need to display an Account Logon user control in the VS. NET development environment, as shown in Figure 1 ). The Code only provides the developer with the UI related to the page details. There is no way to change other aspects of the overall page.

Now, when you run http: // localhost/CSCSdotNET/main. in aspx, default is first loaded. ascx page, because main. the reason for the aspx Page Load event. To navigate to the Login page, you only need to add a p query string parameter to the request. In this example, use URL http: // localhost/CSCSdotNET/main. aspx? P = 01-AccountLogon. The entire main. aspx page is displayed, And the 01-AccountLogon.ascs Web user control dynamically loaded as shown in Figure 1.

  1. Describes the four statuses of ASP. NET.
  2. Ten effective performance optimization methods for ASP. NET
  3. Form Verification Implementation of ASP. NET under MVC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.