What is thre difference between OnClick and Click events?

K

kai

Hi,
In ASP.NET , what is the difference between OnClick and Click events for a
button? Because we have button click event, it can trigger events, why we
still need OnClick?

Please help.

Thanks

kai
 
C

Christopher Reed

OnClick is the method while Click is the event. In essence, they are the
same. The difference is in the execution.

If you set the OnClick attribute in your Button tag in the ASPX page, then
the event is automatically generated when the ASPX is compiled when the
AutoEventWireup attribute in the Page directive is set to true. For
example,

<asp:Button id="btnOne" runat="server" Text="Click Me!"
OnClick="btnOne_Click" />

On the other hand, if you do not set the OnClick attribute, you can always
define the Click event as long as you set the AutoEventWireup attribute in
the Page directive to false. In your code-behind, you would most likely do
something like this (in C#):

void Page_Init(Object objSender, EventArgs evtArgs)
{
...
btnOne.Click += new EventHandler(btnOne_Click);
...
}

For both, you have to create the method btnOne_Click:

void btnOne_Click(Object objSender, EventArgs evtArgs)
{
...
}

Hope this helps!
 
S

Steven Cheng[MSFT]

Thanks for Christopher 's inputs.

Hi Kai,

As Christopher has mentioned, OnXXX (suppose XXX is event name), is a
method of a certain control which can be override by derived class while
XXX is the event defined in the control...

Genearlly, when a certain event is to be fired, the control will call its
OnXXX method, and the base class's implementation will call all the event
handlers registered for that event ...... So for OnXXX method, we can
override it if we're developing a certain control or a derived one from
existing control.... While as for event handler, it provide the means
for ther control's user (page developer ) to hook the event and inject
some custom code when the event got fired......

Hope this is what you're wondering...

If there're anything we didn't quite get or anything else we can help,
please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




--------------------
| From: "Christopher Reed" <[email protected]>
| References: <cNnsf.1253$M%[email protected]>
| Subject: Re: What is thre difference between OnClick and Click events?
| Date: Tue, 27 Dec 2005 23:21:03 -0600
| Lines: 52
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| X-RFC2646: Format=Flowed; Response
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ip68-106-67-38.lu.dl.cox.net 68.106.67.38
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:367244
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| OnClick is the method while Click is the event. In essence, they are the
| same. The difference is in the execution.
|
| If you set the OnClick attribute in your Button tag in the ASPX page,
then
| the event is automatically generated when the ASPX is compiled when the
| AutoEventWireup attribute in the Page directive is set to true. For
| example,
|
| <asp:Button id="btnOne" runat="server" Text="Click Me!"
| OnClick="btnOne_Click" />
|
| On the other hand, if you do not set the OnClick attribute, you can
always
| define the Click event as long as you set the AutoEventWireup attribute
in
| the Page directive to false. In your code-behind, you would most likely
do
| something like this (in C#):
|
| void Page_Init(Object objSender, EventArgs evtArgs)
| {
| ...
| btnOne.Click += new EventHandler(btnOne_Click);
| ...
| }
|
| For both, you have to create the method btnOne_Click:
|
| void btnOne_Click(Object objSender, EventArgs evtArgs)
| {
| ...
| }
|
| Hope this helps!
| --
| Christopher A. Reed
| "The oxen are slow, but the earth is patient."
|
| | > Hi,
| > In ASP.NET , what is the difference between OnClick and Click events
for
| > a button? Because we have button click event, it can trigger events,
why
| > we still need OnClick?
| >
| > Please help.
| >
| > Thanks
| >
| > kai
| >
| >
| >
|
|
|
 
K

kai

Christopher,
Thanks!!!
Kai
Christopher Reed said:
OnClick is the method while Click is the event. In essence, they are the
same. The difference is in the execution.

If you set the OnClick attribute in your Button tag in the ASPX page, then
the event is automatically generated when the ASPX is compiled when the
AutoEventWireup attribute in the Page directive is set to true. For
example,

<asp:Button id="btnOne" runat="server" Text="Click Me!"
OnClick="btnOne_Click" />

On the other hand, if you do not set the OnClick attribute, you can always
define the Click event as long as you set the AutoEventWireup attribute in
the Page directive to false. In your code-behind, you would most likely
do something like this (in C#):

void Page_Init(Object objSender, EventArgs evtArgs)
{
...
btnOne.Click += new EventHandler(btnOne_Click);
...
}

For both, you have to create the method btnOne_Click:

void btnOne_Click(Object objSender, EventArgs evtArgs)
{
...
}

Hope this helps!
 
K

kai

Hi, Cheng
Thanks !!!
Kai
Steven Cheng said:
Thanks for Christopher 's inputs.

Hi Kai,

As Christopher has mentioned, OnXXX (suppose XXX is event name), is a
method of a certain control which can be override by derived class while
XXX is the event defined in the control...

Genearlly, when a certain event is to be fired, the control will call its
OnXXX method, and the base class's implementation will call all the event
handlers registered for that event ...... So for OnXXX method, we can
override it if we're developing a certain control or a derived one from
existing control.... While as for event handler, it provide the means
for ther control's user (page developer ) to hook the event and inject
some custom code when the event got fired......

Hope this is what you're wondering...

If there're anything we didn't quite get or anything else we can help,
please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




--------------------
| From: "Christopher Reed" <[email protected]>
| References: <cNnsf.1253$M%[email protected]>
| Subject: Re: What is thre difference between OnClick and Click events?
| Date: Tue, 27 Dec 2005 23:21:03 -0600
| Lines: 52
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| X-RFC2646: Format=Flowed; Response
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ip68-106-67-38.lu.dl.cox.net 68.106.67.38
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:367244
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| OnClick is the method while Click is the event. In essence, they are
the
| same. The difference is in the execution.
|
| If you set the OnClick attribute in your Button tag in the ASPX page,
then
| the event is automatically generated when the ASPX is compiled when the
| AutoEventWireup attribute in the Page directive is set to true. For
| example,
|
| <asp:Button id="btnOne" runat="server" Text="Click Me!"
| OnClick="btnOne_Click" />
|
| On the other hand, if you do not set the OnClick attribute, you can
always
| define the Click event as long as you set the AutoEventWireup attribute
in
| the Page directive to false. In your code-behind, you would most likely
do
| something like this (in C#):
|
| void Page_Init(Object objSender, EventArgs evtArgs)
| {
| ...
| btnOne.Click += new EventHandler(btnOne_Click);
| ...
| }
|
| For both, you have to create the method btnOne_Click:
|
| void btnOne_Click(Object objSender, EventArgs evtArgs)
| {
| ...
| }
|
| Hope this helps!
| --
| Christopher A. Reed
| "The oxen are slow, but the earth is patient."
|
| | > Hi,
| > In ASP.NET , what is the difference between OnClick and Click events
for
| > a button? Because we have button click event, it can trigger events,
why
| > we still need OnClick?
| >
| > Please help.
| >
| > Thanks
| >
| > kai
| >
| >
| >
|
|
|
 
S

Steven Cheng[MSFT]

You're welcome Kai,

If there're anything else we can help, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "kai" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| References: <cNnsf.1253$M%[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: What is thre difference between OnClick and Click events?
| Lines: 120
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <u1Bsf.1459$M%[email protected]>
| Date: Wed, 28 Dec 2005 18:43:38 GMT
| NNTP-Posting-Host: 66.32.80.38
| X-Complaints-To: (e-mail address removed)
| X-Trace: newsread3.news.atl.earthlink.net 1135795418 66.32.80.38 (Wed, 28
Dec 2005 10:43:38 PST)
| NNTP-Posting-Date: Wed, 28 Dec 2005 10:43:38 PST
| Organization: EarthLink Inc. -- http://www.EarthLink.net
| Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
ne.de!border2.nntp.dca.giganews.com!nntp.giganews.com!elnk-atl-nf1!newsfeed.
earthlink.net!stamper.news.atl.earthlink.net!newsread3.news.atl.earthlink.ne
t.POSTED!a96f5a29!not-for-mail
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:367367
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi, Cheng
| Thanks !!!
| Kai
| | > Thanks for Christopher 's inputs.
| >
| > Hi Kai,
| >
| > As Christopher has mentioned, OnXXX (suppose XXX is event name), is a
| > method of a certain control which can be override by derived class while
| > XXX is the event defined in the control...
| >
| > Genearlly, when a certain event is to be fired, the control will call
its
| > OnXXX method, and the base class's implementation will call all the
event
| > handlers registered for that event ...... So for OnXXX method, we can
| > override it if we're developing a certain control or a derived one from
| > existing control.... While as for event handler, it provide the
means
| > for ther control's user (page developer ) to hook the event and inject
| > some custom code when the event got fired......
| >
| > Hope this is what you're wondering...
| >
| > If there're anything we didn't quite get or anything else we can help,
| > please feel free to post here.
| >
| > Regards,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| > --------------------
| > | From: "Christopher Reed" <[email protected]>
| > | References: <cNnsf.1253$M%[email protected]>
| > | Subject: Re: What is thre difference between OnClick and Click
events?
| > | Date: Tue, 27 Dec 2005 23:21:03 -0600
| > | Lines: 52
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | X-RFC2646: Format=Flowed; Response
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: ip68-106-67-38.lu.dl.cox.net 68.106.67.38
| > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:367244
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | OnClick is the method while Click is the event. In essence, they are
| > the
| > | same. The difference is in the execution.
| > |
| > | If you set the OnClick attribute in your Button tag in the ASPX page,
| > then
| > | the event is automatically generated when the ASPX is compiled when
the
| > | AutoEventWireup attribute in the Page directive is set to true. For
| > | example,
| > |
| > | <asp:Button id="btnOne" runat="server" Text="Click Me!"
| > | OnClick="btnOne_Click" />
| > |
| > | On the other hand, if you do not set the OnClick attribute, you can
| > always
| > | define the Click event as long as you set the AutoEventWireup
attribute
| > in
| > | the Page directive to false. In your code-behind, you would most
likely
| > do
| > | something like this (in C#):
| > |
| > | void Page_Init(Object objSender, EventArgs evtArgs)
| > | {
| > | ...
| > | btnOne.Click += new EventHandler(btnOne_Click);
| > | ...
| > | }
| > |
| > | For both, you have to create the method btnOne_Click:
| > |
| > | void btnOne_Click(Object objSender, EventArgs evtArgs)
| > | {
| > | ...
| > | }
| > |
| > | Hope this helps!
| > | --
| > | Christopher A. Reed
| > | "The oxen are slow, but the earth is patient."
| > |
| > | | > | > Hi,
| > | > In ASP.NET , what is the difference between OnClick and Click
events
| > for
| > | > a button? Because we have button click event, it can trigger events,
| > why
| > | > we still need OnClick?
| > | >
| > | > Please help.
| > | >
| > | > Thanks
| > | >
| > | > kai
| > | >
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top