TOC

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

The basics:

More events

حسنا, الحدث "عند النقر " من الفصل الأخير كان سهلا, ولكن دعنا نحاول كتابة كل الشفرة المطلوبة لاستخدام أي حدث من البداية. وأيضا سوف نضيف متحكم أخر لجعل الأشياء أكثر إفادة - قائمة الأنزلاق - والتي تعطي إمكانية للمستخدم إختيار أي عنصر من القائمة. أضف الشفرة الأتية في أي مكان في الملف Default.aspx.

<asp:DropDownList runat="server" id="GreetList" autopostback="true">
    <asp:ListItem value="no one">No one</asp:ListItem>
    <asp:ListItem value="world">World</asp:ListItem>
    <asp:ListItem value="universe">Universe</asp:ListItem>
</asp:DropDownList>

This thing works just like a normal HTML SELECT element, which is of course what it's translated into upon rendering. The only attribute that would seem new to a person with basic HTML experience, is the autopostback. You will learn more about postbacks in one of the next chapters, but for now, just know that it makes the control contact the server eachtime an item is selected by the user. We will use this to our benefit now, by adding an event:

<asp:DropDownList runat="server" id="GreetList" autopostback="true" onselectedindexchanged="GreetList_SelectedIndexChanged">

We are using the onselectedindexchanged event, and assigning a method from CodeBehind which does not yet exist. You are free to choose the name of the method, but using a convention with the name of the control, an underscore, and then the name of the event, helps you keep track of it all. We better go create the event, so change to the Default.aspx.cs file, and add the following method:

protected void GreetList_SelectedIndexChanged(object sender, EventArgs e)
{
    HelloWorldLabel.Text = "Hello, " + GreetList.SelectedValue;
}

مرة أخرى, سوف نجعلها سهلة للغاية. سنستخدم خاصية SelectedValue لقائمة الانزلاق, والتي تحمل نص خاصية value للعنصر المختار. حاول فتح الموقع واختيار أي عنصر من قائمة الإنزلاق. جيد؟ كل المتحكمات العامة تأتي بمجموعة أحداث مفيدة حيث يمكنك استخدامها مثل هذا:


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!