Every ASP.NET developer needs to be familiar with Page Directives. If you are a beginner and you want to learn about the Page Directives then you can read this article.
So the first question is about Page Directives.
What is a Page Directive?
Basically Page Directives are commands. These commands are used by the compiler when the page is compiled.
How to use the directives in an ASP.NET page
It is not difficult to add a directive to an ASP.NET page. It is simple to add directives to an ASP.NET page. You can write directives in the following format:
<%@%>
See the directive format, it starts with "<%@" and ends with "%>". The best way is to put the directive at the top of your page. But you can put a directive anywhere in a page. One more thing, you can put more than one attribute in a single directive.
Here is the full list of directives:
- @Page
- @Master
- @Control
- @Import
- @Implements
- @Register
- @Assembly
- @MasterType
- @Output Cache
- @PreviousPageType
- @Reference
Let's discuss something about each directive.
When you want to specify the attributes for an ASP.NET page then you need to use @Page Directive. As you know, an ASP.NET page is a very important part of ASP.NET, so this directive is commonly used in ASP.NET.
<%@Page Language="C#" AutoEventWIreup="false" CodeFile="Default.aspx.cs" Inherits="_Default"%>
Now you have some information about @Page Directives. The @Master Directive is quite similar to the @Page Directive. The only difference is that the @master directive is for Master pages. You need to note that, while using the @Master Directive you define the template page's property. Then any content page can inherit all the properties defined in the Master Page. But there are some properties that are only available in a Master Page.
<%@Master Language="C#" AutoEventWIreup="false" CodeFile="MasterPage1.master.cs" Inherits="MasterPage"%>
@Control
@Control builds ASP.NET user controls. When you use the directive you define the properties to be inherited by the user controls and theses values are assigned to the user controls
<%@Control Language="C#" Explicit="True" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
As you know you need to define namespaces in your .cs class before using a C# or VB class. So the @Import Directive imports namespaces. This directive supports just a single attribute "namespace" and this attribute takes a string value that specifies the namespace to be imported. One thing you need to note is that the @Import Directive cannot contain more than one attribute/value pair. But you can use multiple lines.
<%@Import Namespace="System.Data"%>
@Implements
The @Implements Directive gets the ASP.NET pages to implement .Net framework interfaces. This directive only supports a single attribute interface.
<%@Implements Interface="System.Web.UI.IValidator"%>
@Register
When you create a user control and you drag that user control onto your page then you will see the @Register directive. This directive registers your user control on the page so that the control can be accessed by the page.
<%@ Register TagPrefix="MayTag Namespace="MyName.MyNameSpace" Assembly="MyAssembly"%>
@Assembly
The @Assembly Directive attaches assemblies to the page or an ASP.NET user control thereby all the assembly classes and interfaces are available to the class. This directive supports the two attributes Name and src. The Name attribute defines the assembly name and the src attribute defines the source of the assembly.
<%@Assembly Name="MyAssembly"%>
<%@Assembly src="MYAssembly.cs">
@MasterType
The @MasterType Directive connects a class name to the ASP.NET page for getting strongly typed references or members contained in the specified Master Page. This directive supports the two attributes Typename and virtualpath. Typename sets the name of the derived class from which to get the strongly typed or reference members and virtualpath sets the location of the page from which these are retrieved.
<%@MasterType VirtualPath="/MasterPage1.master"%>
@output cache
It controls the output caching policies of an ASP.NET page.
<%@ OutputCache Duration ="180" VaryByParam="None"%>
@Previouspagetype
This directive specifies the page from which any cross-page posting originates.
@Reference
This directive declares that another page or user control shout be complied along with the active page or control. This directive supports the single attribute virtualpath. It sets the location of the page or user control from which the active page will be referenced.
<%@Reference VirtualPayh="~/MyControl.ascx"%>
Final Words
I hope you get some knowledge from here. Please comment about how you like this article. Your comments are very valuable for me, because only you will tell me where I am going wrong and what improvements I need to make to write a better article. Please comment and provide your feedback.
What is a guestbook?
Of course, here we are talking about the most typical guest book. First of all, this is a system that provides the user with the ability to enter text, select an assessment of a particular site, and also to specify their own data (name, e-mail, http, etc.). It is also a system for presenting data entered by various users, with the ability to navigate, send emails to the authors of the messages. Variations are also possible with different settings and with the control of normative vocabulary.
What do we need
Of course, it is assumed that the reader is familiar with the basics of ASP and SQL programming (familiarity with the first parts of this article will be quite enough for this). In addition, we need Microsoft SQL Server 7.0 or 2000, some HTML or text editor (I recommend using Macromedia Dreamweaver UltraDev 4.0) and a little patience.
Creating and preparing a database
To organize the storage of data entered by users, you need one table where you can create fields for storing the user's name, his email address, country of residence, site address, IP address, site user rating value on a five-point scale, etc.:
In order to easily integrate the system into existing sites, it is recommended to plan another table for storing color and other settings. This will allow you to change the specified settings in the future without changing the corresponding parameters in the source texts of the guestbook application modules.
- Run the Data Sources ODBC Configurator - Start->Settings->Control Panel->Administrative Tools->Data Sources ODBC.
- Go to the System DSN tab and create a new data source by clicking on Add…
- In the list of drivers that appears, select the database driver - Microsoft SQL Server and click Next.
- In the Data Source Name line, specify the name of your database (in the described example, Gustbook is the name by which you will refer to it in the future).
- In the Server line, specify the server to which the connection will be made, and click Next.
- Select the authentication mode With SQL Server…, set the username and password to connect to the SQL server; define the protocol for communication with the server (button Client Configuration - TCP/IP) and click Next twice, then click Finish.
- You will see statistics about the actions taken, and you can use the Test Data Source button to check the data source.
- As a result, you will see a line in the list of data sources in your system.
Now that the database is ready, you can proceed directly to creating a guest book.
Integration of the system into a ready-made website
It is clear that the guestbook itself does not make sense. Judge for yourself: who needs a site designed solely to collect the opinions of readers. After all, in order to collect opinions about something, you must first present this something to their judgment. Therefore, it is necessary to pay special attention to the issues related to the simplification of embedding the system into ready-made sites.
In particular, to make it easier to customize the system for the characteristic features of a particular site, it is recommended (as mentioned above) to create a special table to store all these preferences in order to enter certain values specific to your site into it. The obvious advantage of this approach is that in order to embed the system into an already finished site, you do not need to make changes in the source code of the modules, you only need to change the settings in the corresponding database table.
Imagine an example of the implementation of such a table: .
As you can see, there are fields for storing information about the name and password for accessing the system setup mode, about the colors of the main background of the user's message, the top and bottom frames (Mid_Color, Top_Color, Bot_Color fields, respectively), about the color and size of the header of the form used for entering user data (Form_Color and FormTitleSize fields, respectively), about the color, size and style of the font of the text of the message itself, information fields, as well as the guestbook pages themselves (fields MessageFontColor, MessageFontSize, MessageFontFace, InfoFontColor, InfoFontSize, InfoFontFace, PageFontColor, PageFontSize and PageFontFace respectively), switch fields to enable the automatic sending of notifications of new messages by e-mail to the responsible person (for example, the manager or site administrator), fields for storing the e-mail address of the responsible person, the text of the message with thanks for the message left by the user, with the list are not allowed x words and a switch for their filtering mode (if the latter is enabled, then the words in the list of invalid words will be automatically replaced in the message text with asterisks and thus control over the normativeness of the vocabulary of the site texts will be exercised).
The development of the guest book integration system implies the organization of a Web interface for setting all the parameters we have considered (fields of the administration table).
Integration of the system into an already finished site in its pure form can create some difficulties both in the perception of the source text and in the future, if, for example, you need to temporarily disable the guest book on a particular site. Therefore, we will try to develop the system in such a way that its integration into the finished site is not difficult. To do this, it is necessary to form an independent module of the system and include it in the text of the main site where necessary. So, for example, the text of your website page might look like this: