Beginning ASP.NET 1.1 with Visual C# .NET 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Beginning ASP.NET 1.1 with Visual C# .NET 1002003 [Electronic resources] - نسخه متنی

Chris Ullman

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید


Chapter 3, an event handler is essentially the code you write to respond to a particular event. For example, a
Button control raises a
Click event after being clicked; a
ListBox control raises a
SelectedIndexChanged event when its list selection changes; a
TextBox control raises a
TextChanged event whenever its text has changed and the active focus on the form changes, and so on. ASP.NET Web controls support the ability to assign event handlers that execute specific code in response to specific events raised by an ASP.NET Web control.


Events and event handlers are extremely useful to Web developers because they provide a mechanism for responding dynamically to events in our Web pages. For example, let''s say you were asked to write a page containing a button that listed the current date and time to the latest second. For demonstration purposes, when the user clicks on the button, you might want the date and time to be displayed as the button''s new text. To achieve this result, you''ll need to "wire up" an event handler for our
Button control. Let''s do this in Web Matrix.


Try It Out - Creating an Event Handler





Fire up Web Matrix and create a new ASP.NET page called
SimpleButton.aspx within your


C:\BegASPNET11\Ch10 folder. Switch to HTML view, and enter the following code:




<form runat="server">


<asp:Button id="CurrentTimeButton" runat="server"

Text="Click for current time..." OnClick="UpdateTime" />
</form>



Switch to Code view and add the following method to handle the clicking of the button:




void UpdateTime (object sender, EventArgs e)


{

// update the button text with current time

CurrentTimeButton.Text = DateTime.Now.ToLongTimeString();


}




If you run the code in your browser, you''ll see the button we created, and when you click on it you''ll see the current time, as shown in
Figure 10-4:






Figure 10-4

How It Works



In this example, we added a
Button control to a form and named it
CurrentTimeButton . We also set its
Text property to
Click for current time... , which is the first thing you see when you run the page:


<asp:Button id="CurrentTimeButton" runat="server"
Text="Click for current time..." OnClick="UpdateTime" />



In addition, we set the
OnClick property to
UpdateTime , the name of the method that we added to handle the
Click event. We then added the event handler to update the button text with the current time whenever the button is clicked:


void UpdateTime (object sender, EventArgs e)


{
// update the button text with current time
CurrentTimeButton.Text = DateTime.Now.ToLongTimeString();
}


The body of this method has just one line of code, which assigns the value
DateTime.Now.ToLongTimeString () to the
Text property of the
CurrentTimeButton . This is basically saying, "when the
UpdateTime() method is run, display the current time in the
Text property of
CurrentTimeButton (which is a string datatype)."



Note


The
DateTime class is a standard .NET class, which has a
Now() method that returns the current date and time. This date/time value can be displayed in many ways by using different methods, including
ToShortDateString() and
ToLongTimeString() .





/ 220