分析BUTTON按钮点击触发COMMAND事件

Button.Command 事件

[C#]
public event CommandEventHandler Command;

[Visual Basic]
Public Event Command As CommandEventHandler


事件处理程序接收一个 CommandEventArgs 类型的参数,它包含与此事件相关的数据。下列 CommandEventArgs 属性提供特定于此事件的信息。

属性 说明
CommandName 获取命令的名称。
CommandArgument 获取命令的参数。


当在 Web 页上具有多个 Button 控件时,可使用 CommandName 属性来指定或确定与每一 Button 控件关联的命令名。然后,可以编程方式确定 Button 控件的命令名并执行相应的操作。


Button.CommandName 属性
获取或设置命令名,该命令名与传递给 Command 事件的 Button 控件相关联。默认值为 String.Empty。

[C#]
public string CommandName {get; set;}

[Visual Basic]
Public Property CommandName As String


Button.CommandArgument 属性
获取或设置可选参数,该参数与关联的 CommandName 一起被传递到 Command 事件。默认值为 String.Empty。

[C#]
public string CommandArgument {get; set;}

[Visual Basic]
Public Property CommandArgument As String


使用 CommandArgument 属性来指定补充 CommandName 属性的参数。
注意:尽管可以单独设置 CommandArgument 属性,但该属性通常在设置了 CommandName 属性时才使用。


None.gif [C#] 
ExpandedBlockStart.gifContractedBlock.gif
<% dot.gif @ Page Language="C#" AutoEventWireup="True"  %>
None.gif
None.gif
< html >
None.gif
< head >
None.gif
ExpandedBlockStart.gifContractedBlock.gif   
< script  runat ="server" > dot.gif
InBlock.gif
InBlock.gif      
void CommandBtn_Click(Object sender, CommandEventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif
InBlock.gif         
switch(e.CommandName)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif            
case "Sort":
InBlock.gif
InBlock.gif               
// Call the method to sort the list.
InBlock.gif
               Sort_List((String)e.CommandArgument);
InBlock.gif               
break;
InBlock.gif
InBlock.gif            
case "Submit":
InBlock.gif
InBlock.gif               
// Display a message for the Submit button being clicked.
InBlock.gif
               Message.Text = "You clicked the Submit button";
InBlock.gif
InBlock.gif               
// Test whether the command argument is an empty string ("").
InBlock.gif
               if((String)e.CommandArgument == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                  
// End the message.
InBlock.gif
                  Message.Text += ".";
ExpandedSubBlockEnd.gif               }

InBlock.gif               
else
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                  
// Display an error message for the command argument. 
InBlock.gif
                  Message.Text += ", however the command argument is not recogized.";
ExpandedSubBlockEnd.gif               }
                
InBlock.gif               
break;
InBlock.gif
InBlock.gif            
default:
InBlock.gif
InBlock.gif               
// The command name is not recognized. Display an error message.
InBlock.gif
               Message.Text = "Command name not recogized.";
InBlock.gif               
break
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
void Sort_List(string commandArgument)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif
InBlock.gif         
switch(commandArgument)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif            
case "Ascending":
InBlock.gif 
InBlock.gif               
// Insert code to sort the list in ascending order here.
InBlock.gif
               Message.Text = "You clicked the Sort Ascending button.";
InBlock.gif               
break;
InBlock.gif
InBlock.gif            
case "Descending":
InBlock.gif              
InBlock.gif               
// Insert code to sort the list in descending order here.
InBlock.gif
               Message.Text = "You clicked the Sort Descending button.";
InBlock.gif               
break;
InBlock.gif
InBlock.gif            
default:
InBlock.gif        
InBlock.gif               
// The command argument is not recognized. Display an error message.
InBlock.gif
               Message.Text = "Command argument not recogized.";
InBlock.gif               
break;
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
ExpandedSubBlockEnd.gif      }

InBlock.gif
ExpandedBlockEnd.gif   
</ script >
None.gif
None.gif
</ head >
None.gif 
None.gif
< body >
None.gif
None.gif   
< form  runat ="server" >
None.gif
None.gif      
< h3 > Button CommandName Example </ h3 >
None.gif
None.gif      Click on one of the command buttons.
None.gif
None.gif      
< br >< br >
None.gif 
None.gif      
< asp:Button  id ="Button1"
None.gif           Text
="Sort Ascending"
None.gif           CommandName
="Sort"
None.gif           CommandArgument
="Ascending"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif
None.gif      
&nbsp;
None.gif
None.gif      
< asp:Button  id ="Button2"
None.gif           Text
="Sort Descending"
None.gif           CommandName
="Sort"
None.gif           CommandArgument
="Descending"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif
None.gif      
< br >< br >
None.gif
None.gif      
< asp:Button  id ="Button3"
None.gif           Text
="Submit"
None.gif           CommandName
="Submit"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif
None.gif      
&nbsp;
None.gif
None.gif      
< asp:Button  id ="Button4"
None.gif           Text
="Unknown Command Name"
None.gif           CommandName
="UnknownName"
None.gif           CommandArgument
="UnknownArgument"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif
None.gif      
&nbsp;
None.gif
None.gif      
< asp:Button  id ="Button5"
None.gif           Text
="Submit Unknown Command Argument"
None.gif           CommandName
="Submit"
None.gif           CommandArgument
="UnknownArgument"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif       
None.gif      
< br >< br >
None.gif
None.gif      
< asp:Label  id ="Message"  runat ="server" />
None.gif 
None.gif   
</ form >
None.gif 
None.gif
</ body >
None.gif
</ html >
None.gif
None.gif
None.gif
None.gif[Visual Basic] 
ExpandedBlockStart.gifContractedBlock.gif
<% dot.gif @ Page Language="VB" AutoEventWireup="True"  %>
None.gif
None.gif
< html >
None.gif
< head >
None.gif
ExpandedBlockStart.gifContractedBlock.gif   
< script  runat ="server" > dot.gif
InBlock.gif
InBlock.gif      Sub CommandBtn_Click(sender As Object, e As CommandEventArgs) 
InBlock.gif
InBlock.gif         Select e.CommandName
InBlock.gif
InBlock.gif            Case 
"Sort"
InBlock.gif
InBlock.gif               ' Call the method to sort the list.
InBlock.gif               Sort_List(CType(e.CommandArgument, String))
InBlock.gif
InBlock.gif            Case 
"Submit"
InBlock.gif
InBlock.gif               ' Display a message 
for the Submit button being clicked.
InBlock.gif               Message.Text 
= "You clicked the Submit button"
InBlock.gif
InBlock.gif               ' Test whether the command argument is an empty string (
"").
InBlock.gif               If CType(e.CommandArgument , String) 
= "" Then
InBlock.gif              
InBlock.gif                  ' End the message.
InBlock.gif                  Message.Text 
&= "."
InBlock.gif               
InBlock.gif               Else
InBlock.gif               
InBlock.gif                  ' Display an error message 
for the command argument. 
InBlock.gif                  Message.Text 
&= ", however the command argument is not recogized."
InBlock.gif               
InBlock.gif               End If                
InBlock.gif
InBlock.gif            Case Else
InBlock.gif
InBlock.gif               ' The command name is not recognized. Display an error message.
InBlock.gif               Message.Text 
= "Command name not recogized."
InBlock.gif
InBlock.gif         End Select
InBlock.gif
InBlock.gif      End Sub
InBlock.gif
InBlock.gif      Sub Sort_List(commandArgument As String)
InBlock.gif
InBlock.gif         Select commandArgument
InBlock.gif
InBlock.gif            Case 
"Ascending"
InBlock.gif 
InBlock.gif               ' Insert code to sort the list 
in ascending order here.
InBlock.gif               Message.Text 
= "You clicked the Sort Ascending button."
InBlock.gif
InBlock.gif            Case 
"Descending"
InBlock.gif              
InBlock.gif               ' Insert code to sort the list 
in descending order here.
InBlock.gif               Message.Text 
= "You clicked the Sort Descending button."
InBlock.gif
InBlock.gif            Case Else
InBlock.gif        
InBlock.gif               ' The command argument is not recognized. Display an error message.
InBlock.gif               Message.Text 
= "Command argument not recogized."
InBlock.gif
InBlock.gif         End Select
InBlock.gif
InBlock.gif      End Sub
InBlock.gif
ExpandedBlockEnd.gif   
</ script >
None.gif
None.gif
</ head >
None.gif 
None.gif
< body >
None.gif
None.gif   
< form  runat ="server" >
None.gif
None.gif      
< h3 > Button CommandName Example </ h3 >
None.gif
None.gif      Click on one of the command buttons.
None.gif
None.gif      
< br >< br >
None.gif 
None.gif      
< asp:Button  id ="Button1"
None.gif           Text
="Sort Ascending"
None.gif           CommandName
="Sort"
None.gif           CommandArgument
="Ascending"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif
None.gif      
&nbsp;
None.gif
None.gif      
< asp:Button  id ="Button2"
None.gif           Text
="Sort Descending"
None.gif           CommandName
="Sort"
None.gif           CommandArgument
="Descending"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif
None.gif      
< br >< br >
None.gif
None.gif      
< asp:Button  id ="Button3"
None.gif           Text
="Submit"
None.gif           CommandName
="Submit"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif
None.gif      
&nbsp;
None.gif
None.gif      
< asp:Button  id ="Button4"
None.gif           Text
="Unknown Command Name"
None.gif           CommandName
="UnknownName"
None.gif           CommandArgument
="UnknownArgument"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif
None.gif      
&nbsp;
None.gif
None.gif      
< asp:Button  id ="Button5"
None.gif           Text
="Submit Unknown Command Argument"
None.gif           CommandName
="Submit"
None.gif           CommandArgument
="UnknownArgument"
None.gif           OnCommand
="CommandBtn_Click"  
None.gif           runat
="server" />
None.gif       
None.gif      
< br >< br >
None.gif
None.gif      
< asp:Label  id ="Message"  runat ="server" />
None.gif 
None.gif   
</ form >
None.gif 
None.gif
</ body >
None.gif
</ html >

转载于:https://www.cnblogs.com/xiaodi/archive/2005/03/31/129704.html

### 回答1: 在Python中,可以使用Tkinter库来创建GUI应用程序,并使用Button组件来创建按钮。要使按钮触发事件,可以使用command参数来指定一个函数或方法,当按钮点击时,该函数或方法将被调用。例如: ```python import tkinter as tk def button_click(): print("按钮点击了!") root = tk.Tk() button = tk.Button(root, text="点击我", command=button_click) button.pack() root.mainloop() ``` 在上面的示例中,我们创建了一个名为button_click的函数,并将其指定为按钮command参数。当按钮点击时,该函数将被调用,并输出一条消息。最后,我们使用mainloop()方法来启动GUI应用程序的事件循环。 ### 回答2: 在Python中,我们可以使用Tkinter模块创建GUI界面,并为按钮添加点击事件。 首先,需要导入Tkinter模块: ```python import tkinter as tk ``` 然后,创建一个窗口并设置大小: ```python window = tk.Tk() window.geometry("300x300") ``` 接着,创建一个按钮并设置它的文本和点击事件处理函数: ```python def button_clicked(): print("按钮点击了!") button = tk.Button(window, text="点击我", command=button_clicked) button.pack() ``` 在这里,我们创建了一个名为`button_clicked`的函数,它将被作为按钮点击事件响应。 接下来,我们为按钮设置了文本和点击事件处理函数,并通过`pack()`方法将按钮添加到窗口中。 最后,我们需要让窗口保持运行: ```python window.mainloop() ``` 完整代码如下: ```python import tkinter as tk def button_clicked(): print("按钮点击了!") window = tk.Tk() window.geometry("300x300") button = tk.Button(window, text="点击我", command=button_clicked) button.pack() window.mainloop() ``` 当你运行代码并点击按钮时,将会在控制台输出"按钮点击了!"。 ### 回答3: 在Python中,我们可以使用许多不同的图形用户界面(GUI)库来创建按钮并设置单击触发事件。以下是使用Tkinter库来创建按钮并设置单击事件的方法: 首先,导入Tkinter库 ```python import tkinter as tk ``` 创建主窗口并设置标题: ```python root = tk.Tk() root.title('Button Click Event in Python') ``` 创建一个函数来处理按钮单击事件: ```python def button_click(): # 这里可以编写按钮单击事件的代码 pass ``` 创建一个按钮并将其添加到窗口中: ```python button = tk.Button(root, text='Click me', command=button_click) button.pack() ``` 在这里,我们创建了一个名为“button”的按钮,使用文本“Click me”作为标签,并将其单击命令设置为“button_click”。最后,我们使用.pack()将按钮添加到主窗口中。 当用户单击按钮时,“button_click”函数将被调用,并且我们可以在该功能中执行任何操作。例如,我们可以在该函数中打印一条消息: ```python def button_click(): print('Button clicked!') ``` 要显示窗口并开始GUI应用程序的主循环,请使用以下代码: ```python root.mainloop() ``` 当用户单击按钮时,将会在Python控制台中打印“Button clicked!”的消息。当然,您可以在“button_click”函数中添加任何其他操作,例如更新文本标签、读取/写入文件或与数据库进行交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值