Programing
ASP.NET Basics Part two – special folders
by Mats Hellman on Feb.11, 2009, under ASP.NET, Programing
I’m really sorry this post is a little late. My keyboard’s left shift button died and I had to get myself a new one. More on that in another post.
I assume you have read the article from last week. This week we are going to take a look at a few of the special folders that ASP.NET 2.0 uses, in specific we will use the folders APP_CODE and APP_LOCALRESOURCES. There are also a few others but I’ll get into them when I start using them myself
.
Special folders are new to ASP.NET 2.0 and the idea of using them is plain brilliant. Any class, wsdl file or typed datasheet you put in APP_CODE will automatically be available for all pages in your web application. And with APP_LOCALRESOURCES you can set the language of the page by using a few files. This really is brilliant and it make development faster, so you can spend more time in the all important testing phase .
The Demo2 website
After you start up your development IDE, be it Visual Webdeveloper or Visual Studio, create a new website and call it demo2. When you have your new solution open you can delete the APP_DATA since we won’t be using it today.
After that right-click on the URL to your solution and select Add ASP.NET Folder then create one APP_CODE folder and one APP_LOCALRESOURCES folder.
Next let’s get on with some examples on what we can do with our folders.
APP_CODE folder
As I said earlier this folder can hold classes, .wsdl (Web Service Description Language) files and typed datasheets and any of them placed in the folder automatically becomes available for the rest of the pages in your solution. You can even place class code from different languages, like VB.NET and CSharp. The only thing to remember if you decide to use more than one language is that they should be separated in subfolders. Using VB and CS would be the logical choice. If you want to use both languages you have to do a few changes. First of all you have to ad a web.config file to your solution and in it place the following simple code
<compilation> <codesubdirectories> <add directoryname="VB"></add> <add directoryname="CS"></add> </codesubdirectories> </compilation>
This is just to tell ASP.NET compiler where to find the code. You can’t throw both .vb and .cs files in the same directory. And this way it is really clear where to find them.
To demonstrate the use of the folder we will make a simple calculator class. So ad a Class file in the APP_CODE folder and call it Calculator.cs.
public class Calculator { public int Add(int a, int b) { return (a + b); } public int Sub(int a, int b) { return (a - b); } }
After opening the calculator class we add some logic. This will be a really simple application so it wont do much but we could still have two functions, Add and Sub. I will only use Add in this demo but this gives you a view into the logic here.
What you should notice is that as soon as you have the code in place and save it Intellisense picks it up and you can start using it in the Default.aspx file. Or as I did in Default.aspx.cs. This is a really simple page and all it does is add two values which are hardcoded in the page. I chose 100 and 100 and add them together. And voila it works.
APP_LOCALRESOURCES
The localresources folder is used to construct resources that can be used by a single .aspx file, the globalresources does almost the same but it is application-wide. So to test this feature lets create a page called, imaginatively, Test.aspx. Leave it empty for now. Add two resource files in localresources, I chose to add the default Test.aspx.resx and a file for my native language Swedish, Test.aspx.sv.resx. In them ad the following
Test.aspx.resx
| Name | Value | Comment |
| PageTitle | Welcome to Yoursite :: Demo2 | |
| Question | What’s your name? | |
| Answer | Hi there, |
Test.aspx.sv.resx
| Name | Value | Comment |
| PageTitle | Välkommen till Dinsida :: Demo2 | |
| Question | Vad heter du? | |
| Answer | Hej på dej, |
You can use your own languages, just change sv to anything you want. You can also have many many more files in here if you want. I only speak and write Finnish,Swedish and English so my site would be quite small. But if you write 20 languages feel free to put them all in there
.
Now let’s go back to the Test.aspx file. We need to do some work on it to get the local resources in use. First of all, drop in a label (yes I like them) and a textbox and a submit button and another label.
Call the above lblQuestion, tbName and lblAnswer. Remember to keep ID’s as clear and descriptive as possible. It’s not a problem in a solution like this one but if you are working on something with say 1000 pages it will be. Now open the codebehind file and paste or write the following.
public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Page.Title = (String)GetLocalResourceObject("PageTitle"); lblQuestion.Text = (String)GetLocalResourceObject("Question"); } protected void Button1_Click(object sender, EventArgs e) { lblAnswer.Text = (String)GetLocalResourceObject("Answer") + " " + tbName.Text; } }
And here is a screenshot of the application in action. My browser is set to Swedish locales so my page displays in Swedish.
![]()
Se how easy it was to get the values out of our resource files. As I told you there is nothing to it. It’s fast, simple and well for someone who likes code simply beautiful. As always the code for this application can be downloaded as a .zip file.
ASP.NET 2.0 basics
by Mats Hellman on Feb.01, 2009, under ASP.NET, Programing
This is the first post in a series I will be going through as I get into learning ASP.NET. In this first part we will go trough what ASP.NET is and look at how it is handled at the server. I might not get everything right here since I really am a beginner. But writing about it helps me in my learning process so I write and you can read.
More experienced programmers are welcome to comment and correct me if/when I get something wrong, as I said I’m a beginner.
What Is ASP.NET
ASP.NET is not just a new version of ASP, it’s a whole new way for web development. ASP.NET allows you to a full featured programming language such as Visual Basic.NET (VB) or C# (C-Sharp). So what is so different about ASP.NET. Well first of all it’s not a scripting language like the old ASP, in ASP.NET we can use code behind files and separate the logic from the design totally. This is one of the features I really love. The code becomes so much more readable when separated from the HTML, I’ll give you a example in a minute. But if you like the inline model, it’s possible to use it to. I won’t be writing about it here.
Another difference is that ASP.NET code is actually compiled before handed of to the client. So when a page is requested by the browser the ASP.NET engine get’s the page and if changes were made or if it is the first time this particular page has been requested it will compile it before handing it off to the client. Doesn’t this take time you might ask, well it does, but I wouldn’t say it’s even noticeable. The upside is that the next request will get the precompiled version of this page making it a lot faster.
A picture says more than a thousand words so here is a image of the process.
How do I get started?
First of all, I like to have a good development tool, an IDE (Integrated Development Environment) if possible. Microsoft makes gives us two choices. One being the almighty Visual Studio 2005 and the other is a free, yes you heard me, a free IDE called Visual Webdeveloper 2005. Visual webdeveloper can be downloaded from the http://www.asp.net website.
Hello ASP.NET!
Anyone who has ever read a programming book knows the first program written will almost certainly be a “Hello World!” program. So that’s what we are going to start with here also, but let’s make it a little more challenging shall we.
Well use the TextChanged control of a text box to initiate welcome and use a label to display our text. This is after all why we want to use ASP.NET, we want to use all the nice classes and features of ASP.NET. I’m using Visual Studio but you might just as well be using Visual Webdeveloper here. Fire up your IDE and start a new website, I’m more accustomed to C,C++ and PHP code so my writings will , at least for now, remain with CSharp as codebehind.
![]()
You are now presented with the Default.aspx file. Note that this file is the design file, it’s not the Default.aspx.cs ( codebehind CSharp).
On the first row you see something new, it’s the the Page Directive. Whit the page directive you can control the behavior of your page. There are eleven directives at your disposal. So what are directives? Well they are commands the compiler uses when it’s compiling the page.
Directives are incorporated to a page simply as following,
<%@ [Directive] [Attribute=Value] %>
As I said there are eleven directives and they all have their attributes so instead of going trough them all here, you can take a look at them on http://msdn.microsoft.com/en-us/library/xz702w3e.aspx .
Code
Ok then, let’s get started. First switch the view of your page to design instead of source. Drop in a textbox and label control on the page. We don’t need a Submit button because we will use the TextChanged control. Basically that means that once your done entering something in the box just hit Enter and watch the magic happen.
In the Properties explorer you’ll find Misc and you’ll be able to name your textbox and your label. Good
names help in later stages when using them in the codebehind file. So I labeled mine tbName and lblHello. tb for textbox and lbl for label. After this open your codebehind file Default.aspx.cs. Let’s start building the logic. What we’ll do is use the same page to get some input, well ask for the users name and by using TextChanged we can display it on the label without moving to another page.
Sources
So this is how we do it. These are the sources for the files. I’ll also include a .zip for you with the whole demo directory to download if you want it. Anyway here it goes
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Hello ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Your name:<asp:TextBox ID="tbName" runat="server" OnTextChanged="tbName_TextChanged"
Width="342px"></asp:TextBox><br />
<br />
<asp:Label ID="lblHello" runat="server" Height="174px" Text="Label" Width="417px"></asp:Label></div>
</form>
</body>
</html>Looking at this you see how effective ASP.NET is. We only have 16 lines of code in the file to accomplish this. Now let’s look at the logic.
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void tbName_TextChanged(object sender, EventArgs e) { //Set the label text at text change. lblHello.Text = "Hello " + tbName.Text + " welcome to ASP.NET."; } }
What you want to look at is the tbName_TextChange part. With just one line of code we can change the content of the label.
lblHello.Text = "Hello " + tbName.Text + " welcome to ASP.NET.";
That one line sets lblHello text to the string we supply, which is built on the welcoming text and the content of the textbox control.
Bye and thanks for the fish
So this was a really short introduction to ASP.NET. Hope you stick with me as we go along in this series. Next weekend I’ll take a look at some of the built in folders of asp.net and build some custom classes. Until then, enjoy life and keep coding. The zipped files can be downloaded from http://www.nixadmins.net/files/Demo1.zip
Testing WP-Syntax
by Mats Hellman on Jan.29, 2009, under ASP.NET, Programing
A few weeks ago I decided I needed a project or something to learn. It had to be work related but still had to be something I don’t do very often at work. So I decided it was time to take a plunge into ASP.NET 2.0.
I moved the site from Drupal to Wordpress yesterday and found a great plugin WP-Syntax to get syntax highlighting
working. And what better way to learn than to write about it as you go. Writing forces you to really understand what it is that you are reading and not just skip trough chapters.
The book I got was WROX ASP.NET Professional 2.0.
Since yesterday I’ve read up to chapter 3 and I find it very interesting. I haven’t used ASP.NET since 1.1 and I have to say it has got a long way since that. Why I started with 2.0 now that 3.5 is out is simply because I had this nice pack of books at home.
Get to the point!
Well this post was just to see if the WP-Syntax plugin can handle CSharp like it should and if it does I will be writing my own introduction and thoughts around ASP.NET 2.0 as I read, learn and code. So do check back. I’ll try to keep a weekly post about ASP.NET and it should be published every weekend.
Don’t consider this something to read, it’s just a class to see the highlight in WP-Syntax. The class below has been copied from C-Sharp Stations tutorial.
// Namespace Declaration using System; // helper class class OutputClass { string myString; // Constructor public OutputClass(string inputString) { myString = inputString; } // Instance Method public void printString() { Console.WriteLine("{0}", myString); } // Destructor ~OutputClass() { // Some resource cleanup routines } } // Program start class class ExampleClass { // Main begins program execution. public static void Main() { // Instance of OutputClass OutputClass outCl = new OutputClass("This is printed by the output class."); // Call Output class' method outCl.printString(); } }
The #1 reason for programers / Gentoo users to be slacking of
by Mats Hellman on Feb.07, 2008, under Linux, Programing
I found this xckd comic very funny. Being a Linux user and using Gentoo from time to time. I compile some programs and in Gentoo, you should know if you tried it, you compile almost everything.
