我對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>