关于asp:Button控件的一些学习

Button可以是提交按钮也可以是命令按钮。即submit或者command

Button控件可以用来作为Web页面中的普通按钮。submit类型按钮用来把Web页面提交到服务器处理,没有从服务器返回的过程;command类型的按钮有一个相应的command名(通过CommandName属性设置该命令名字),当有多个command类型的按钮共享一个事件处理函数时,可以通过Command名字区分要出来哪个Button的事件。

当一个按钮即定义了onClick事件,又定义了onCommand事件的话,那么会怎样呢?

答案:两个方法都将执行,先执行onClick事件,后执行onCommand事件。

看一下下面的例子即可:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!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 runat="server">
    <title>Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" ID="LabelMessage" ForeColor="Red"></asp:Label>
    </div>
    <div>
        <asp:Button runat="server" ID="Button1" Text="Button1" OnClick="Button1_Click" OnCommand="Button_Command" CommandArgument="A" />
        <asp:Button runat="server" ID="Button2" Text="Button2" OnClick="Button2_Click" OnCommand="Button_Command" CommandArgument="B" />
        <asp:Button runat="server" ID="Button3" Text="Button3" OnClick="Button3_Click" OnCommand="Button_Command" CommandArgument="C" />
    </div>
    </form>
    <script type="text/javascript">
        function getMe() {
            alert('ok');
        }
    </script>
</body>
</html>

 

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Button1.Attributes.Add("onclick", "getMe();");                              //这句话用来给Button1添加客户端事件。
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string message = string.Format("The {0} event of {1} is fired","Click","Button1");
            this.LabelMessage.Text = message;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string message = string.Format("The {0} event of {1} is fired", "Click", "Button2");
            this.LabelMessage.Text = message;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string message = string.Format("The {0} event of {1} is fired", "Click", "Button3");
            this.LabelMessage.Text = message;
        }

        protected void Button_Command(object sender,CommandEventArgs e)
        {
            string message = string.Format("The {0} event of {1} is fired", "Command", e.CommandArgument);
            this.LabelMessage.Text += ";" + message;
        }
    }
}


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值