To actually handle the Command event, in the procedure of the event, you must write code that includes a conditional statement that will help you identify what actual control was clicked. To identify each control, the <asp:Button> tag is equipped with an attribute named CommandName. You must first create a name. If you are working visually, to create a name for a button, click that button on the form. In the Properties section of the Properties window, click CommandName and type a name, any name of your choice. You should type the same name for each button that will share this name. If you are working manually, add a CommandName attribute to each button. Make sure that each button that will belong to a group uses the same name. Here are examples: <%@ Page Language="C#" %> <html> <head> <script runat="server"> private void ButtonCommanded(object sender, CommandEventArgs e) { } </script> <title>Exercise</title> </head> <body> <form id="frmExercise" method="post" runat="server"> <ul style="list-style-type: none;"> <li><asp:Button id="btnVerify" Text="Verify" CommandName="first" OnCommand="ButtonCommanded" runat="server"></asp:Button></li> <li><asp:Button id="btnSend" Text="Send" CommandName="something" OnCommand="ButtonCommanded" runat="server"></asp:Button></li> <li><asp:Button id="btnRetrieve" Text="Retrieve" CommandName="first" OnCommand="ButtonCommanded" runat="server"></asp:Button></li> </ul> </form> </body> </html> Notice that the first and the third buttons use the same command name. After specifying the names, in the event, you would use a conditional statement to identify the group of controls by name and take the necessary action.
Normally, a button is an intermediary control. A web page visitor uses the other controls on a form, then clicks the button. That's where the button plays its role, and that's when you must intervene. A submit button is one that is implicitly configured to send the values of the form to the server that would take over in processing them, as opposed to a scenario where the values would be processed by the browser (also called the client). To create a submit button, the <asp:Button> tag is equipped with a Boolean attribute named UseSubmitBehavior. Its default behavior is True. This means that you don't have to write code for the button. On the other hand, if you want to handle the click event yourself, set this property to False. If you do this, when the web page is generated, the server would add the client-side code to it. As a result, this: <%@ Page Language="C#" %> <html> <head> <title>Exercise</title> </head> <body> <form id="frmExercise" method="post" runat="server"> <asp:Button id="btnExercise" Text="Exercise" UseSubmitBehavior="False" runat="server"></asp:Button> </form> </body> </html> would produce this: <html> <head> <title>Exercise</title> </head> <body> <form name="frmExercise" method="post" action="exercise.aspx" id="frmExercise"> <div> <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /> <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJMjI4MTU0NDAyZGTWj/v0HSNU5v6ZCxY12ZELsX3V2A==" /> </div> <script type="text/javascript"> //<![CDATA[ var theForm = document.forms['frmExercise']; if (!theForm) { theForm = document.frmExercise; } function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } } //]]> </script> <div> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLVxM1/Au6Su5EK2vUgSIgbHGO7Qj9mH+vWTQkeumQ=" /> </div> <input type="button" name="btnExercise" value="Exercise" onclick="javascript:__doPostBack('btnExercise','')" id="btnExercise" /> </form> </body> </html> |
|
|||||
|