spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / asp_net2 / 1 To page 1current pageTo page 3To page 4
[previous][next]

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


Build Your Own ASP.NET Website Using C# andVB.NET. Pt. 2.

Directives

The directives section is one of the most important parts of an ASP.NET page. Directives control how a page is compiled, specify settings when navigating between pages, aid in debugging (error-fixing), and allow you to import classes to use within your page’s code. Directives start with the sequence <%@, followed by the directive name, plus any attributes and their corresponding values, then end with %>. Although there are many directives that you can use within your pages, the two most important are the Import and Page directives. We will discuss directives in greater detail later, but, for now, know that the Import and Page directives are the most useful for ASP.NET development. Looking at the sample ASP.NET page in Figure 2.1, you can see that a Page directive was used at the top of the page as shown:

<%@ Page Language="VB" %>
<%@ Page Language="C#" %>

The Page directive, in this case, specifies the language that’s to be used for the application logic by setting the Language attribute appropriately. The value provided for this attribute, in quotes, specifies that we’re using either VB.NET or C#. There’s a whole range of different directives; we’ll see a few more later in this chapter.

Unlike ASP, in ASP.NET, directives can appear anywhere on a page, but are most commonly written as the very first lines.

Code Declaration Blocks

In Chapter 3, VB.NET and C# Programming Basics we’ll talk about code-behind pages and how they let us separate our application logic from an ASP.NET page’s HTML presentation code. If you’re not working with code-behind pages, however, code declaration blocks must be used to contain all the application logic of your ASP.NET page. This application logic defines variables, subroutines, functions, and more. In our page, we place the code inside <script> tags, like so:

<script runat="server">
Sub mySub()
  ' Code here
End Sub
</script>

Here, the tags enclose some VB.NET code, but it could just as easily be C# if our page language were set thus:

<script runat="server">
void mySub() {
  // Code here
}
</script>

Before .NET emerged, ASP also supported such script tags using a runat="server" attribute, although they could only ever contain VBScript, and, for a variety of reasons, they failed to find favor among developers. Code declaration blocks are generally placed inside the <head> tag of your ASP.NET page. The sample ASP.NET page shown in Figure 2.1, for instance, contained the following code declaration block:

<script runat="server">
Sub Page_Load()
  lblMessage.Text = "Hello World"
End Sub
</script>

Perhaps you can work out what the equivalent C# code would be:

<script runat="server">
void Page_Load() {
  lblMessage.Text = "Hello World";
}
</script>

The <script runat="server"> tag accepts two other attributes, as well. You can set the language used in the block with the language attribute:

<script runat="server" language="VB">
<script runat="server" language="C#">

If you don’t specify a language within the code declaration block, the ASP.NET page will use the language provided by the language attribute of the Page directive. Each page may only contain code in a single language; for instance, it is not possible to mix VB.NET and C# in the same page.

The second attribute available is src, which lets you specify an external code file to use within your ASP.NET page:

<script runat="server" language="VB" src="mycodefile.vb">
<script runat="server" language="C#" src="mycodefile.cs">

Code Render Blocks

You can use code render blocks to define inline code or inline expressions that execute when a page is rendered, and you may recognize these blocks from traditional ASP. Code within a code render block is executed immediately as it is encountered, usually when the page is loaded or rendered for the first time, and every time the page is loaded subsequently. Code within a code declaration block, on the other hand, occurring within script tags, is only executed when it is called or triggered by user or page interactions. There are two types of code render blocks: inline code and inline expressions, both of which are typically written within the body of the ASP.NET page.

Inline code render blocks execute one or more statements and are placed directly inside a page’s HTML within <% and %> characters.

Inline expression render blocks can be compared to Response.Write() in classic ASP. They start with <%= and end with %>, and are used to display values of the variables and methods on a page.

Looking back at Figure 2.1, you can see both types of code render blocks:

<% Dim Title As String = "Zak Ruvalcaba" %>
<%= Title %>

This equates to the following C#:

<% String Title = "Zak Ruvalcaba"; %>
<%= Title %>

The first line represents an inline code render block and must contain complete statements in the appropriate language. Here, we’re setting the value of the Title variable to the string Zak Ruvalcaba. The last line is an example of an inline expression render block used to write out the value of the Title variable, Zak Ruvalcaba, onto the page.

ASP.NET Server Controls

At the heart of ASP.NET pages lies the server controls, which represent dynamic elements that your users can interact with. There are four basic types of server control: ASP.NET controls, HTML controls, validation controls, and user controls.

All ASP.NET controls must reside within a <form runat="server"> tag in order to function correctly. The only two exceptions to this rule are the HtmlGenericControl and the Label Web control.

Server controls offer the following advantages to ASP.NET developers:

Because ASP.NET is all about controls, we’ll be discussing them in greater detail as we move through this book. For instance, in the next few chapters, we’ll discuss HTML controls and Web controls (Chapter 4, Web Forms and Web Controls), Validation controls (Chapter 5, Validation Controls), Data controls (Chapter 9, The DataGrid and DataList Controls), and so on.

home / programming / asp_net2 / 1 To page 1current pageTo page 3To page 4
[previous][next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: June 28, 2004

URL: http://webreference.com/programming/asp_net2/1