TOC

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

UserControls:

Events in UserControls

Trong chương trước, chúng ta đã xem và định nghĩa thuộc tính cho UserControl nhưng event có thể được khai báo và dùng! Nó cho phép bạn đóng gói các chức năng trong control và cho phép page hoạt động với các event kích hoạt bởi control nếu cần.

Trong chương này, chúng ta sẽ tạo ra một UserControl mới và đơn giản, để minh họa cách tạo event. Nó không phải là một ví dụ thực tế nhưng nó chỉ có nghĩa chỉ cho bạn cách dùng event trong UserControl. Nếu bạn không biết cách tạo và dùng UserControl thì hãy quay trở lại chương trước. Phần này tập trung vào event.

Đầu tiên, chúng ta tạo ra một EventUserControl mới với code bên trong:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EventUserControl.ascx.cs" Inherits="EventUserControl" %>
Page title: 
<asp:TextBox runat="server" ID="txtPageTitle" />
<asp:Button runat="server" ID="btnUpdatePageTitle" OnClick="btnUpdatePageTitle_Click" Text="Update" />

Chỉ là text và control mà chúng ta biết. Trong CodeBehind, nó giống như sau:

public partial class EventUserControl : System.Web.UI.UserControl
{
    private string pageTitle;
    public event EventHandler PageTitleUpdated;

    protected void btnUpdatePageTitle_Click(object sender, EventArgs e)
    {
        this.pageTitle = txtPageTitle.Text;
        if(PageTitleUpdated != null)
            PageTitleUpdated(this, EventArgs.Empty);
    }

    public string PageTitle
    {
        get { return pageTitle; }
    }
}

Chúng ta đã định nghĩa biến pageTitle và một thuộc tính cho nó. Sau đó chúng ta có một event. Như bạn thấy, nó được định nghĩa giống bất kỳ trường nào nhưng hơi khác. Lý thuyết trong C# nên tôi không đi sâu vào ở phần này.

Trong sự kiện Click của button, chúng ta thiết lập trường pageTitle. Sau đó chúng ta kiểm tra liệu PageTitleUpdated có null không. Nếu không, nó có nghĩa là chúng ta đã gửi event này đi đâu đó và trong trường hợp này chúng ta gửi thông báo bằng cách gọi phương thức PageTitleUpdated. Chúng ta gửi như tham số (tham chiếu tới UserControl) là sender và một EventArgs rỗng. Điều này đảm bảo rằng mọi subscirber được báo rằng pageTitle được cập nhật.

<%@ Register TagPrefix="My" TagName="EventUserControl" Src="~/EventUserControl.ascx" %>

And inserted it like this:

<My:EventUserControl runat="server" ID="MyEventUserControl" OnPageTitleUpdated="MyEventUserControl_PageTitleUpdated" />

As you can see, we have defined an event handler for the PageTitleUpdated event like if it was any other server control. In the CodeBehind of our page, we define the simple event handler for the UserControl event like this:

protected void MyEventUserControl_PageTitleUpdated(object sender, EventArgs e)
{
    this.Title = MyEventUserControl.PageTitle;
}

It's that simple! Now obviously, updating the page title could have been done directly from our UserControl, so it's just to provide a simple example on how to use events, but as you will likely learn later on, this technic will come in handy in lots of situations.


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!