从C#/ASP.NET中的动态创建控件中获取选定的复选框/收音机和文本输入



我对C#有一些经验,但是我是ASP方面的新手,我正在从事一个调查项目,该项目从MSSQL Server获取问题/答案,加载问题所需的答案类型的适当控件(复选框,收音机,下拉菜单,文本输入),将问题添加为ListItem的每个可能的答案,然后将填充的控制器添加到单个调查问题中的占位符中网页。在答案提交时,占位符将重新填充下一个问题,并为答案类型加载适当的控制器。

我目前遇到很多麻烦,试图在单击"提交"按钮时获得任何答案,因为我无法参考任何复选框/收音机/文本输入,以查看选择哪些已选择哪些是否。

在调试和踏上每一行并观看本地变量变化以查看发生的事情时,这似乎是插入答案的层次结构。

我尝试在提交嘴内使用foreach循环(除其他外,在下面的代码示例中保留了它)以检查所有项目,但似乎根本无法访问。.经过各种测试,似乎答案被摧毁了,第二次按下了提交班顿,并且在提交方法中的任何内容都可以运行之前,我到底如何让用户答案对他们提交?

Question.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {

        ....

                else if (questionType == 2) //checkbox 
                {
                    CheckBoxQuestionController checkBoxController = (CheckBoxQuestionController)LoadControl("~/CheckBoxQuestionController.ascx");
                    checkBoxController.ID = "checkBoxQuestionController";
                    checkBoxController.QuestionLabel.Text = questionText;
                    SqlCommand optionCommand = new SqlCommand("SELECT * FROM answerOptionTable WHERE answerOptionTable.q_Id = " + currentQuestion, connection);
                    //run command
                    SqlDataReader optionReader = optionCommand.ExecuteReader();
                    //loop through all results
                    while (optionReader.Read())
                    {
                        ListItem item = new ListItem(optionReader["answerText"].ToString(), optionReader["a_Id"].ToString());
                        int currentAnswerId = Convert.ToInt32(optionReader["a_Id"]);
                        checkBoxController.QuestionCheckBoxList.Items.Add(item); //add answer to list
                    }
                    QuestionPlaceholder.Controls.Add(checkBoxController);
                }

            //other questionType checking here
            connection.Close();
    }
protected void SubmitButtonClick(object sender, EventArgs e)
    {
        //template.Items.
        Control resultControl = FindControl("checkBoxQuestionController");
        //test 1
        CheckBoxList resultControl2 = (CheckBoxList)FindControl("checkBoxQuestionController");
        CheckBoxList resultControl3 = (CheckBoxList)FindControl("questionCheckBoxList"); 
        //test 123213
        CheckBoxList Cbx = (CheckBoxList)QuestionPlaceholder.FindControl("checkBoxQuestionController");
        //test 2
        //for (int i = 0; i < QuestionPlaceholder.Controls.Count; i++)
        //{
        //    if (QuestionPlaceholder.Controls[i].GetType() == typeof(CheckBoxList))
        //    {
        //        CheckBoxList myList = QuestionPlaceholder.Controls[i].GetType();
        //    }
        //}
        //test 3
        //foreach (ListItem cbList in QuestionPlaceholder.Controls.("checkBoxQuestionController")
        //{
        //    if (cbList.Selected)
        //    {
        //    }
        //}
        //test 4
        //foreach (ListItem cb in QuestionPlaceholder.Controls.OfType<ListItem>())
        //{
        //    if (cb != null)
        //    {
        //    }
        //}
        int count = 0;
        List<ListItem> selected = new List<ListItem>();
        foreach (ListItem item in debugList.Items)
        {
            if (item.Selected)
            {
                count++;
            }
        }
            Response.Redirect("Question.aspx");
    }

checkboxquestioncontroller.ascx.cs

public partial class CheckBoxQuestionController : System.Web.UI.UserControl
{
    //Getters and setters
    public Label QuestionLabel
    {
        get
        {
            return questionLabel;
        }
        set
        {
            questionLabel = value;
        }
    }

    public CheckBoxList QuestionCheckBoxList
    {
        get
        {
            return questionCheckBoxList;
        }
        set
        {
            questionCheckBoxList = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

checkboxquestioncontroller.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CheckBoxQuestionController.ascx.cs" Inherits="Survey_Prototype.CheckBoxQuestionController" %>
    <div class="bodyTitle">
        <asp:Label ID="questionLabel" runat="server" Text="LabelText"></asp:Label>
    </div>
    <div class="answerOptionContainer">
        <asp:CheckBoxList ID="questionCheckBoxList" runat="server">
        </asp:CheckBoxList>
    </div>

不确定为什么您不直接使用CheckboxList控件,而是使用自己的控件

当您使用自己的控件时,应自己处理ViewState,否则后控制将在您发回后

之后消失

在您的情况下,您访问了UC的控件集并修改了它,但是当页面发后期,您添加到其中的所有子控件将消失,因为在子控件为之前应用了视图state创建的,因此实际上将丢失用户选择

您应该将所有修改代码放在控件中以遵循覆盖方法,不要删除基础

然后,您的子控制(在这里是QuarteCheckBoxList)将在应用视图state之前生成,然后保留您的用户选择,您可以使用任何方法来浏览选项以查看选择的选项

>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }

最新更新