Selected state not maintained on postback

Discussion in 'ASP .Net Building Controls' started by Santel, Jun 12, 2007.

  1. Santel

    Santel Guest

    Hi,

    I tried to create a custom server control that displays some radio
    buttons and it should postback the page on selecting the items. I
    tried like below code, but on postback, the selected radio button
    state is not maintained. Anyone could tell me what is missing?

    public class WebCustomControl1 : RadioButtonList
    {
    protected override void Render(HtmlTextWriter output)
    {
    this.Items.Add(new ListItem("aaa"));
    this.Items.Add(new ListItem("bbb"));
    this.AutoPostBack = true;

    base.Render(output);
    }
    }
     
    Santel, Jun 12, 2007
    #1
    1. Advertisements

  2. Try moving those lines from the Render event to the Init event.
     
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider], Jun 12, 2007
    #2
    1. Advertisements

  3. Santel

    Santel Guest

    Hi Steve,

    Thanks for the reply. As it is custom control class, I couldn't see
    any Init event there. Could you tell me which one you are telling?
     
    Santel, Jun 13, 2007
    #3
  4. Santel

    Teemu Keiski Guest

    You'd need to wire an event hander for it. Same can be achieved by
    overriding OnInit method in your control.

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    //...
    this.Items.Add(new ListItem("aaa"));
    this.Items.Add(new ListItem("bbb"));
    this.AutoPostBack = true;

    }
     
    Teemu Keiski, Jun 13, 2007
    #4
    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.