471,073 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Dynamicly created Radio Buttons

I have the following Code:

foreach (MyClass d in myClassCollection)
{
curRadioBtn = new RadioButton();
curRadioBtn.Text = d.ToString();
curRadioBtn.GroupName = "Contact";
curRadioBtn.ID = d.Name;
curRadioBtn.EnableViewState = true;
curRadioBtn.CheckedChanged += RadioButton_CheckedChanged;
curPanel = new Panel();
curPanel.Height = curRadioBtn.Height;
curPanel.Controls.Add(curRadioBtn);
panel1.Controls.Add(curPanel);
}

and I have two other RadioButtons on the form already, they are on Panel1.
The ones on the form cause RadioButton_CheckedChanged to be fired on a post
back, but the dynamiclly created ones don't.

The questions I have are:
1) How do I cause the dynamic radio buttons to fire the event?
2) How do I tell which one is selected, out side of firing the event?
I know I can get the ID in the event from the sender, just curious
on how to do it without the event.
3) When I do the post back all the dynamicly created RadioButtons go away,
how do I force the page to remember them when it's redisplayed without
having to re-create them?

Thanks
Wayne Sepega
Jan 5 '06 #1
2 1615
Hi,

there would be answers to those questions, however, have you considered
using easier approach, such as creating them with a Repeater?

Sample (though with textBoxes)
http://forums.asp.net/1158998/ShowPost.aspx

But quick replies:

1. Dynamic controls need to be added on every request and on request
(postback) expecting the events to be raised, they need to be added to
Controls collection at Load event, because postback data loading happens
last time (fixed stage) right after Load.

2. There's not very practical ways. It would need inspecting Request.Form
collection, and locating the control with key there (matching control's
UniqueID), and then getting reference to the instance with Page.FindControl

3. Either with repeater as I demonstrated (Repeater handles control
recreation dirty work), or then just you need to create them on postback,
because postback events won't be raised otherwise.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Wayne Sepega" <Me******@community.nospam> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
I have the following Code:

foreach (MyClass d in myClassCollection)
{
curRadioBtn = new RadioButton();
curRadioBtn.Text = d.ToString();
curRadioBtn.GroupName = "Contact";
curRadioBtn.ID = d.Name;
curRadioBtn.EnableViewState = true;
curRadioBtn.CheckedChanged += RadioButton_CheckedChanged;
curPanel = new Panel();
curPanel.Height = curRadioBtn.Height;
curPanel.Controls.Add(curRadioBtn);
panel1.Controls.Add(curPanel);
}

and I have two other RadioButtons on the form already, they are on Panel1.
The ones on the form cause RadioButton_CheckedChanged to be fired on a
post back, but the dynamiclly created ones don't.

The questions I have are:
1) How do I cause the dynamic radio buttons to fire the event?
2) How do I tell which one is selected, out side of firing the event?
I know I can get the ID in the event from the sender, just curious
on how to do it without the event.
3) When I do the post back all the dynamicly created RadioButtons go away,
how do I force the page to remember them when it's redisplayed without
having to re-create them?

Thanks
Wayne Sepega

Jan 5 '06 #2
Here are a few articles that will help to explain the sequence of things and
WHY controls need to be reloaded on each postback - and which event to use -
and other concepts relevant to your question.

http://aspnet.4guysfromrolla.com/articles/081402-1.aspx

http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

http://aspalliance.com/642

http://msdn.microsoft.com/library/de...nLifecycle.asp

http://www.15seconds.com/issue/020102.htm

http://www.eggheadcafe.com/articles/20050609.asp

http://msdn.microsoft.com/library/de...structures.asp

-HTH
"Wayne Sepega" <Me******@community.nospam> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
I have the following Code:

foreach (MyClass d in myClassCollection)
{
curRadioBtn = new RadioButton();
curRadioBtn.Text = d.ToString();
curRadioBtn.GroupName = "Contact";
curRadioBtn.ID = d.Name;
curRadioBtn.EnableViewState = true;
curRadioBtn.CheckedChanged += RadioButton_CheckedChanged;
curPanel = new Panel();
curPanel.Height = curRadioBtn.Height;
curPanel.Controls.Add(curRadioBtn);
panel1.Controls.Add(curPanel);
}

and I have two other RadioButtons on the form already, they are on Panel1.
The ones on the form cause RadioButton_CheckedChanged to be fired on a
post back, but the dynamiclly created ones don't.

The questions I have are:
1) How do I cause the dynamic radio buttons to fire the event?
2) How do I tell which one is selected, out side of firing the event?
I know I can get the ID in the event from the sender, just curious
on how to do it without the event.
3) When I do the post back all the dynamicly created RadioButtons go away,
how do I force the page to remember them when it's redisplayed without
having to re-create them?

Thanks
Wayne Sepega

Jan 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Oscar Monteiro | last post: by
6 posts views Thread by Craig Keightley | last post: by
2 posts views Thread by dpazza | last post: by
4 posts views Thread by Blasting Cap | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.