Delgate help.

  • Thread starter Thread starter bredal Jensen
  • Start date Start date
B

bredal Jensen

Hello gurus,

I need to dynamically create event handlers for my dynamically created radio
buttons in
an aspx.cs page.


The event should be fired when a user select the radio button.
I set the "autoposback" property to true for the buttons that need
to fire events when they are selected .

How can i do this with delegates?


Thanks in advance


Jensen
 
Hi ,

It's very easy, I assume you have one or more method to handle the events
of the radiobuttons. They need to have the correct signature :
void MethodName( object sender, EventArgs e) which correspond to the
EventHandler delegate.

At the creation time you can assign the correct method to the new control:
RadioButton rb = new RadioButton();
rb.CheckedChanged += new EventHandler( MethodName );

Will do the trick, as you may have more than one objects using the same
handler you must use the sender parameter to know which object sent the
event.


Cheers,
 
Thanks a lot...

Ignacio Machin ( .NET/ C# MVP ) said:
Hi ,

It's very easy, I assume you have one or more method to handle the events
of the radiobuttons. They need to have the correct signature :
void MethodName( object sender, EventArgs e) which correspond to the
EventHandler delegate.

At the creation time you can assign the correct method to the new control:
RadioButton rb = new RadioButton();
rb.CheckedChanged += new EventHandler( MethodName );

Will do the trick, as you may have more than one objects using the same
handler you must use the sender parameter to know which object sent the
event.


Cheers,
 
The code is actually not doing what i excpected.

Could you please put up a complete code listing assuming i
only have a method to handle the CheckChanged event?


Thanks
 
Here is what i did:

public delegate void specialRadioEventHandler(object sender, EventArgs e);

....

....

....





private void BuildMyDialog()

{

//Dynamically creating my table . Function called in
page_Load(IsPostBack==false)

....

....

....

RadioButton rb = new RadioButton();

if (RadioMeetMycodnition)

{

rb.AutoPostBack = true;

rb.CheckedChanged+=new MyOwnEventHandler( DoSomething );

}



public voidDoSomething (object sender, EventArgs e)

{

//Only for testing purpose.

string teststring = "oi";

Response.Write(teststring);

}






Can you spot out something wrong?
 
Back
Top