我对C#有一点点经验,但我是新来的ASP方面的东西,我正在一个调查项目从MSSQL服务器获取问题/答案,为问题需要的答案(复选框,收音机,下拉菜单,文本输入)加载适当的控件,将问题的每个可能答案添加为ListItem,然后添加已填充的控制器到位于单一调查问题网页内的占位符。回答提交时,占位符将重新填充下一个问题,并为答案类型加载相应的控制器。从C#/ ASP.NET中动态创建的控件获取选中的复选框/无线电和文本输入
我目前遇到很多麻烦,试图得到无论用户的答案是什么,当他们点击提交按钮,因为我无法获得对任何复选框/无线电/文本输入的引用,以查看哪些是选择与否。
在调试和逐步检查每一行并观察局部变量以查看发生了什么时,this seems to be the hierarchy of the inserted answers at the time they are all added to the placeholder。
我试过在submitButton里面使用forEach循环(除此之外,在下面的代码示例中留下它)来检查所有项目,但似乎无法访问它..在各种测试之后看起来答案被摧毁了第二个submitButton被按下之前,在submitButton方法中的任何东西都可以运行之前,那么我怎么能在用户提交时得到用户的答案?
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>