235.programmers World.c#
235.programmers World.c#
As with the WinForms .NET components you’re used to, ASP. PASSING SHOT
NET components expose properties, methods and events. In Next month we’ll be focusing
the above code fragment, the Page_Load handler gets called on Web Matrix from Microsoft.
in response to a Load event. If the IsPostBack property This is a community-supported
returns false, then we know it’s not a load that results from a project that provides a high-
postback. This means it must be the first load of the page, quality, zero-cost tool for ASP.
and any appropriate first-time initialisation can be performed. NET website development. If
you can’t wait until next month
2 What’s in a Page? The Page class inhabits the System. but want to check it out for
yourself, point your browser at
Web.UI namespace and, as the name suggests, provides a
www.asp.net/webmatrix and
server-side encapsulation of a Web Forms page or .ASPX file. get downloading. It’s a trim 1.3
A Web Forms page consists of static HTML and/or one or MByte download, but does
more ASP.NET controls. As with regular desktop programming, assume that the .NET
there’s a design-time form where you can lay out the various framework is already installed.
ASP.NET controls which make up the page, and there’s also Here we’re building a web page that initially contains nothing but The website contains a lot of
separate ‘code-behind’ source which contains the server-side a ListBox component, also from the System.Web.UI namespace. tutorial information, a guided
script that implements the page. As with any other .NET tour and even a freely
source code, the code-behind file has the extension ‘.CS’ if it three entries of the list-box to ‘Starter’, ‘Main Course’ and downloadable Web Matrix
book in PDF format. And yes,
happens to be a C# file. ‘Desert’. I also modified the control’s background colour. The
you can program Web Matrix
If you’re using something like Visual Studio 2005 (or even resulting HTML looks like this: using C#.
the existing 2003 version), you’ll find that the IDE will
automatically handle the creation of the code-behind file as <asp:ListBox ID=‘ListBox1’ runat=‘server’
shown in the accompanying screenshot. Here, we have the BackColor=‘#C0C0FF’ Height=‘280px’
default web page with the designer view showing a simple OnSelectedIndexChanged=‘ListBox1_
SelectedIndexChanged’ Width=‘384px’>
list-box. This corresponds to the file Default.aspx. The
<asp:ListItem>Starter</asp:ListItem>
corresponding code-behind source is contained inside <asp:ListItem>Main Course</asp:ListItem>
Default.aspx.cs. <asp:ListItem>Desert</asp:ListItem>
If you look at the properties page on the right, you’ll see <asp:ListItem></asp:ListItem>
that the ListBox class is actually implemented inside System. </asp:ListBox>
Web.UI.WebControls along with the rest of the ASP.NET
components. To see all the web controls (and other goodies) As you can see, an ASP.NET control is introduced by the
contained within this namespace, you can use something <asp> tag. In this case, the list-box contains three ListItem
like Reflector to browse the metadata within the assembly. entries (actually four, the last one doesn’t have a caption set).
It’s instructive to add a few random Web Forms What’s interesting is that you can select different parts of this
components to a page and then swap over to the Source tab HTML from inside VS.NET, and the Properties window will
in order to see the HTML ‘source’ file that’s being generated. I change to reflect the currently selected item. For example,
tried adding a list-box to a page and then changing the first with a ListItem selected, you can modify its Enabled,
GOING FURTHER Selected, Text and Value properties via the Properties window
At first glance, ASP.NET and and the changes are immediately reflected in the HTML
web programming might look code. If you look at the C# code-behind source, you’ll see
intimidating (it still looks something like this:
pretty intimidating after a
second or third glance). But public partial class _Default : System.
help is at hand in the form of a Web.UI.Page
large number of ASP.NET titles, {
which you can pick up from protected void Page_Load(object sender,
Amazon using that oh-so- EventArgs e)
tempting ‘Buy now with 1- {
Click’ button. One of my }
favourites is ‘Build Your Own protected void ListBox1_SelectedIndexChang
ASP.NET Website Using C# & ed(object sender, EventArgs e) Reflector can be used to browse the assemblies that contain the
VB.NET’ by Z. Ruvalcaba. { implementations of the various ASP.NET controls.
Another is ‘Beginning ASP.Net }
1.1 E-Commerce: From Novice } client. If you examine the top of the .ASPX file generated by
to Professional’ by Darie and VS.NET, this is what you’ll see:
Watson. Read this, and you Here, the page has two defined property handlers; the Load
can implement a ‘Buy Now’
hander that we mentioned earlier and also a handler to <%@ Page Language=‘C#’
button for your own website. AutoEventWireup=‘true’ CodeFile=‘Default.
control what happens when the list-box selection is
changed. It’s easy to think of this in terms of plain-vanilla aspx.cs’ Inherits=‘_Default’ %>
desktop programming, but remember: this handler is invoked
on the server when the list-box selection is changed on the Crucially, this associates the .ASPX file with the C# containing
remote client machine. any defined event handlers and so forth. The
You might be perplexed by the partial keyword in the AutoEventWireup attribute (which is also new in ASP.NET 2.0)
above C# declaration. This is a new enhancement that was causes the ASP.NET system to automatically ‘wire up’ each of
added to the C# language in order to support ASP.NET 2.0. It’s the event handlers in the code file to the appropriate event
been added to VB.Net for the same reason. In essence, this exposed by the page.
keyword allows the declaration of a C# class declaration to In next month’s tutorial, we’ll get to grips with Web Matrix,
extend across more that one source. We’re telling the build some simple web pages and see how all this stuff
compiler: this is some of the declaration for the _Default works out in practice. The ASP.Net learning curve is a bit
NEXT MONTH class, but not all of it. This much ought to be obvious steep in places, but its well worth getting to grips with this
because, if you think about it, where are the declarations for technology. If you browse the web, you’ll find that more and
Next month, we’ll get to grips
with Web Matrix, Microsoft’s the list-box and those four ListItem entries? They simply don’t more third-party ASP.NET controls are available which can be
free but powerful development appear in the C# source file at all. used to create stunning web sites. To see what’s on offer, go
tool for creating websites There’s actually a lot of magic that happens behind the to www.asp.net and then click ‘Control Gallery’ from the
based around ASP.NET. scenes when the server receives a page request from a toolbar. You’ll be impressed with what’s now available. PCP