Professional Documents
Culture Documents
NET
Part 1 of 4 - ASP.NET
Fundamentals (in C#)
Table of Contents
Fritz Onion's Intro to ASP.NET Part 1 of 4 - ASP.NET Fundamentals (in C#) .................... 1
Exercise 1 Creating a simple .aspx page........................................................................................................................2
Exercise 2 Building and using a supplemental assembly ..............................................................................................5
Exercise 3 Building a .aspx page with code behind.......................................................................................................7
Fritz Onion's Intro to ASP.NET Part 1 of 4 - ASP.NET Fundamentals (in C#)
Page 1 of 8
Fritz Onion's Intro to ASP.NET Part 1 of 4 - ASP.NET Fundamentals (in C#)
Exercise 1
Creating a simple .aspx page
Scenario
In this exercise, you will create a new ASP.NET Web Project with Visual Studio .NET 2003 and create a simple
.aspx page that displays artificial weather for a number of different zip codes.
Page 2 of 8
Fritz Onion's Intro to ASP.NET Part 1 of 4 - ASP.NET Fundamentals (in C#)
Tasks Detailed Steps
<p>The weather for zipcode 04090 is: </p>
<p>The weather for zipcode 90210 is: </p>
<p>The weather for zipcode 84101 is: </p>
<p>The weather for zipcode 80014 is: </p>
<p>The weather for zipcode 02101 is: </p>
</body>
</html>
5. Try running your a. To test your page in the browser, right click on the WeatherPage.aspx file in the
page. Solution Explorer and select Set as start page.
b. Select Debug | Start without debugging (or press Ctrl-F5) to launch the page in
the browser.
c. Close the Internet Explorer window.
6. Next, add a server- a. Add a server-side script block just below your @Page directive:
side code block to <script runat="server">
your page with a // TODO
method called </script>
'GetForecast'. b. Type the following code in the newly created script block of WeatherPage.aspx
after //TODO.
static Random rand = new Random();
This adds a member variable declaration to your script block for a reference to the
Random class and initializes it to a newly allocated instance of Random.
c. Add the following code in the script block of WeatherPage.aspx by pasting
Snippet2
string GetForecast(string zip)
{
string ret = string.Empty;
switch (rand.Next(5))
{
case 0:
ret = "Sunny and warm";
break;
case 1:
ret = "Partly cloudy and chilly";
break;
case 2:
ret = "Cold with a chance of snow";
break;
case 3:
ret = "Rain";
break;
case 4:
ret = "Foggy and damp";
break;
}
return ret;
}
This adds a method declaration to your script block which accepts a string as input
(zipcode) and returns a string (forecast) which is a randomly generated weather
forecast.
7. Finally, complete a. Use server-side script notation to call GetForecast at the end of each paragraph
Page 3 of 8
Fritz Onion's Intro to ASP.NET Part 1 of 4 - ASP.NET Fundamentals (in C#)
Tasks Detailed Steps
each of the <p> element - the first one should now look like:
elements of your <p>The weather for zipcode 04090 is:
page by actually <%= GetForecast("04090") %> </p>
calling your
GetForecast function
to retrieve the
forecast for that
zipcode!
8. Compile and run a. You can explicitly compile by pressing Ctrl-Shift-B, or selecting Build | Build
your page! Solution from the menu.
b. Launch your application by selecting Debug | Start without debugging (or press
Ctrl-F5) (which will also compile your application if necessary).
c. Verify that you see randomly generated forecasts for each zip code and close the
Internet Explorer window.
Page 4 of 8
Fritz Onion's Intro to ASP.NET Part 1 of 4 - ASP.NET Fundamentals (in C#)
Exercise 2
Building and using a supplemental assembly
Scenario
In this exercise, you will build a secondary library (assembly) which contains a WeatherForecaster class to
perform the weather forecast functions that you build directly into the page in the last exercise.
Page 5 of 8
Fritz Onion's Intro to ASP.NET Part 1 of 4 - ASP.NET Fundamentals (in C#)
Tasks Detailed Steps
zip)
{
//...
6. Add a reference to a. Right click on the References folder in your before web project in the Solution
your Explorer and select Add Reference.
WeatherForecaster b. In the Add Reference dialog click on the Projects tab.
assembly in your
c. Select the WeatherForecaster project and click Select.
Web project
d. Click OK.
Adding this reference causes VS.NET to copy the WeatherForecaster assembly to
the /bin directory of the web application, as well as add it to the list of referenced
assemblies during compilation.
7. Change the calls to a. Double-click the WeatherPage.aspx file
GetForecast to use b. Change the calls to GetForecast in the interspersed script blocks to use the
your new class namespace-qualified class reference
library in your
AspDotNetIntro.Lab1.WeatherForecaster.GetForecast("04090")
WeatherPage.aspx
.
page
8. Compile and test! a. Compile by pressing Ctrl-Shift-B, or select Build | Build Solution from the
menu.
b. Launch your application by selecting Debug | Start without debugging (or press
Ctrl-F5).
c. Verify that you see randomly generated forecasts for each zip code and close the
Internet Explorer window.
Page 6 of 8
Fritz Onion's Intro to ASP.NET Part 1 of 4 - ASP.NET Fundamentals (in C#)
Exercise 3
Building a .aspx page with code behind
Scenario
In this exercise, you will add another page to your Web application that uses code-behind to separate code from
presentation.
6. Compile and test! a. Right click WeatherPageWithCodeBehind.aspx in the Solution Explorer and
Page 7 of 8
Fritz Onion's Intro to ASP.NET Part 1 of 4 - ASP.NET Fundamentals (in C#)
Tasks Detailed Steps
select Set as start page, then select Debug | Start without debugging to test the
page.
Page 8 of 8