asp.net - How to use Button Command event

Button - Command event and CommandName property
ButtonCommandNameExample.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button_Command(Object sender, System.Web.UI.WebControls.CommandEventArgs e) { 
        switch(e.CommandName)
        {
            case ("CheckedNow"):
                CheckBox1.Checked = true;
                break;
            case ("UnCheckedNow"):
                CheckBox1.Checked = false;
                break;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>asp.net Button example: how to use OnCommand and CommandName property</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Fuchsia">Using CommandName property</h2>
        <asp:CheckBox ID="CheckBox1" runat="server" Text="Click the button for checked or unchecked" ForeColor="Red" />
        <br /><br />
        <asp:Button ID="Button1" runat="server" Text="Check Now" OnCommand="Button_Command" CommandName="CheckedNow" Font-Bold="true" ForeColor="Teal" />
        <asp:Button ID="Button2" runat="server" Text="UnCheck Now" OnCommand="Button_Command" CommandName="UnCheckedNow" Font-Bold="true" ForeColor="Teal" />
    </div>
    </form>
</body>
</html>


Related asp.net example