Events and delegates.

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

bredal Jensen

Hello,
I have populated a web form with dynamically created radiobuttons in
a table. I wnat some of the radio buttons to fire events when their
..CheckChanged property has changed.
I understand and was told this can be done with events and delegates. But my
code would
not compile. 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 specialRadioEventHandler( DoSomething );

}



public voidDoSomething (object sender, EventArgs e)

{

//Only for testing purpose.

string teststring = "oi";

Response.Write(teststring);

}


.......

.......

.....



I'am i doing something wrong?





Thanks...
 
you have to create the radio button even in post back. So remove IsPostBack
line.

If you dont create the control, asp.net runtime will not have the control
and cant handle the event that it supposed to do.

Av.
 
I do in fact create the control when the very first time the
page loads . (IsPostBack==false ) in the Page_load event.
 
then it should be working. are you concerned about not seeing your
teststring (OI).

put Response.End() after Response.Write

Av.
 
I did put a breakpoint in my event handler , but the code never breaks
there... Hummmm!
 
You have to enable viewstate on the radio button and every control
above it in the control tree for this to work. Are you turning it off
somewhere?

-Sam
 
Oops. It just occured to me what your problem might be. Do
Page.Controls.Add(rb). If it complains about not in a Form tag then
declare a placeholder in the aspx page inside the Forms tags and do
Placholder.Controls.Add(rb) instead.
 
I'm already adding the controls to my table cells and i want them to
populate this dynamically created table.
I just need to know which one was clicked.
The do generate a post back, (thanks to: rb.AutoPostBack = true;) but no
events other then the OnLoad.
I need to take some action only when i know that a specific click event on
the radio button was generated.
Not just every postback event

Thanks
 
Back
Top