簡體   English   中英

在向導C#中為每個RadioButtonList獲取ListItem

[英]get ListItem for each RadioButtonList in Wizard C#

請幫忙。 我需要從.net向導控件中獲取所有問題和可能答案的列表。 這是我的嘗試:

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

該代碼的工作原理是它可以解決所有問題。 但是,當我取消注釋注釋位以獲取可能的單選按鈕列表答案時,它將失敗。 我收到一個錯誤:“無法將類型為'System.Web.UI.LiteralControl'的對象轉換為類型為'System.Web.UI.WebControls.RadioButtonList'。”

我可以理解為什么會發生這種情況,但我不知道如何解決。 非常感謝您的幫助。

保羅的親切問候

嘗試這個。

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

我認為您需要更加注意循環和引用的屬性。 我猜是因為我不知道您的控制結構,但我認為遵循這些思路的事情會更符合我的期望:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM