Events and delegates.

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...
 
A

avnrao

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.
 
B

bredal Jensen

I do in fact create the control when the very first time the
page loads . (IsPostBack==false ) in the Page_load event.
 
A

avnrao

then it should be working. are you concerned about not seeing your
teststring (OI).

put Response.End() after Response.Write

Av.
 
B

bredal Jensen

I did put a breakpoint in my event handler , but the code never breaks
there... Hummmm!
 
S

Sam

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
 
S

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.
 
B

bredal Jensen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top