Real Time Streaming Protocol April 1998. Microsoftnet Framework Repair Tool . The server uses it to indicate to. ASP. Net Master Pages: Tips, Tricks, and Traps. Master pages are a great addition to the ASP. NET feature set. Master pages help us build consistent and maintainable user interfaces. Master pages, however, are not without their quirks. All-in-One Home Cinema, Stylish Designed. All new multi-entertainment home cinema system that delivers immersive sound quality. It’s an advanced college Management System Project written in C#.Net 4.5 as front end and SQL Server 2012 as Back end. Version 2.9 : Lots of Security Features are added. NSD DNS server install. The first task is to install NSD. We are going to use NSD v3.2.8 for this example. Make to get the latest version since you want to have all. The master page contains some common elements, like a head tag. The most important server-side controls are the form tag (form1) and the ContentPlaceHolder.Sometimes master page behavior is surprising, and indeed the very name master page can be a bit misleading. In this article, we are going to examine some of the common problems developers run into when using master pages, and demonstrate some practical advice for making effective use of master pages. For an introduction to master pages, see . To make use of master pages, we first need to understand how master pages work. Many of the tips and traps covered later in this article revolve around understanding the magic behind master pages. Let’s dig into these implementation details first. For Internal Use Only. When a web request arrives for an ASP. NET web form using a master page, the content page (. Let’s say we are using the following, simple master page. Content. Place. Holder (Content. Place. Holder. 1). Let’s also write a simple web form to use our master page. Place. Holder, it moves the controls into the matching Content. Place. Holder. In our simple setup, the master page will find a match for Content. Place. Holder. 1, and copy over the Label. All of this work occurs after the content page’s Pre. Init event, but before the content page’s Init event. During this brief slice of time, the master page is deserving of its name. The master page is in control - giving orders and rearranging controls. However, by the time the Init event fires the master page becomes just another child control inside the page. In fact, the Master. Page class derives from the User. Control class. I’ve found it useful to only think of master pages as masters during design time. When the application is executing, it’s better to think of the master page as just another child control. The Pre. This is the next topic for discussion. Handling the Pre. Init Event. We can use the @ Page directive and the web. A page’s Master. Page. File property sets the master page for the content page to use. If we try to set this property from the Load event, we will create an exception. In other words, the following code. The simple solution is to just use the Pre. Init event, but we probably don’t want to write the Pre. Init event handler over and over for each web form in our application. Chances are good the Pre. Init event handler will need to look up the master page name from a database, or a cookie, or from some user preference settings. We don’t want to duplicate this code in every webform. A better idea is to create a base class in a class library project, or in the App. For web forms with inline code, we just need to change the Inherits attribute of the @ Page directive.< %@Page. Language=. If a specific page doesn’t want it’s master page set, it can choose not to derive from Base. Page. This is useful if different areas of an application use different master pages. However, there may be times when we want an application to enforce a specific master page. It could be the same type of scenario (we pull the master page name from a database), but we don’t want to depend on developers to derive from a specific base class (imagine a third party uploading content pages). In this scenario we can factor the Pre. Init code out of the base class and into an Http. Module. Http. Modules sit in the ASP. NET processing pipeline and can listen for events during the processing lifecycle. Modules are good solutions when the behavior you want to achieve is orthogonal to the page processing. For instance, authentication, authorization, session state, and profiles are all implemented as Http. Modules by the ASP. NET runtime. You can plug- in and remove these modules to add or discard their functionality. Here is a module to set the Master. Page. File property on every Page object. System; using System. Web; using System. Web. UI; publicclass. Master. Page. Module : IHttp. Module. The Pre. Request. Handler. Execute fires just before ASP. NET begins to execute a page. During the event handler, we first check to see if ASP. NET is going to execute a Page handler (this event will also fire for . Master. Page. File property). We hook the page’s Pre. Init event. During the Pre. Init event handler we set the Master. Page. File property. Again, the event handler might look up the filename from the database, or a cookie, or a session object, which is useful when you give a user different layouts to choose from. To use the module, we just need to add an entry to the application’s web. Modules> < addname=. There are different approaches we can take to achieve interaction, but the best approaches are the ones that use the master page for what it is: a user control. First, let’s look at how the content page can interact with the master page. Content Page to Master Page Interaction. Let’s imagine we want all of the pages in our application to have some text in a footer area. This seems like the perfect job for a master page, so we will add a label control to our master.< formid=. Here is one approach we can use from page’s Page. Find. Control is fragile, and will return null if someone renames Footer. Label, or removes the control entirely. This problem can't be discovered until runtime. Find. Control also has some additional difficulties when INaming. Containers are involved - we will discuss this topic later. A better approach is to establish a formal relationship between the master page and content page, and take advantage of strong typing. Instead of the content page poking around inside the master page, let’s have the master page expose the footer text as a property. We can add the following code to our master page. Public. Property Footer. Text() As. String Get Return Footer. Label. Text. End. Get Set(By. Val value As. String)Footer. Label. Text = value. End. Set. End. Property. The best way to use this property is to place a @ Master. Type directive in our content page. When the ASP. NET compiler sees the @ Master. Type directive, it creates a strongly typed Master property in our Page derived class. What if we have 2 different master pages in the application? In this scenario, we have a problem, because the Virtual. Path attribute supports only a single master page. We’ve tightly coupled our page to a specific master. If we assign a Master. Page. File that does not match the Master. Type, the runtime will throw an exception. Unable to cast object of type 'ASP. Fortunately, the @ Master. Type directive doesn’t require us to use a Virtual. Path, we can also specify a type name. Once again we will turn to inheritance to solve this problem. If all the content pages expect their master pages to have footer text, then let’s define a base class for the master pages to inherit. We can take one of two approaches with the base class. One approach is to use an abstract (Must. Inherit) base class: using System. Web. UI; publicabstractclass. Base. Master. Page : Master. Page. All we need is an @ Master. Type directive set to the base class. Instead of using a Virtual. Path attribute, we use a Type. Name attribute and specify the name of the base class. This approach is possible only if we are sure every master page will have a label with an ID of “Footer. Label”. using System. Web. UI; using System. Web. UI. Web. Controls; publicclass. Base. Master. Page : Master. Page. If we are using code- beside files instead of inline script, we need to use Code. File. Base. Class=”Base. Master. Page” in the @ Master directive to ensure ASP. NET can wire up the base class’s Label field with the Label control. Master Page To Content Page Interaction. Here is a case where the master part of the master page name can be misleading. The master page sounds like a good place to put logic and code that will tell the page how to do something. After all, a master page is the master, right? We now know that the master page is just another child control. Ideally, the master page will remain passive. Instead of telling it’s parent page what to do, the master page should tell a page when something interesting happenes, and let the page decide what to do. Let’s pretend every page in our application displays a report, and every page needs a button for users to click and email the report. Putting a Button and a Text. Box inside the master page seems like a reasonable choice.< asp: Text. Boxrunat=? We can choose from the following options: Handle the Click event in the master page, and have the master page email the report. Expose the Button and Text. Box as public properties of the master page, and let the content page subscribe to the click event (and email the report). Define a custom Send. Email event, and let each page subscribe to the event. The first approach can be ugly because the master page will need to call methods and properties on the page. Master pages are about layout, we don’t want to clutter them with knowledge of reports and specific pages. The second approach is workable, but it tightly couples the page to the master. We might change the UI one day and use a Drop. Down. List and a Menu control instead of a Text. Box and Button, in which case we’ll end up changing all of our pages. The third approach decouples the master page and content page nicely.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |