Events and delegates.

Discussion in 'ASP .Net' started by bredal Jensen, Apr 29, 2004.

  1. 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...
     
    bredal Jensen, Apr 29, 2004
    #1
    1. Advertisements

  2. bredal Jensen

    avnrao Guest

    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.
     
    avnrao, Apr 29, 2004
    #2
    1. Advertisements

  3. I do in fact create the control when the very first time the
    page loads . (IsPostBack==false ) in the Page_load event.
     
    bredal Jensen, Apr 29, 2004
    #3
  4. bredal Jensen

    avnrao Guest

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

    put Response.End() after Response.Write

    Av.
     
    avnrao, Apr 29, 2004
    #4
  5. I did put a breakpoint in my event handler , but the code never breaks
    there... Hummmm!
     
    bredal Jensen, Apr 29, 2004
    #5
  6. bredal Jensen

    Sam Guest

    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
     
    Sam, Apr 30, 2004
    #6
  7. bredal Jensen

    Sam Guest

    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.
     
    Sam, Apr 30, 2004
    #7
  8. 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
     
    bredal Jensen, Apr 30, 2004
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.