Question

Please help. I need to get a list of all questions and possible answers from my .net Wizard control. Here is my attempt:

foreach (WizardStep step in Wizard1.WizardSteps)
        {
            foreach (Control c1 in step.Controls)
            {
                if (c1 is Label)
                {
                    Label1.Text += ((Label)c1).Text + "<br/><br/>";
                }

                //foreach (Control c2 in step.Controls)
                //{
                //    foreach (RadioButtonList rbl in step.Controls)
                //    {
                //        foreach (ListItem li in Items)
                //        {
                //            Label1.Text += li.Text.ToString() + "<br/><br/>";
                //        }
                //    }
                //}
            }
        }

This code works in that it gets all the questions. But when I uncomment the commented bit to get the possible radiobuttonlist answers, it fails. I get an error: "Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.RadioButtonList'."

I can sort of understand why this happens but I don't know how to fix. Help is much appreciated.

kindest regards Paul

Was it helpful?

Solution

Try this.

    foreach (WizardStep step in Wizard1.WizardSteps)
    {
        foreach (Control c1 in step.Controls)
        {
            if (c1 is Label)
            {
                Label1.Text += ((Label)c1).Text + "<br/><br/>";
            }

            if(c1 is RadioButtonList)
            {
              foreach (ListItem li in ((RadioButtonList)c1).Items)
              { 
                Label1.Text += li.Text + "<br/><br/>";
              }
            }
        }
    }

OTHER TIPS

I think you need to pay more attention to what you are looping and what properties you are referencing. I'm guessing because I don't know your control structure, but I think something along these lines would be more what I'd expect to see:

        foreach (WizardStep step in Wizard1.WizardSteps)
        {
            foreach (Control c1 in step.Controls)
            {
                if (c1 is Label)
                {
                    Label1.Text += ((Label)c1).Text + "<br/><br/>";
                }
                else
                {
                    foreach (Control c2 in c1.Controls)
                    {
                        if (c2 is RadioButtonList)
                        {
                            RadioButtonList rbl = (RadioButtonList)c2;

                            foreach (ListItem li in rbl.Items)
                            {
                                Label1.Text += li.Text.ToString() + "<br/><br/>";
                            }
                        }
                    }
                }
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top