13.07.2015 Views

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8: Control Events and Event Handlers 213Clicking the Clear button removes everything in the TextBox control except the word“Clear.” The string “Clear” is the assigned value of the CommandArgument property ina Button control tag as shown in the following code snippet:Even though the assignment appears to be a string, it still must be converted to a string inthe C# portion of the application. The following line makes that conversion:cmdArg = e.CommandArgument.ToString();Once that is done, you have successfully transferred the value of the CommandArgumentfrom the <strong>ASP</strong>.<strong>NET</strong> Web form to a C# string variable. Instead of using a simple string like“Clear,” you can also enter a more detailed <strong>com</strong>mand using Structured Query Language(SQL) such as the following:CommandArgument="INSERT ENames (eName,eMail) Values (@eName, @eMail)";By assigning different SQL statements to the web controls, you will be able to store queryand nonquery SQL <strong>com</strong>mands as values with the CommandArgument property in Buttonweb controls. (Chapters 11 and 12 explain using SQL <strong>com</strong>mands and working with data indatabase tables.)SummaryIn Chapters 5 and 6, the review of Web forms and web controls introduced events associatedwith the forms and controls. This chapter sought to isolate certain events and to look moreclosely at events and handling them. For the most part, the use of events and a control isa one-to-one operation. Each event has a unique event handler, and all C# code handlesthe event with a single method.In this chapter you saw that not only can events <strong>com</strong>e from unexpected places, butalso that a single event handler can deal with more than a single control. TextBox controlscan use Change events, and the Init event has similar properties and consequences as theLoad events found in different controls. Further, using Button web controls and <strong>com</strong>mandevents, you can use a single event handler to filter out the actions required for severaldifferent buttons. By assigning values to both CommandName and CommandArgumentproperties and using the C# CommandEventArgs object as a parameter in the eventhandler method, you can pass <strong>com</strong>mand values the <strong>ASP</strong>.<strong>NET</strong> Button controls to C#.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!