ASP.NET
From Reporting Cookbook: www.forjournalists.com/cookbook
ASP.NET is the name of a collection of tools from Microsoft for building Web applications. It includes software code libraries for languages such as C# and Visual Basic.NET that make it easier to build Web sites that interact with databases, process XML and display data on the Web.
Contents |
[edit] Where Else Can I Go?
This is the main page for the ASP.NET portion of the Cookbook. Links and link categories for ASP.NET pages should be inserted in this section. The following pages also are available:
Lesson 1: Basic controls
Please add your comments to this page. You also can ask questions and request additions to this section.
[edit] What is ASP.NET?
ASP.NET is a technology (sort of like a programming language) you can use to generate powerful, dynamic, data-driven Web sites. ASP.NET is part of the .NET Framework, which is a platform that can be used to build not only Web pages, but also Windows applications. This is an oversimplification, but you can think of ASP.NET as a programming language, such as Visual Basic, C# or Java, and the .NET Framework as the compiler that turns that programming language into a language the server can understand.
[edit] What Do I Need?
The server that will house your ASP.NET pages and the machine you use to design those pages must have the .NET Framework (current version is 3.0) installed. Computers viewing those pages don't need anything other than a Web browser.
The server also must have a Web service installed and running, such as IIS (Internet Information Server), which comes with many of the Windows platforms but might not be installed by default. Once this is installed, you need to create a directory to house your Web site and make sure that ASP.NET is enabled for this directory. More more information on how to do this, you can go here.
To design ASP.NET pages, you technically could just use a text editor like Notepad. However, your best bet is to use Visual Studio 2005 or later or Microsoft's free, light version of Visual Studio, Visual Web Developer, which you can download here.
[edit] Testing Your Setup:
Once you get the .NET Framework installed, you should design a very basic page. Just open up Visual Studio or Visual Web Developer (VS/VWD) and create a new Web site. Store it in the directory set up on the server (or, if this is not an option, copy this page to that directory on the server after you create and save the page on your desktop). This should automatically create a Web page for you called Default.aspx and open it on your screen for editing. If this does not happen, right-click the directory (which should be in the top-right corner of VS/VWD) and select Add New Item, Web Form. Name it whatever you want.
From the Toolbox, which should be along the left side of your screen, drag a label to the page. Click Source near the bottom of your screen to see the code, find the label and make sure it contains the Text attribute and that it is populated with some text. For example:
<asp:Label id="Label1" Text="This is my test label."></Label>
Save and run the page in your Web browser. If you see the text you put in the label's Text property on your screen, you're in business. If you do not see any text on your screen but get a blank white page, most likely the server is not properly configured. Go to IIS on the server and make sure the directory is set up to allow ASP.NET. A link higher up on this page will take you to instructions for setting up a virtual directory on the server, if you need more information.
If you will be using a database manager to house databases that will be used on your Web pages, you also should test your ability to connect to that data. Drag a GridView control to your test page and configure it to connect to a table in your database. I will not go over all the steps to do this, but it's fairly easy. Drag the control to your page. Switch to Design View (by clicking Design near the bottom of your screen), click the little arrow near the top-right corner of the control, and select Select Data Connection, <New Connection>. From here, you will enter information, such as your login and password and the query to use to fetch the data.
When you're done, save the page, copy it to the server (if necessary) and run the page. If you get data back, you're in really good shape. If not, don't panic. You just have to do a little more work to get things set up properly. The next section will give you some basic advice on fixing the problem.
[edit] Troubleshooting:
When you start designing pages in ASP.NET, particularly when you start using more sophisticated controls and add code of your own, you will get error messages. Don't get frustrated. Instead, work on your skills to debug those error messages.
The first thing you should do in you Web site is go to the file called Web.config in your root directory. (If you don't see one, right-click the directory in VS/VWD, select Add New Item and select Web.config.)
ASP.NET is heavily focused on security. So it isn't in the habit of just giving away information about errors unless you tell it to. We need to tell it to give us details about errors when they are encountered. Generally, though, you'll want to disable these features before putting your Web site out there for the public or your newsroom.
First, let's talk a little about Web.config. If you haven't already guessed, this is the configuration file for your Web site. Settings here will be used by all of your Web pages unless you override these settings in individual pages. This is nice because if you have to change something globally, you can do it in one place. (There is also a Machine.config file elsewhere on the server, but we won't worry about that for now. Just be aware that it's there, and it's the first place you should check if your settings in Web.config don't seem to be working correctly.)
Web.config is an XML file. If you don't know what that is, don't worry about it for now. But basically you have a series of parent and child tags. Each tag has an opening tag and a corresponding closing tag this is the same thing prefixed by a space. For example: <system.web> and </system.web>. A parent tag can have child tags nested between its opening and closing tags, and child tags can, in turn, also have tags nested within them. For example:
<configuration>
<system.web>
<page>
<namespaces>
<add namespace="System" />
<add namespace="System.Collections" />
<add namespace="System.Collections.Specialized" />
</namespaces>
</page>
<system.web>
</configuration>
Don't worry about what any of this stuff means for now. Just try to understand the structure of tags nested within other tags. Also, take a look at the <add> tags. Notice that they do not have closing tags. Each has a slash at the end of the tag. This indicates the tag both opens and closes. It also means you cannot have any tags nested within it, since there is no closing tag. You'll also see that each <add> tag has a namespace property that is set to a value. Don't worry about this yet, other than noting that tags often take properties, and this is how you assign values to these properties.
Now let's get back to what we need to do in this file. Drill down into <configuration> and <system.web>, and look for a <compilation> tag. Most likely, you will already see Debug here, but it needs to be set to "True" (including the quotes). If you don't see it, just put your cursor in the tag, press the space bar, and VS/VWD will even give you a context menu to help you find the property you want. This is what Microsoft calls IntelliSense, and it is an awesome tool to help you avoid typos and browse what properties are available to you.
If you get to the point where you are using custom error pages, you also will want to turn off custom errors so you don't see those instead of getting information from the debugger. This setting gets its own tag in <system.web>. Make sure you do NOT nest it inside any child tags of <system.web>. It should look like this:
<customErrors mode="Off">
Once you do this, you should get more details on your Web browser when you run pages and choke on an error.
Whenever you get an error, look at the information you get on the error page. From that, the problem might be obvious, though usually it isn't. When the latter is the case, Google the error messages on the error screen (but not your source code). Often, this will give you several links to pages where others have encountered and solved the problem you are encountering.
[edit] To Code or Not to Code:
When you create as ASP.NET page (with extension .aspx), VS/VWD sets up either a section of your page or a completely separate page to place code. This is where programming takes place outside of dragging controls to the page and setting properties for those controls. When you create a page, there is a checkbox near the bottom of the dialog where you can indicate whether you want to place the code in a separate page. If you elect to do this, VS/VWD will create a separate file with exactly the same name as the page you just created, with an extension added to indicate which programming language you want to use.
ASP.NET supports more than 20 programming languages. However, you'll want to stick with either Visual Basic or C#. There are plenty of adherents to each language, but in the end, pick the one you're most comfortable with. If you're not sure which to use, check with people you are likely to go to for help and see what they use. If that doesn't help, flip a coin. It really doesn't matter.
This does not mean you need to learn a programming language to use ASP.NET. Usually, you can just pick up the bits and pieces you need as you go. I've been writing .NET pages for the past couple of years and only recently started reading a book on Visual Basic .NET. I did this only to make me a little more savvy in terms of writing code but was always able to find what I needed by searching the Web and asking other .NET programmers for help.
If you do not put your code in a separate page, VS/VWD will insert <script> tags near the top of your page where all your code will go.
[edit] Where to get help?
If you are not a programmer, ASP.NET can be a little daunting at first. Don't give up. Much of what you need to do is just dragging controls onto the page and using wizards to configure those controls. But invariably, you will eventually have to start digging around in the guts of the code to get it to do exactly what you want to do, and you'll bang your head in frustration over various errors you get. Don't sweat it. Just accept it, work on your troubleshooting skills and keep going. The more you learn, the easier it gets.
As I said above, when you get an error from running your page, a good trick is to Google the text of the error. Often, this will quickly lead you to links where others have encountered and dealt with this problem. You also can add site:microsoft.com to your Google search to limit the search results to Microsoft pages, though you probably don't want to do this unless you're initial search results aren't helping. If that fails, post your question to the NICAR list. More and more people are using ASP.NET, and we're a friendly bunch.
Over time, you should get familiar with Microsoft's online help. A lot of it is technical gobbledygook, but it is a good resource to learn all there is to know about the various controls, methods and namespaces. (Don't worry if you don't understand methods or namespaces. You'll pick it up later.) The easiest way to do this is to type whatever you are looking for into the Google search box and add site:msdn2.microsoft.com. You'll get back a ton of links for what you're looking for. If a control is new to you and you want to learn the basics of how to use that control, spend some time poking around these sites, and you'll be in good shape.
If you're looking for a good book, check out the ASP.NET books put out by Wrox. I was designing simple pages after just a few chapters of their Beginning ASP.NET 2.0 book. That book is good because it takes you through examples you can type in yourself and try out. Wrox also has online forums for you to post answers and see what questions other users have.






