473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1682
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Raghuram Banda | last post by:
Hi All, The following is the function I used to create RADIO buttons using DOM. It works fine with Netscape but not with IE. function addGroup3Radio() { var cellId =...
4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
6
by: Craig Keightley | last post by:
I have a page that has n number of radio groups (yes/No) how can i prevent the form being submitted if more than one radio group is not selected? By default all radio groups are unchecked ...
1
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected items using radio buttons, subtotal the costs and...
2
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on...
4
by: Blasting Cap | last post by:
I have a page that has a number of radio buttons that will be displayed to different access levels of a user who logs in to my website. For instance, if there are a dozen buttons, user1 will see...
2
by: Chris Monro | last post by:
Hi! I've created a UI control that adds(Creates) or subtracts(Destroys) standard buttons on a panel. I want to have each of these created buttons become accessible as UI elements, just as the...
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
5
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.