Jon Glazer
Jon Glazer

Reputation: 785

Add PreRender Event to User Control in asp.net

I am converting a vb user control to c#. I don't remember how to bind an event to a control in c#. In vb its pretty straight forward with the Handles clause:

Protected Sub Control_PreRender(Sender As Object, e As EventArgs) Handles Me.PreRender

I cannot seem to work out how to do this in c# short of just creating the method and hope it links itself. Is this the way to go?

public void Control_PreRender(object sender, EventArgs e)

Upvotes: 1

Views: 2043

Answers (1)

Camilo Terevinto
Camilo Terevinto

Reputation: 32068

You got it almost right, the only problem is that events don't auto-attach themselves. You need to add this to the constructor of the control:

public MyControl()
{
    // your current code
    PreRender += Control_PreRender;
}

Upvotes: 2

Related Questions