0
我需要在我的页面来创建动态单选按钮,然后访问回传他们的价值观ASP.net 2.0动态单选按钮
我创建使用下面的代码的按钮:
foreach (Answer answer in question.Answers)
{
RadioButton radioButton = new RadioButton();
radioButton.Text = answer.Text;
radioButton.GroupName = question.Id.ToString();
radioButton.ID = question.Id + "_" + answer.Id;
TableRow answerRow = new TableRow();
TableCell answerCell = new TableCell();
TableCell emptyCell = new TableCell();
emptyCell.ColumnSpan = 2;
answerCell.Controls.Add(radioButton);
answerRow.Cells.Add(emptyCell);
answerRow.Cells.Add(answerCell);
table.Rows.Add(answerRow);
}
在回发我尝试使用他们的创建ID访问单选按钮:
foreach (Answer answer in question.Answers)
{
RadioButton currentAnswer = Page.FindControl(question.Id + "_" + answer.Id) as RadioButton;
if (currentAnswer != null) // Damn this null
{
if(currentAnswer.Checked)
{
answers[questionCounter] = answer;
}
}
else
{
allQuestionsAnswered = false;
}
}
但似乎find方法从来没有找到答案。
请让我知道何时使用上面的代码。 在加载方法中,在预加载中,在按钮中单击提交方法。
看来,我总是得到空,从来没有找到控制。
我应该传递给该方法的控制参数是什么? – 2011-03-24 09:27:35
您可以将您的页面控件封装在PlaceHolder中,并将该PlaceHolder传递给该方法。你也可以做一个foreach(在Page.Controls中控制c){Control myControl = FindControlRecursive(c,id));如果(myControl!= null){//这是!您的控制权!}} – Dante 2011-03-24 09:34:48
我一直在研究这个问题,如果在页面加载中没有重新加载相同的控件,我无法从按钮单击事件中获取它们的值。我如何解决这个问题,我是否加载并删除它们? – 2011-03-24 10:40:29