TOC

This article is currently in the process of being translated into Vietnamese (~28% done).

The basics:

CodeBehind

Trong khi ví dụ đầu tiên của chúng ta ổn, không may là nó đã phá vỡ nguyên tắc code của ASP.NET: chia tách markup và code. Như bạn đã chú ý, chúng ta đã thêm một khối script (sử dụng <% %>), nơi chúng ta viết một dòng code C# để sử dụng Label. Điều này ổn khi viết một ví dụ nhỏ, nhưng chúng ta sẽ sớm bị rối tung lên với một đống code C# trộn lẫn với một lượng lớn HTML code. Nếu bạn thêm vào cả JavaScript và CSS nữa, mọi thứ sẽ trở nên hỗn loạn mỗi khi sửa chữa. Đó là lý do MS giới thiệu CodeBehind, một kỹ thuật cho phép bạn tách biệt hoàn toàn markup (HTML, CSS, v..v..) với code (C#, VB.NET etc). Giờ hãy cùng nhau bỏ đoạn script (from <% to %>) và lưu lại.

As we talked about earlier, VS added a file called Default.aspx.cs. If you can't see it in the Solution Explorer, then click the little plus sign left of the Default.aspx file. Open this file. Now, if you haven't worked with .NET or another non-web programming language before, it might look a bit scary at this point. It looks nothing like HTML. However, I will try to explain the different parts of the content, and soon you will hopefully see that CodeBehind is a great tool to get a better overview of your work. Here is a complete listing of the file as it looks right now:

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)
    {

    }
}

For now, the top part is rather irrelevant to us. It's a list of namespaces being included with the using keyword, for usage in the file. Since this is an ASP.NET tutorial and not a dedicated C# tutorial, I won't explain this in depth. Next, we have the class. Classes are a part of the concept of Object Oriented programming, which has become very popular, especially with languages like Java and C#. OO is a very complex subject, which also won't be explained within this tutorial.

The name of this class is "_Default", and the : (colon) tells us that this class inherits from the Page class in the System.Web.UI namespace. This means that our page can already do a bunch of things, without any programming, because it inherits methods and properties from another class. All ASP.NET pages inherits from the Page class, or another class which inherits from the Page class.

The only method within this class is the Page_Load, which is called everytime the page is loaded. Let's use that to our advantage, and set the ext from this method. We can use the exact same line of code as before, but of course without the script block tags. Add the line of code between the { and } characters:

HelloWorldLabel.Text = "Hello, world!";

That's it. Run the project (F5), and have a look. The page looks exactly like before, but we have just used CodeBehind for the first time. But this example is starting to get a bit old, so in the next chapter, we will look into something a bit more interesting.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!