Export (0) Print
Expand All

Button.OnClick Method

Raises the Click event of the Button control.

[Visual Basic]
Protected Overridable Sub OnClick( _
   ByVal e As EventArgs _
)
[C#]
protected virtual void OnClick(
 EventArgs e
);
[C++]
protected: virtual void OnClick(
 EventArgs* e
);
[JScript]
protected function OnClick(
   e : EventArgs
);

Parameters

e
A System.EventArgs that contains the event data.

Remarks

The Click event is raised when the Button control is clicked. This event is commonly used when no command name is associated with the Button control (for instance, with a submit button).

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnClick method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors:  When overriding OnClick in a derived class, be sure to call the base class's OnClick method so that registered delegates receive the event.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to specify and code an event handler for the Click event in order to display a simple message on the Web page.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
 
<head>

   <script language="VB" runat=server>

    Sub SubmitBtn_Click(sender As Object, e As EventArgs)
        Message.Text = "Hello World!!"
    End Sub 'SubmitBtn_Click

  </script>

</head>
 
<body>
   <form runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <p>

      <asp:label id="Message" runat="server"/>
 
   </form>
 
</body>
</html>
   

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
 
<head>

   <script language="C#" runat=server>

      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         Message.Text="Hello World!!";
      }

   </script>

</head>
 
<body>
   <form runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <p>

      <asp:label id="Message" runat="server"/>
 
   </form>
 
</body>
</html>
   

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>
 
<head>

   <script language="JScript" runat=server>

      function SubmitBtn_Click(sender : Object, e : EventArgs) 
      {
         Message.Text="Hello World!!";
      }

   </script>

</head>
 
<body>
   <form runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <p>

      <asp:label id="Message" runat="server"/>
 
   </form>
 
</body>
</html>
   

[Visual Basic] 

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
 
<head>

   <script runat="server">

      Sub Page_Load(sender As Object, e As EventArgs)
   
         ' Manually register the event-handling method for the Click  
         ' event of the Button control.
         AddHandler Button1.Click, AddressOf SubmitBtn_Click
      
      End Sub

      Sub SubmitBtn_Click(sender As Object, e As EventArgs) 
     
         Message.Text="Hello World!!"
      
      End Sub

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Button Click Example</h3>

      Click the <b>Submit</b> button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           runat="server"/>
       
      <p>

      <asp:label id="Message" 
           runat="server"/>
 
   </form>
 
</body>

</html>


[C#] 

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

<html>
 
<head>

   <script runat="server">

      void Page_Load(Object sender, EventArgs e)
      {
         // Manually register the event-handling method for the Click  
         // event of the Button control.
         Button1.Click += new EventHandler(this.SubmitBtn_Click);
      }

      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         Message.Text="Hello World!!";
      }

   </script>

</head>
 
<body>

   <form runat="server">

      <h3>Button Click Example</h3>

      Click the <b>Submit</b> button.<br><br>
 
      <asp:Button id="Button1"
           Text="Submit"
           runat="server"/>
       
      <p>

      <asp:label id="Message" 
           runat="server"/>
 
   </form>
 
</body>

</html>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Button Class | Button Members | System.Web.UI.WebControls Namespace | Click | System.EventArgs

Show:
© 2014 Microsoft