Article

A Fast Track Guide to ASP.NET - Chapter 1

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 Next

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 goes into 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. Let's consider a simple form that extracts the author details from the pubs database. We'll 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 show you 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 own samples. Like ASP we 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 extension of .aspx.

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 we see the following:

706_24_1

A Fast Track Guide to ASP.NET 33 Nothing particularly challenging here, and when the button is pressed, the grid fills with authors from the selected state:

706_25_1

If you liked this article, share the love:
Print-Friendly Version Suggest an Article