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

G

Guest

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 = 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!
 
S

Siva M

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...

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 = 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!
 
G

Guest

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

Siva M

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.

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++;
}
}
 
G

Guest

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 said:
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.

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 said:
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...

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 = 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!

 
S

Siva M

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


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 said:
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.

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 said:
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...

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 = 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!

 
G

Guest

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 said:
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;
}
}
}
}


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 said:
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.

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 said:
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...

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 = 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!


 

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

Top