分析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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值