Use PlaceHolder controls in ASP.NET

zhaozj2021-02-16  104

Marc Gusmano

When converting from a web page to another, some parts of your ASP.NET apps are often unchanged - such as some "decorative" part: GIF images at the header , Horizontal toolbar or navigation bar, link on the left, and so on. You can continue with them without having to rebuild each page.

In the past, a popular ASP technology created an incrude file for each of the main sections of a page, then built ä, the entire HTML table, the unit's unit contains each include file. This method is feasible, but not beautiful, it is easy to go wrong. Fortunately, ASP.NET allows you to solve this problem with a better part, use a Visual Studio .NET PlaceHolder Web control. This control contains other controls as a collection. You can clear that Collection or add it at runtime; for example, you can add a web user control (a modular web page) at runtime. I will tell how the PlaceHolder Web control combines with web user controls to provide a method for building an ASP.NET application.

First create a new ASP.NET web application in VS.NET. I use C # in the sample code. Create a web form, saved file with an ASPX extension. I call it for 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 pages "content".

Create your choice site decoration content in the main.aspx page. Add a PlaceHolder control in your place where you want to place your site. Then, add the following code to your page Page Load Event:

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 ("Modules /" P ".ascx"));}}

Through this code, you can see a query string parameter P in the project module folder that contains the name of the web user control page. You don't have to put all ASCX files in a separate folder, but do this can be further isolated.

Once you get that string, empty the PlaceHolder's Controls collection, clear your previously possible ASCX. 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, then the page represented by the P query string is loaded.

You don't need a lot of force building code to display multiple web pages on your site. Add a new web user control page in your VS.NET project. Once you have added this page, you can design it as the only page in your application and write code; you don't even know it will run in the main.aspx page. Of course, if the page needs to interact with the home page, the situation is not as simple, but I am not going to tell this question.

figure 1.

Keep your web guis's constant elements

Imagine that you need to display an Account Logon user control in the VS.NET development environment (see Figure 1). The code only provides developers with UI associated with this page detail. There is no way to change the other aspects of the overall page.

Now, when you run http://localhost/cscsdotnet/main.aspx in a browser, you first load the Default.ascx page, which is due to main.aspx's Page Load event. To navigate to the login page, you simply add a P Query string parameter to the request - in this example, use the URL http: //localhost/cscsdotnet/main.aspx? P = 01-Accountlogon. The entire main.aspx page is now displayed, as well as the dynamically loaded 01-AccountLogon.ascs web user control you see in Figure 1.

转载请注明原文地址:https://www.9cbs.com/read-10603.html

New Post(0)