473,805 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamically created radio buttons

I am working with some code where a number of contacts are taken from a
table and then added to a page with a radio button to the right of each
one where the name of each radio button has the contact key appended.

RadioButton rbtn = new RadioButton();
rbtn.ID = "btn" +
ds.Tables[0].Rows[i]["ContactKey "].ToString();
rbtn.GroupName = "Group1";

rbtn.CheckedCha nged += new
EventHandler(rb tn_CheckedChang ed);

if (ds.Tables[0].Rows[i]["PrimaryContact "].ToString() ==
"y")
{
rbtn.Checked = true;
}

pnlContactList. Controls.Add(rb tn);

How in the rbtn_CheckedCha nged event do I obtain the Id of the radio
button that is selected?
Cheers,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Apr 10 '06 #1
3 5183
You can normally cast the "sender" to the Control (RadioButton in this case)
firing the event; best to do it defensively:

RadioButton rb = sender as RadioButton;
if(rb!=null) { // have a valid RadioButton
// do something with rb
}

Marc

"Mike P" <mi*******@gmai l.com> wrote in message
news:eR******** ******@TK2MSFTN GP04.phx.gbl...
I am working with some code where a number of contacts are taken from a
table and then added to a page with a radio button to the right of each
one where the name of each radio button has the contact key appended.

RadioButton rbtn = new RadioButton();
rbtn.ID = "btn" +
ds.Tables[0].Rows[i]["ContactKey "].ToString();
rbtn.GroupName = "Group1";

rbtn.CheckedCha nged += new
EventHandler(rb tn_CheckedChang ed);

if (ds.Tables[0].Rows[i]["PrimaryContact "].ToString() ==
"y")
{
rbtn.Checked = true;
}

pnlContactList. Controls.Add(rb tn);

How in the rbtn_CheckedCha nged event do I obtain the Id of the radio
button that is selected?
Cheers,

Mike

*** Sent via Developersdex http://www.developersdex.com ***

Apr 10 '06 #2
Thanks Marc, that works perfectly.

I've never really used the 'as' keyword...is this just an alternative
form of casting?

*** Sent via Developersdex http://www.developersdex.com ***
Apr 10 '06 #3
The "as" operator returns null if the cast fails (e.g. sender is a TextBox),
where-as a straight cast would throw an exception.

The main reason I started using it is that FxCop kept moaning about
performing the cast twice when following the "test via is, then cast"
approach; "as" covers both of these in one, easily tested, step.

Marc

"Mike P" <mi*******@gmai l.com> wrote in message
news:eD******** ********@TK2MSF TNGP04.phx.gbl. ..
Thanks Marc, that works perfectly.

I've never really used the 'as' keyword...is this just an alternative
form of casting?

*** Sent via Developersdex http://www.developersdex.com ***

Apr 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2096
by: mortb | last post by:
I create a table containing radiobuttons in client script depending on what choices the user makes. It works fine the radio buttons appear *but* they are *not clickable*. Why? Is there a solution? I'm using IE 6.0 cheers, mortb actual code included (cut from a larger page):
2
13533
by: obsidian8 | last post by:
Hi All, I have looked around for an answer to this question, but haven't found one as of yet. I'm trying to use javascript to dynamically create raido buttons. I am able to create them easily enough but they aren't clickable. It doesn't matter if I set them as checked or not, you can't manipulate them at all. The code I'm using appears below: function test(){ componentMainTable = document.getElementById("mycomponent");
2
4427
by: Steve Black | last post by:
Hello, I am dynamically creating checkboxes on my web page based on data in a SQL Server table. Therefore, these checkboxes do not exist at design time and I cannot refer to them in my server-side code at design time. Once the page is submitted, how can I access these checkboxes in my code-behind? Thanks for the help,
1
3326
by: Karthick Kumar | last post by:
Hi, I have the following code which displays all the images from a specific folder with a Radio button in it: Dim objFile i = 1 For Each objFile In objFolder.Files If (i = 1) Then Response.Write("<TR>")
2
1340
by: Amelyan | last post by:
I *finally* narrowed down my problem to my custom web control. If I hookup event inside dynamically created custom web control, it doesn't get fired. However, event gets fired for any other standard dynamically created web control. the code below works in VS 2003 (radio_CheckedChanged get invoked), but it doesn't work in VS 2005. Is there something extra I need to override or add in VS2005 to make event fire?
2
11987
by: James P. | last post by:
Help, I need to display radio buttons on a form. The data is from SQL table: each row in each table is displayed as a radio button. I have multiple SQL tables so I understand I need to put them each in a GroupBox. All the examples I saw from the books or from the web show me how to add static radio buttons at design, or dynamically at run time but with fixed radio buttons (like from an array). I need to create radio buttons based...
2
1697
by: epigram | last post by:
I'm dynamically creating a number of radio buttons on my aspx page based upon data read from a db. Each radio button has autopostback turned on. I'm experiencing two problems. 1) I am reading the db, creating the radio buttons, and setting the checked property (also based upon data in the db) in Page_Load regardless of whether a postback is occurring or not. The page loads and displays correctly each time unless the database is updated...
7
4876
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 buttons and check box. I am adding two radio buttons once for a row.Now my problem is the radio buttons in all the rows that added dynamically are behaving as same group i.e when i am selecting a radio button in second row,radio button in first row...
2
3814
by: spooky | last post by:
Hi, I have a html table that has its rows dynamically created. My first column contains radio buttons. I want to get the row for which the radio button is selected. Spooky
5
6710
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 = ConfigurationManager.ConnectionStrings.ConnectionString; protected void Page_Load(object sender, EventArgs e) { try
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9179
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.