Selected state not maintained on postback

S

Santel

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);
}
}
 
S

Steve C. Orr [MCSD, MVP, CSM, ASP Insider]

Try moving those lines from the Render event to the Init event.
 
S

Santel

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?
 
T

Teemu Keiski

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;

}
 

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

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top