LinkButton.OnCommand(CommandEventArgs) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
引发 LinkButton 控件的 Command 事件。
protected:
virtual void OnCommand(System::Web::UI::WebControls::CommandEventArgs ^ e);
protected virtual void OnCommand (System.Web.UI.WebControls.CommandEventArgs e);
abstract member OnCommand : System.Web.UI.WebControls.CommandEventArgs -> unit
override this.OnCommand : System.Web.UI.WebControls.CommandEventArgs -> unit
Protected Overridable Sub OnCommand (e As CommandEventArgs)
参数
包含事件数据的 CommandEventArgs。
示例
以下示例演示如何为事件指定和编码处理程序 Command ,以显示单击的 LinkButton 控件。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>LinkButton Command Event Example</title>
<script language="C#" runat="server">
void LinkButton_Command(Object sender, CommandEventArgs e)
{
Label1.Text = "You chose: " + e.CommandName + " Item " + e.CommandArgument;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>LinkButton Command Event Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Order Item 10001"
CommandName="Order"
CommandArgument="10001"
OnCommand="LinkButton_Command"
runat="server"/>
<br />
<asp:LinkButton id="LinkButton2"
Text="Order Item 10002"
CommandName="Order"
CommandArgument="10002"
OnCommand="LinkButton_Command"
Runat="server"/>
<br />
<br />
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>LinkButton Command Event Example</title>
<script language="VB" runat="server">
Sub LinkButton_Command(sender As Object, e As CommandEventArgs)
Label1.Text = "You chose: " & e.CommandName & " Item " & _
e.CommandArgument
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>LinkButton Command Event Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Order Item 10001"
CommandName="Order"
CommandArgument="10001"
OnCommand="LinkButton_Command"
runat="server"/>
<br />
<asp:LinkButton id="LinkButton2"
Text="Order Item 10002"
CommandName="Order"
CommandArgument="10002"
OnCommand="LinkButton_Command"
Runat="server"/>
<br />
<br />
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
注解
单击 Command 控件时将 LinkButton 引发 该事件。 当命令名称(如 Sort
)与 LinkButton 控件关联时,通常使用此事件。 这样,就可以在网页上创建多个 LinkButton 控件,并通过编程方式确定单击了哪个 LinkButton 控件。
备注
事件 Command 以 的形式 BubbleEvent
通过控件层次结构引发。
引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件。
OnCommand 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。
继承者说明
在派生类中重写 OnCommand(CommandEventArgs) 时,一定要调用基类的 OnCommand(CommandEventArgs) 方法,以便已注册的委托对事件进行接收。
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈