关于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" %>



    Test Page


   


   

       
   

   

       
       
       
   

   

   

 

后台代码:

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;
        }
    }
}


 

 

你可能感兴趣的:(button,asp,command,object,server,string)