Button Control
After the button control is executed, it is parsed as <input type = "Submit">CodeThe advantage of the button control is thatProgramParameters can be passed at the same time. The passing parameters are completed by the commandname and commandargument attributes.
Here is an example of two button controls.
<% @ Page Language = "C #" %>
<Script language = "C #" runat = "server">
Public void button_click (Object sender,EventargsE)
{
String argname = (button) sender). commandname;
// Sender is an object, so commandargument is a string
String argarg = (button) sender). commandargument;
Label1.text = "the action you selected is: <font color = Red>" + argname + "</font>. The action target is: <font color = Red> "+ argarg +" </font> ";
}
</SCRIPT>
<HTML>
<Head>
<Title> </title>
</Head>
<Body>
<B> button control demonstration </B>
<HR>
<Form runat = "server">
<Asp: button id = "button1" commandname = "" commandargument = "" text = "Action 1" runat = "server" onclick = "button_click"/> <br>
<Asp: button id = "button2" commandname = "" commandargument = "basketball" text = "Action 2" runat = "server" onclick = "button_click"/> <br>
<HR>
<Asp: Label id = "label1" runat = "server"/>
</Form>
</Body>
</Html>
////////////////////////////////////
<% @ Page Language = "C #" %>
<Script language = "C #" runat = "server">
Public void button_click (Object sender,CommandeventargsE)
{
String argname = E. commandname;
// Commandeventargsr is a class, so commandargument. tostring ();
String argarg = E. commandargument. tostring ();
Label1.text = "the action you selected is: <font color = Red>" + argname + "</font>. The action target is: <font color = Red> "+ argarg +" </font> ";
}
</SCRIPT>
<HTML>
<Head>
<Title> </title>
</Head>
<Body>
<B> button control demonstration </B>
<HR>
<Form runat = "server">
<Asp: button id = "button1" commandname = "" commandargument = "" text = "Action 1" runat = "server" oncommand = "button_click"/> <br>
<Asp: button id = "button2" commandname = "" commandargument = "basketball" text = "Action 2" runat = "server" oncommand = "button_click"/> <br>
<HR>
<Asp: Label id = "label1" runat = "server"/>
</Form>
</Body>
</Html>
Analysis: 1. Two programs use different methods to accomplish the same functions and effects.
2.The commandeventargs class has two common attributes: commandname (used to obtain the command name) and commandargument (used to obtain the command parameters ). The two are interrelated.
3. The two programs use onclick and oncommand to stimulate events respectively. The difference is that oncommand is passed with values.
4. All Web controls (such as imagebutton and linkbutton) of the button class contain the commandname and commandargument attributes.
Problem: parameters used by the above two programsWhat is the difference between commandeventargs and eventargs? Why is it used in the first program?Eventargs,But cannot useCommandeventargs(It has been verified that the overload of "button_click" does not match the delegate "system. eventhandler)?
Example: http://hi.baidu.com/fuhengyu/blog/item/b37e14fb3e6055264e4aea65.html
Solution: query the information on the Internet and obtain the following results:
This type of problem occurs because the function is called in some events of the control, and the parameters of these functions are different from the parameter types used in the event. Search for onpageindexchanging, onpageindexchanged, and other keywords in msdn:
C #
Protected virtual void onpageindexchanged (eventargs E)
C #
Protected virtual void onpageindexchanging (detailsviewpageeventargs E)
C #
Protected virtual void onrowdeleting (gridviewdeleteeventargs E)
C #
Protected virtual void onrowdeleted (datarowchangeeventargs E)
In this way, the types of parameters used in the event are clearly visible. The same applies to onrowdeleted and onrowdeleting.
Event:
Public void mylist_page (Object sender, gridviewpageeventargs E)
Public void mydatagrid_delete (Object sender, gridviewdeleteeventargs E)