By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,382 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,382 IT Pros & Developers. It's quick & easy.

Urgent - How to access all radiobuttonlists in a loop (c# asp.net)

P: n/a
C# asp.net 2.0.

I'm creating an online survey. I'm making a string array that's holding the
variables of the answers made in the radiobuttonlists that i create manually.
I need to make a method that loops through all the radiobuttonlists and puts
the selected answers in the string array that i've created. I guess this is
relatively easy, but I haven't figured it out..here's the code that I have
for now:

protected void cmdSave_Click(object sender, EventArgs e)
{
string date = MakeMySqlDateTime();

int numberOfAnswers = 24; //Also the number of radiobuttonlists
string[] a = new string[numberOfAnswers]; //Variables to hold the
values until the insertion

//ALL THE RADIOBUTTONLISTS GOES HERE:
for (int i = 0; i <= numberOfAnswers; i++)
{
a[i] = HERE I NEED TO ACCESS ALL THE RADIOBUTTONLISTS
}
a1 = rbl1.SelectedValue.ToString();

//-----END OF RADIOBUTTONLISTS----

Can someone please give me some hints and code to help me?

Thanks!
Aug 18 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Try this (assuming the form is named as form1):

foreach (Control c in this.form1.Controls)
{
if (c is RadioButtonList)
{
Response.Write(((RadioButtonList)c).SelectedValue + "<br/>");
}
}

If you have nested RadioButtonList in any container controls then you will
have to run this loop recursively for each of those containers...

"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
C# asp.net 2.0.

I'm creating an online survey. I'm making a string array that's holding the
variables of the answers made in the radiobuttonlists that i create
manually.
I need to make a method that loops through all the radiobuttonlists and puts
the selected answers in the string array that i've created. I guess this is
relatively easy, but I haven't figured it out..here's the code that I have
for now:

protected void cmdSave_Click(object sender, EventArgs e)
{
string date = MakeMySqlDateTime();

int numberOfAnswers = 24; //Also the number of radiobuttonlists
string[] a = new string[numberOfAnswers]; //Variables to hold the
values until the insertion

//ALL THE RADIOBUTTONLISTS GOES HERE:
for (int i = 0; i <= numberOfAnswers; i++)
{
a[i] = HERE I NEED TO ACCESS ALL THE RADIOBUTTONLISTS
}
a1 = rbl1.SelectedValue.ToString();

//-----END OF RADIOBUTTONLISTS----

Can someone please give me some hints and code to help me?

Thanks!

Aug 18 '06 #2

P: n/a
Ok, that worked, thanks, but now I have another small issue...If the user
selected the "Other" option, he's supposed to write text into a textbox
beside the radiobuttonlist. How can I identify whict textbox to retrieve
from, from within the loop?
Here is the code:

int controlCounter = 0;

foreach (Control c in this.survey.Controls)
{
if (c is RadioButtonList)
{
a[controlCounter] = ((RadioButtonList)c).SelectedValue;
if (a[controlCounter].ToString() == "Other:")
{
//How can i determine which textbox to grab the text
from, I have several radiobuttonlists that contains the "Other:" option..
}
controlCounter++;
}
}

"Siva M" wrote:
Try this (assuming the form is named as form1):

foreach (Control c in this.form1.Controls)
{
if (c is RadioButtonList)
{
Response.Write(((RadioButtonList)c).SelectedValue + "<br/>");
}
}

If you have nested RadioButtonList in any container controls then you will
have to run this loop recursively for each of those containers...

"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
C# asp.net 2.0.

I'm creating an online survey. I'm making a string array that's holding the
variables of the answers made in the radiobuttonlists that i create
manually.
I need to make a method that loops through all the radiobuttonlists and puts
the selected answers in the string array that i've created. I guess this is
relatively easy, but I haven't figured it out..here's the code that I have
for now:

protected void cmdSave_Click(object sender, EventArgs e)
{
string date = MakeMySqlDateTime();

int numberOfAnswers = 24; //Also the number of radiobuttonlists
string[] a = new string[numberOfAnswers]; //Variables to hold the
values until the insertion

//ALL THE RADIOBUTTONLISTS GOES HERE:
for (int i = 0; i <= numberOfAnswers; i++)
{
a[i] = HERE I NEED TO ACCESS ALL THE RADIOBUTTONLISTS
}
a1 = rbl1.SelectedValue.ToString();

//-----END OF RADIOBUTTONLISTS----

Can someone please give me some hints and code to help me?

Thanks!

Aug 18 '06 #3

P: n/a
Well, the simple way is to check the ID radio button list (in case of
"Other") and get the associated text box value by hardcoding the textbox ID.

"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:91**********************************@microsof t.com...
Ok, that worked, thanks, but now I have another small issue...If the user
selected the "Other" option, he's supposed to write text into a textbox
beside the radiobuttonlist. How can I identify whict textbox to retrieve
from, from within the loop?
Here is the code:

int controlCounter = 0;

foreach (Control c in this.survey.Controls)
{
if (c is RadioButtonList)
{
a[controlCounter] = ((RadioButtonList)c).SelectedValue;
if (a[controlCounter].ToString() == "Other:")
{
//How can i determine which textbox to grab the text
from, I have several radiobuttonlists that contains the "Other:" option..
}
controlCounter++;
}
}

"Siva M" wrote:
Try this (assuming the form is named as form1):

foreach (Control c in this.form1.Controls)
{
if (c is RadioButtonList)
{
Response.Write(((RadioButtonList)c).SelectedValue + "<br/>");
}
}

If you have nested RadioButtonList in any container controls then you will
have to run this loop recursively for each of those containers...

"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
C# asp.net 2.0.

I'm creating an online survey. I'm making a string array that's holding
the
variables of the answers made in the radiobuttonlists that i create
manually.
I need to make a method that loops through all the radiobuttonlists and
puts
the selected answers in the string array that i've created. I guess this
is
relatively easy, but I haven't figured it out..here's the code that I have
for now:

protected void cmdSave_Click(object sender, EventArgs e)
{
string date = MakeMySqlDateTime();

int numberOfAnswers = 24; //Also the number of radiobuttonlists
string[] a = new string[numberOfAnswers]; //Variables to hold the
values until the insertion

//ALL THE RADIOBUTTONLISTS GOES HERE:
for (int i = 0; i <= numberOfAnswers; i++)
{
a[i] = HERE I NEED TO ACCESS ALL THE RADIOBUTTONLISTS
}
a1 = rbl1.SelectedValue.ToString();

//-----END OF RADIOBUTTONLISTS----

Can someone please give me some hints and code to help me?

Thanks!

Aug 18 '06 #4

P: n/a
But this is inside a loop, how can i hardcode the ID when I don't know whicth
radiobuttonlist the loop is at?

Is there another way of doing this?

"Siva M" wrote:
Well, the simple way is to check the ID radio button list (in case of
"Other") and get the associated text box value by hardcoding the textbox ID.

"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:91**********************************@microsof t.com...
Ok, that worked, thanks, but now I have another small issue...If the user
selected the "Other" option, he's supposed to write text into a textbox
beside the radiobuttonlist. How can I identify whict textbox to retrieve
from, from within the loop?
Here is the code:

int controlCounter = 0;

foreach (Control c in this.survey.Controls)
{
if (c is RadioButtonList)
{
a[controlCounter] = ((RadioButtonList)c).SelectedValue;
if (a[controlCounter].ToString() == "Other:")
{
//How can i determine which textbox to grab the text
from, I have several radiobuttonlists that contains the "Other:" option..
}
controlCounter++;
}
}

"Siva M" wrote:
Try this (assuming the form is named as form1):

foreach (Control c in this.form1.Controls)
{
if (c is RadioButtonList)
{
Response.Write(((RadioButtonList)c).SelectedValue + "<br/>");
}
}

If you have nested RadioButtonList in any container controls then you will
have to run this loop recursively for each of those containers...

"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
C# asp.net 2.0.

I'm creating an online survey. I'm making a string array that's holding
the
variables of the answers made in the radiobuttonlists that i create
manually.
I need to make a method that loops through all the radiobuttonlists and
puts
the selected answers in the string array that i've created. I guess this
is
relatively easy, but I haven't figured it out..here's the code that I have
for now:

protected void cmdSave_Click(object sender, EventArgs e)
{
string date = MakeMySqlDateTime();

int numberOfAnswers = 24; //Also the number of radiobuttonlists
string[] a = new string[numberOfAnswers]; //Variables to hold the
values until the insertion

//ALL THE RADIOBUTTONLISTS GOES HERE:
for (int i = 0; i <= numberOfAnswers; i++)
{
a[i] = HERE I NEED TO ACCESS ALL THE RADIOBUTTONLISTS
}
a1 = rbl1.SelectedValue.ToString();

//-----END OF RADIOBUTTONLISTS----

Can someone please give me some hints and code to help me?

Thanks!

Aug 18 '06 #5

P: n/a
foreach (Control c in this.form1.Controls)
{
if (c is RadioButtonList)
{

if ((RadioButtonList)c).SelectedValue == "Other")
{
switch (c.ID)
{
case "CityList":
otherCity = txtCity.Text;
break;
case "FavoriteAuthorList":
otherFavAuthor = txtFavAuthor.Text;
break;
}
}
}
}
"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:D0**********************************@microsof t.com...
But this is inside a loop, how can i hardcode the ID when I don't know
whicth
radiobuttonlist the loop is at?

Is there another way of doing this?

"Siva M" wrote:
Well, the simple way is to check the ID radio button list (in case of
"Other") and get the associated text box value by hardcoding the textbox
ID.

"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:91**********************************@microsof t.com...
Ok, that worked, thanks, but now I have another small issue...If the user
selected the "Other" option, he's supposed to write text into a textbox
beside the radiobuttonlist. How can I identify whict textbox to retrieve
from, from within the loop?
Here is the code:

int controlCounter = 0;

foreach (Control c in this.survey.Controls)
{
if (c is RadioButtonList)
{
a[controlCounter] = ((RadioButtonList)c).SelectedValue;
if (a[controlCounter].ToString() == "Other:")
{
//How can i determine which textbox to grab the text
from, I have several radiobuttonlists that contains the "Other:" option..
}
controlCounter++;
}
}

"Siva M" wrote:
Try this (assuming the form is named as form1):

foreach (Control c in this.form1.Controls)
{
if (c is RadioButtonList)
{
Response.Write(((RadioButtonList)c).SelectedValue + "<br/>");
}
}

If you have nested RadioButtonList in any container controls then you
will
have to run this loop recursively for each of those containers...

"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
C# asp.net 2.0.

I'm creating an online survey. I'm making a string array that's holding
the
variables of the answers made in the radiobuttonlists that i create
manually.
I need to make a method that loops through all the radiobuttonlists and
puts
the selected answers in the string array that i've created. I guess this
is
relatively easy, but I haven't figured it out..here's the code that I
have
for now:

protected void cmdSave_Click(object sender, EventArgs e)
{
string date = MakeMySqlDateTime();

int numberOfAnswers = 24; //Also the number of radiobuttonlists
string[] a = new string[numberOfAnswers]; //Variables to hold
the
values until the insertion

//ALL THE RADIOBUTTONLISTS GOES HERE:
for (int i = 0; i <= numberOfAnswers; i++)
{
a[i] = HERE I NEED TO ACCESS ALL THE RADIOBUTTONLISTS
}
a1 = rbl1.SelectedValue.ToString();

//-----END OF RADIOBUTTONLISTS----

Can someone please give me some hints and code to help me?

Thanks!

Aug 18 '06 #6

P: n/a
Thanks, that was excactly what i was started on :)

but damn...I thought that i could make this totally automatic with a few
loops..

"Siva M" wrote:
foreach (Control c in this.form1.Controls)
{
if (c is RadioButtonList)
{

if ((RadioButtonList)c).SelectedValue == "Other")
{
switch (c.ID)
{
case "CityList":
otherCity = txtCity.Text;
break;
case "FavoriteAuthorList":
otherFavAuthor = txtFavAuthor.Text;
break;
}
}
}
}
"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:D0**********************************@microsof t.com...
But this is inside a loop, how can i hardcode the ID when I don't know
whicth
radiobuttonlist the loop is at?

Is there another way of doing this?

"Siva M" wrote:
Well, the simple way is to check the ID radio button list (in case of
"Other") and get the associated text box value by hardcoding the textbox
ID.

"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:91**********************************@microsof t.com...
Ok, that worked, thanks, but now I have another small issue...If the user
selected the "Other" option, he's supposed to write text into a textbox
beside the radiobuttonlist. How can I identify whict textbox to retrieve
from, from within the loop?
Here is the code:

int controlCounter = 0;

foreach (Control c in this.survey.Controls)
{
if (c is RadioButtonList)
{
a[controlCounter] = ((RadioButtonList)c).SelectedValue;
if (a[controlCounter].ToString() == "Other:")
{
//How can i determine which textbox to grab the text
from, I have several radiobuttonlists that contains the "Other:" option..
}
controlCounter++;
}
}

"Siva M" wrote:
Try this (assuming the form is named as form1):
>
foreach (Control c in this.form1.Controls)
{
if (c is RadioButtonList)
{
Response.Write(((RadioButtonList)c).SelectedValue + "<br/>");
}
}
>
If you have nested RadioButtonList in any container controls then you
will
have to run this loop recursively for each of those containers...
>
"Svein Erik" <Sv*******@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
C# asp.net 2.0.
>
I'm creating an online survey. I'm making a string array that's holding
the
variables of the answers made in the radiobuttonlists that i create
manually.
I need to make a method that loops through all the radiobuttonlists and
puts
the selected answers in the string array that i've created. I guess this
is
relatively easy, but I haven't figured it out..here's the code that I
have
for now:
>
protected void cmdSave_Click(object sender, EventArgs e)
{
string date = MakeMySqlDateTime();
>
int numberOfAnswers = 24; //Also the number of radiobuttonlists
string[] a = new string[numberOfAnswers]; //Variables to hold
the
values until the insertion
>
//ALL THE RADIOBUTTONLISTS GOES HERE:
for (int i = 0; i <= numberOfAnswers; i++)
{
a[i] = HERE I NEED TO ACCESS ALL THE RADIOBUTTONLISTS
}
a1 = rbl1.SelectedValue.ToString();
>
//-----END OF RADIOBUTTONLISTS----
>
Can someone please give me some hints and code to help me?
>
Thanks!
>
>

Aug 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.