Writing ASP.NET Pages


The first part of this chapter has been a brief overview of some of the differences between ASP and ASP.NET, and Chapter 4 covers this in more detail. Now it's time to show you how to get those ASP.NET pages up and running as quickly as possible. Consider a simple form that extracts the author details from the pubs database. It will have a drop down list to show the various states where the authors live, a button to fetch the information, and a grid. This will quickly demonstrate several simple techniques you can use in your pages.

Creating a Web Site

The first thing to do is decide on where you want to create your samples. Like ASP, you can create a directory under \InetPub\ wwwroot , or create a directory elsewhere and use a Virtual Site or Virtual Directory to point to it. There's no difference between the methods , it's purely a matter of preferences.

Next you can create your web pages, using whatever editor you prefer. You should give them an .aspx extension.

The Sample Page

Now let's add the code for the sample page “ call this SamplePage.aspx (we'll examine it in more detail after we've seen it running). This page assumes that the Pubs database is installed on your system.

  <%@ Import Namespace="System.Data.SqlClient" %>   <script language="VB" runat="server">   Sub Page_Load(Sender As Object, E As EventArgs)   If Not Page.IsPostBack Then   state.Items.Add("CA")   state.Items.Add("IN")   state.Items.Add("KS")   state.Items.Add("MD")   state.Items.Add("MI")   state.Items.Add("OR")   state.Items.Add("TN")   state.Items.Add("UT")     End If   End Sub     Sub ShowAuthors(Sender As Object, E As EventArgs)   Dim con As New SqlConnection("Data Source=.; " & _   "Initial Catalog=pubs; UID=sa;   PWD=")   Dim cmd As SqlCommand   Dim qry As String   con.Open()   qry = "SELECT * FROM authors where state='" & _   state.SelectedItem.Text & "'"   cmd = New SqlCommand(qry, con)   DataGrid1.DataSource = cmd.ExecuteReader()   DataGrid1.DataBind()   con.Close()   End Sub   </script>     <form runat="server">     State: <asp:DropDownList id="state" runat="server" />   <asp:Button Text="Show Authors" OnClick="ShowAuthors" runat="server"/>   <p/>   <asp:DataGrid id="DataGrid1" runat="server"/>     </form>  

When initially run, you'll see the results as shown in Figure 1-7:

click to expand
Figure 1-7:

Nothing particularly challenging here, and when the button is pressed, the grid fills with authors from the selected state (Figure 1-8):

click to expand
Figure 1-8:

Again, nothing that couldn't be achieved with ASP, but let's look at the page code, starting with the controls:

 <form runat="server">     State: <asp:DropDownList id="state" runat="server" />     <asp:Button Text="Show Authors" OnClick="ShowAuthors" runat="server"/>     <p/>      <asp:DataGrid id="DataGrid1" runat="server"/> </form> 

Here we have a form marked with the runat="server" attribute. This tells ASP.NET that the form will be posting back data for use in server code. Within the form, there is a DropDownList control(the equivalent of an HTML SELECT list) to contain the states, a Button (equivalent of an HTML INPUT type="button") to postback the data, and a DataGrid control to display the authors. The button uses the OnClick event to identify the name of the server-side code to run when the button is pressed. Don't get confused by thinking this is the client-side, DHTML onClick event, because it's not. The control is a server-side control ( runat="server" ) and therefore the event will be acted upon within server-side code.

Now look at the remaining code, starting with the Import statement. This tells ASP.NET that we are going to use some data access code, in this case code specific to SQL Server.

 <%@ Import Namespace="System.Data.SqlClient" %> 

Next comes the actual code, written in Visual Basic.

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

Here is the first real introduction to the event architecture. When a page is loaded, the Page_Load event is raised, and any code within the event procedure is run. In our case, we want to fill the DropDownList with a list of states, so we just manually add them to the list. In reality, this data would probably come from a database.

 Sub Page_Load(Sender As Object, E As EventArgs) If Not Page.IsPostBack Then    state.Items.Add("CA")    state.Items.Add("IN")    state.Items.Add("KS")    state.Items.Add("MD")    state.Items.Add("MI")    state.Items.Add("OR")    state.Items.Add("TN")    state.Items.Add("UT")  End If End Sub 

One thing to note about this code is that it is wrapped in an If statement, checking for a Boolean property called IsPostBack . One of the great things about the web controls is that they retain their contents across page posts, so we don't have to refill them. Since the Page_Load event runs every time the page is run, we'd be adding the states to the list that already exists, and the list would keep getting bigger. The IsPostBack property allows us to identify whether or not this is the first time the page has been loaded, or if we have done a post back to the server.

Now, when the button is clicked, the associated event procedure is run. This code just builds a SQL statement, fetches the appropriate data, and binds it to the grid.

 Sub ShowAuthors(Sender As Object, E As EventArgs)     Dim con As New SqlConnection("Data Source=.; " & _      "Initial Catalog=pubs; " & _      ConfigurationSettings.AppSettings("DsnPubs"))     Dim cmd As SqlCommand     Dim qry As String      con.Open()     qry = "SELECT * FROM authors where state='" & _     state.SelectedItem.Text & "'"     cmd = New SqlCommand(qry, con)          DataGrid1.DataSource = cmd.ExecuteReader()     DataGrid1.DataBind()      con.Close() End Sub 

This code isn't complex, although it may seem confusing at first glance. The rest of the book explains many of these concepts in more detail, but we can easily see some of the benefits. The code is neatly structured, making it easy to write and maintain. Code is broken down into events, and these are only run when invoked. Chapter 4 contains a good discussion of the page events, how they can be used, and the order in which they are raised.

What's also noticeable is that there's less code to write compared to an equivalent ASP page. This means that we can create applications faster “ most of the legwork is done by the controls themselves . What's also cool about the control architecture is that we can write our own to perform similar tasks . Because the entire .NET platform is object based, we can take an existing control and inherit from it, creating our own, slightly modified control. A simple example of this would be a grid within a scrollable region. The supplied grid allows for paging, but not scrolling.




Professional ASP. NET 1.1
Professional ASP.NET MVC 1.0 (Wrox Programmer to Programmer)
ISBN: 0470384611
EAN: 2147483647
Year: 2006
Pages: 243

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net