How to use Button ClientClick event in asp.net


asp.net Button example: how to use ClientClick event

C# Code

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

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "Button1 Clicked!";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Label1.Text = "Button2 Clicked!";
        }
    }
}

HTML Code

<%@ Page Language="C#" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
    
    protected void Button1_Click(object sender, System.EventArgs e) {  
        Label1.Text = "Button1 Clicked!";  
    }
    
    protected void Button2_Click(object sender, System.EventArgs e) {  
        Label1.Text = "Button2 Clicked!";  
    }  
</script>  
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server"></asp:Label>  
        <br /><br />  
        <asp:Button ID="Button1" runat="server" Text="click me" OnClick="Button1_Click"/>  
        <asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click"/>  
    
    </div>
    </form>
</body>
</html>

Share this article on