translate VB -> CS

E

Eirik Eldorsen

Could someone please help me translate this into C#?

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender



Eirik Eldorsen
 
K

Kevin Spencer

I could help you translate it, and I will, but I have to wonder what good
it's going to do you? One line of code does not a programmer make. If you
want to program in C#, I would suggest learning it. That said...

private void Page_PreRender(Object sender, System.EventArgs Val)

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
T

Teemu Keiski

Hi,

C# has no equivalent to VB's Handles statement, instead you need to wire the
event handler explicitly. To follow the event handler practise, you do it
like this:

1. Create the event handler method
========================
private void Page_PreRender(object sender, EventArgs e)
{
// Code goes here
}

2. Wire the event handler
=================
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.PreRender += new System.EventHandler(this.Page_PreRender);
}

Bit simpler way is to just override Page's OnPreRender method when all the
code you need is:
================================================================
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
//Code goes here
}

This is works because these every steps in Page lifecycle (the corresponding
events like Init, Load, PreRender) have corresponding On<EventName> method
(in Page/Control class) which is overridable (virtual in another words).
This also means that no explicit event wiring is needed.

Idea behind this is that with standard event pattern there's always this
overridable On<EventName> method which actually raises the event and when
you override the method your overriding method is guaranteed to run at this
time, but it is then important to remember to call the base class method
(the first line in my example) so that the event will originally be raised
(because it is raised by the base class method) and again the wired event
handlers (which the first example demonstrates as wiring one such) will be
executed.

Here is about the event pattern to give you the background information for
what I explained
http://msdn.microsoft.com/library/d...ide/html/cpconprovidingeventfunctionality.asp
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

The signature for the method is this:

private void Page_PreRender(Object sender, EventArgs e)
{
}

But, you have to create the event handler too:

this.PreRender += new System.EventHandler(this.Page_PreRender);


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
E

Eirik Eldorsen

I know C#. The problem is that I don't know VB. I'm translating a small
example.
What I did'nt understand in the VB code was:
 
E

Eirik Eldorsen

I know C#. The problem is that I don't know VB. I'm trying to translating a
small
example.
What I did'nt understand in the VB code was:
Handles MyBase.PreRender
 
T

Teemu Keiski

Handles is VB's alternative way to wire event handler explicitly to the
event.
 
J

Jo Inferis

Cowboy said:
But, you have to create the event handler too:

this.PreRender += new System.EventHandler(this.Page_PreRender);

Interestingly enough, you don't actually *have* to do this. At least not for
fundamental event handlers such as Page_Load, Page_PreRender and Page_Init
etc.

I've found you can even call the handlers things like Page_load or
Page_Prerender and they're still added to the list on the right events.

I'm not sure whether this is documented behaviour or not...
 
S

Scott Allen

Yes, this happens when the AutoEventWireup attribute is set to true
in the @Page directive. You would not want to set AutoEventWireup to
true and also add the event with code, the event will fire twice.
 
K

Kevin Spencer

Ah! Understood! Makes much more sense.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

Jo Inferis

Scott said:
Yes, this happens when the AutoEventWireup attribute is set to true
in the @Page directive. You would not want to set AutoEventWireup to
true and also add the event with code, the event will fire twice.

Hmm...perhaps there's something odd about my installation then?

I always explicitly use AutoEventWireUp="False", and it *still* works.
 
S

Scott Allen

The only other option is that Visual Studio .Net will automatically
wire up the Page_Load event for you in " Web Form Designer generated
code" region of the the code behind. I'm not sure how you could
automatically be getting the PreRender event.

--s
 
Joined
Sep 7, 2007
Messages
1
Reaction score
0
Kevin Spencer said:
I could help you translate it, and I will, but I have to wonder what good
it's going to do you? One line of code does not a programmer make. If you
want to program in C#, I would suggest learning it. That said...

private void Page_PreRender(Object sender, System.EventArgs Val)

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Eirik Eldorsen" <[email protected]> wrote in message
news:#[email protected]...
> Could someone please help me translate this into C#?
>
> Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles MyBase.PreRender
>
>
>
> Eirik Eldorsen
>
>

My god, it was a question, they didn't want a lecteur.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top