小迪的Blog
学习ing...(注明:该Blog中的任何信息都非原创,只是作为个人的阅读笔记)
posts - 144,comments - 52,trackbacks - 0

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 属性时才使用。


[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   
<script runat="server">

      
void CommandBtn_Click(Object sender, CommandEventArgs e) 
      
{

         
switch(e.CommandName)
         
{

            
case "Sort":

               
// Call the method to sort the list.
               Sort_List((String)e.CommandArgument);
               
break;

            
case "Submit":

               
// Display a message for the Submit button being clicked.
               Message.Text = "You clicked the Submit button";

               
// Test whether the command argument is an empty string ("").
               if((String)e.CommandArgument == "")
               
{
                  
// End the message.
                  Message.Text += ".";
               }

               
else
               
{
                  
// Display an error message for the command argument. 
                  Message.Text += ", however the command argument is not recogized.";
               }
                
               
break;

            
default:

               
// The command name is not recognized. Display an error message.
               Message.Text = "Command name not recogized.";
               
break

         }


      }


      
void Sort_List(string commandArgument)
      
{

         
switch(commandArgument)
         
{

            
case "Ascending":
 
               
// Insert code to sort the list in ascending order here.
               Message.Text = "You clicked the Sort Ascending button.";
               
break;

            
case "Descending":
              
               
// Insert code to sort the list in descending order here.
               Message.Text = "You clicked the Sort Descending button.";
               
break;

            
default:
        
               
// The command argument is not recognized. Display an error message.
               Message.Text = "Command argument not recogized.";
               
break;

         }


      }


   
</script>

</head>
 
<body>

   
<form runat="server">

      
<h3>Button CommandName Example</h3>

      Click on one of the command buttons.

      
<br><br>
 
      
<asp:Button id="Button1"
           Text
="Sort Ascending"
           CommandName
="Sort"
           CommandArgument
="Ascending"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>

      
&nbsp;

      
<asp:Button id="Button2"
           Text
="Sort Descending"
           CommandName
="Sort"
           CommandArgument
="Descending"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>

      
<br><br>

      
<asp:Button id="Button3"
           Text
="Submit"
           CommandName
="Submit"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>

      
&nbsp;

      
<asp:Button id="Button4"
           Text
="Unknown Command Name"
           CommandName
="UnknownName"
           CommandArgument
="UnknownArgument"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>

      
&nbsp;

      
<asp:Button id="Button5"
           Text
="Submit Unknown Command Argument"
           CommandName
="Submit"
           CommandArgument
="UnknownArgument"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>
       
      
<br><br>

      
<asp:Label id="Message" runat="server"/>
 
   
</form>
 
</body>
</html>



[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   
<script runat="server">

      Sub CommandBtn_Click(sender As Object, e As CommandEventArgs) 

         Select e.CommandName

            Case 
"Sort"

               ' Call the method to sort the list.
               Sort_List(CType(e.CommandArgument, String))

            Case 
"Submit"

               ' Display a message 
for the Submit button being clicked.
               Message.Text 
= "You clicked the Submit button"

               ' Test whether the command argument is an empty string (
"").
               If CType(e.CommandArgument , String) 
= "" Then
              
                  ' End the message.
                  Message.Text 
&= "."
               
               Else
               
                  ' Display an error message 
for the command argument. 
                  Message.Text 
&= ", however the command argument is not recogized."
               
               End If                

            Case Else

               ' The command name is not recognized. Display an error message.
               Message.Text 
= "Command name not recogized."

         End Select

      End Sub

      Sub Sort_List(commandArgument As String)

         Select commandArgument

            Case 
"Ascending"
 
               ' Insert code to sort the list 
in ascending order here.
               Message.Text 
= "You clicked the Sort Ascending button."

            Case 
"Descending"
              
               ' Insert code to sort the list 
in descending order here.
               Message.Text 
= "You clicked the Sort Descending button."

            Case Else
        
               ' The command argument is not recognized. Display an error message.
               Message.Text 
= "Command argument not recogized."

         End Select

      End Sub

   
</script>

</head>
 
<body>

   
<form runat="server">

      
<h3>Button CommandName Example</h3>

      Click on one of the command buttons.

      
<br><br>
 
      
<asp:Button id="Button1"
           Text
="Sort Ascending"
           CommandName
="Sort"
           CommandArgument
="Ascending"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>

      
&nbsp;

      
<asp:Button id="Button2"
           Text
="Sort Descending"
           CommandName
="Sort"
           CommandArgument
="Descending"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>

      
<br><br>

      
<asp:Button id="Button3"
           Text
="Submit"
           CommandName
="Submit"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>

      
&nbsp;

      
<asp:Button id="Button4"
           Text
="Unknown Command Name"
           CommandName
="UnknownName"
           CommandArgument
="UnknownArgument"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>

      
&nbsp;

      
<asp:Button id="Button5"
           Text
="Submit Unknown Command Argument"
           CommandName
="Submit"
           CommandArgument
="UnknownArgument"
           OnCommand
="CommandBtn_Click" 
           runat
="server"/>
       
      
<br><br>

      
<asp:Label id="Message" runat="server"/>
 
   
</form>
 
</body>
</html>
posted on 2005-03-31 21:46  小迪  阅读(...)  评论(...编辑  收藏