Questions about "Handles BlahBlahBlah" for codebehind

A

antonyliu2002

Right now I put my VB code in the aspx pages. I would prefer to use
codebehind, especially many of my VB functions may be shared across
multiple aspx pages.

I've tried a few pages with codebehind by follwing some examples on the
Web and they work just fine.

However, I am scared away from codebehind by such extenstions as
"Handles MyBase.Load", "Handles Button1.Click" and "Handles
MyBase.Init" etc. which we seem to have to add to the signatures of the
Subs or Functions we define.

So, here are my questions:

1. When do we need to add such "Handles BlahBlah" extenstions?

2. Which "Handles XXX" do we add to what kind Subs? What are the
criteria?

Thanks.
 
K

Kevin Spencer

It makes no difference whether it's in the CodeBehind or not. It's all just
one class definition. The "Handles" statements indicate that the Sub is an
Event Handler. It handles an event fired by a Control.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Complex things are made up of
lots of simple things.
 
A

antonyliu2002

Codebehind or not does seem to have a difference. I never use "Handle
BlahBlah" in my inline aspx pages. But it seems that I must explicitly
say "Handle BlahBlah" in codebehind to handle button clicks for
example. Otherwise, it's not gonna work.
 
K

Kevin Spencer

Okay, since you know better than me, I'd better just shut up and listen.

--

Kevin Spencer
Microsoft MVP
Professional Numbskull

Complex things are made up of
lots of simple things.
 
J

Juan T. Llibre

re:
That's fine, since you do not want to help anyway.

Please, let's limit our comments to technical issues.

re:
But it seems that I must explicitly say "Handle BlahBlah" in codebehind
to handle button clicks for example. Otherwise, it's not gonna work.

Not necessarily.

The VB language supports the "handles" statement -- which allows you to
wire-up the bindings to event handlers on the event handler methods themselves.

Although you can also explicitly wire-up event handlers using the server control
declaration (onclick="button1_click"), VB developers typically expect/prefer
using the Handles keyword instead.

C# as a language doesn't have a concept like the "handles" keyword.
Instead, you must explicitly wire-up event definitions.

i.e., VB developers can use either method but C# developers must explicitly wireup events.

You may set AutoEventWireup="true" for VB.Net.

In VS.Net 200x, this means that you can skip the "Handles ...." statement,
provided the method is named correctly.

E.g., if you have AutoEventWireup set to true, this method in a Codebehind file would handle the
Click Event of the Button Control with ID "Button1" without using onclick="Button1_Click"
declaratively :

Sub Button1_Click(ByVal s As Object, ByVal e As EventArg)
' Look, no Handles
End Sub

If you set AutoEventWireup="false", you would need to add the Handles:

Sub Button1_Click(ByVal s As Object, ByVal e As EventArg) Handles Me.Button1.Click
' Need Handles
End Sub

I hope this makes the issue a bit clearer for you.



Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
A

antonyliu2002

Hey, Juan, thanks a lot for your great education. I now know why I had
to say "handle blah blah". I thought that Kevin was grouchy for
something I said which I don't know, and I did not return to this group
for a while.

I've printed it out.

Thanks again.
 

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

Top