translate VB -> CS

  • Thread starter Thread starter Eirik Eldorsen
  • Start date Start date
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
 
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.
 
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
 
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!
************************************************
 
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:
 
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
 
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...
 
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.
 
Ah! Understood! Makes much more sense.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
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.
 
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
 
Back
Top