[英]Getting selected checkboxes/radios and text input from dynamically created controls in C#/ASP.NET
我对C#有一点经验,但是我对ASP方面还是陌生的,并且我正在从事一个调查项目,该项目从MSSQL服务器获取问题/答案,并为哪种类型的问题提供适当的控件需要(复选框,单选框,下拉菜单,文本输入),将问题的每个可能答案添加为ListItem,然后将填充的控制器添加到位于单个调查问题网页内的占位符。 提交答案后,占位符将重新填充下一个问题,并为答案类型加载适当的控制器。
我目前在尝试获取用户单击“提交”按钮时得到的答案时遇到了很多麻烦,因为我无法获得对任何复选框/收音机/文本输入的引用,以查看是否选择了哪些复选框。
在调试和逐步浏览每一行并观察局部变量的变化以查看发生了什么时, 这似乎是将插入的答案全部添加到占位符时的层次结构 。
我试过在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>
不知道为什么不直接使用复选框列表控件而是使用自己的控件
当您使用自己的控件时,viewstate应该由您自己处理,否则子控件将在您回发后消失
在您的情况下,在question.aspx.cs中,您访问了UC的控件集并进行了修改,但是当页面回发时,您添加到其中的所有子控件都将消失,因为在创建子控件之前已应用了viewstate,因此实际上,用户选择将丢失
您应该将所有修改代码放入控件中,然后改写为以下重写的方法,不要删除基本的
然后在应用ViewState之前将生成您的子控件(这里是QuestionCheckBoxList),然后将保留您的用户选择,并且您可以使用任何方法浏览选项以查看选择了哪个
protected override void CreateChildControls()
{
base.CreateChildControls();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.