AddHandler in C# doesn't work

  • Thread starter Thread starter Bjorn Sagbakken
  • Start date Start date
B

Bjorn Sagbakken

With ASP.NET 2.0:
In VB code I have added controls dynamically, then added a handler using
AddHandler(Object, Method)
It all works fine.

Now I am trying to do the same in a C# module, but the AddHandler() is not
recognized at all, even though I have tried to add "using
System.ComponentModel;"

I fell like I am missing some basic stuff here, references or ..

Anyone with an idea or a simple example?

Bjorn
 
Hi Bjorn,

In C# we use += operator to attach event handlers:

TextBox textBox = new TextBox();
textBox.ID = "txt1";
textBox.Text = "Howdy";
textBox.AutoPostBack = true;
tetxBox.TextChanged += new EventHandler(this.TextChangedHandler);

private void TextChangedHandler(object sender, EventArgs e)
{
// do something here
TextBox textBox = (TextBox) sender;
string text = textBox.Text;
//
}

hope this helps
 
Thanks a lot for the great help.

Bjorn

Milosz Skalecki said:
Hi Bjorn,

In C# we use += operator to attach event handlers:

TextBox textBox = new TextBox();
textBox.ID = "txt1";
textBox.Text = "Howdy";
textBox.AutoPostBack = true;
tetxBox.TextChanged += new EventHandler(this.TextChangedHandler);

private void TextChangedHandler(object sender, EventArgs e)
{
// do something here
TextBox textBox = (TextBox) sender;
string text = textBox.Text;
//
}

hope this helps
 
AddHandler() in VB is equivalent to:

controlToAddTo.EventToHandle += new EventHandler();

If you want to look up anything, check wiring delegates, or similar, as this
is fairly basic work with delegates (events are delegates in C# -- a bit of
an oversimplification, but it works until you get more advanced).

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

*************************************************
| Think outside the box!
|
*************************************************
 
Back
Top