TOC

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

The basics:

Events

ASP.NET is an event-driven way of making web applications. With PHP and Classic ASP, you have one file, which is executed line after line, from start to end. However, ASP.NET is very different. Here we have events, which are either activated by the user in one way or another. In the previous example, we used the Page_Load method. Actually, this is an event, which the Page class calls when the page is loaded. We will use the same technique in the next example, where we will add a couple of controls to our simple hello world example. To make it a bit more interesting, we will change the "world" word with something defined by the user. Have a look at this codelisting, where we add two new controls: A Button control and a TextBox control.

<form id="form1" runat="server">
<div>
    <asp:Label runat="server" id="HelloWorldLabel"></asp:Label>
    <br /><br />
    <asp:TextBox runat="server" id="TextInput" /> 
    <asp:Button runat="server" id="GreetButton" text="Say Hello!" />
</div>
</form>

Conforme se pode ver, os dois novos controlos foram adicionados, no entanto não são muito úteis como estão. Pode-se executar o exemplo para ver com os próprios olhos - ao clicar no botão, a página pura e simplesmente é actualizada. Vamos mudar isso, e para tal vamos começar por fazê-lo da forma mais fácil. O Visual Studio vem com um editor WYSIWYG (What You See Is What You Get ou "O que vê é o que tem", em Português) e, ainda que eu raramente o utilize, torna as coisas mais fáceis. Para criar eventos, por exemplo.

So, click the Design button in the bottom of VS. Now you will see a visual representation of our page. We wish to add a Click event to the button, and this is very simply - just doubleclick the GreetButton, and you will be taken to the CodeBehind file of our page. As you can see, a fine new method has been added, called GreetButton_Click. If you have a look at the Default.aspx file (you need to go from Design view to Source view), you will see that an attribute has been added to our Button control, telling which method to call when the button is clicked. All this work done with a simple doubleclick.

Agora vamos adicionar algum código ao recém criado evento. Vamos usar o texto "Hello, world" da TextBox para a nossa Label . Isto é muito simples e só necessita de uma linha de código.

HelloWorldLabel.Text = "Hello, " + TextInput.Text;

Run the project again (ctrl+F5), and you will see the our old page with a couple of new controls. The "Hello, world!" text is still there, because we set it in the Page_Load event. Now try entering a name in the textbox, and press the button. Voila, the text is changed, and we have just used our first control event. Notice how we can add code which is not necessarily called unless the user performs a specific task. This is different from the good old Classic ASP/PHP approach, but you will soon get used to it, and you will probably also come to like it a lot!


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!