[英]Accessing dynamically created html elements from c# code behind
I am dynamically generating radio buttons for a list of questions, each question has at least 3 choices as answers. 我正在动态生成用于问题列表的单选按钮,每个问题至少有3个选择作为答案。 I generated the radio buttons in a div called
questionsList
but I can't access them or their fieldset... Here's my code 我在名为
questionsList
的div中生成了单选按钮,但是我无法访问它们或它们的字段集。这是我的代码
_questions = Page.Form.FindControl("questionsList");
foreach (Question q in _survey.Questions)
{
if (!string.IsNullOrEmpty(q.QuestionEng))
{
List<Answers> answers = _blSurvey.GetAnswers(q.TypeOfQuestion);
string choiceItem = string.Empty;
foreach (Answers a in answers)
{
choiceItem += "<input type='radio' value='" + a.AnswerId + q.QuestionId +
"' name='radio-choice-v-2' id='radio" + idCount +
"' runat='server' /><label for='radio" + idCount++ +
"' >" + a.AnswerEng + "</label>";
}
var question =
new LiteralControl(
"<form><fieldset runat='server' class='questions' data-role='controlgroup' ID ='question" +
q.QuestionId + "'>" +
"<legend runat='server'>" + ++index + ". " + q.QuestionEng + "</legend>" + choiceItem +
"</fieldset></form>");
_questions.Controls.Add(question);
}
}
I tried to use FindControl()
with the string question and the question's id since it is the IS I specified for the fieldset ( ID ='question" + q.QuestionId
) but it didn't work, I also tried to access any of the dynamically generated controls but I always get null. If my way of generating controls is incorrect, please tell me what is the best method to do so I am very new to asp.net. Thanks in advance. 我尝试将
FindControl()
与字符串问题和问题的ID一起使用,因为它是我为ID ='question" + q.QuestionId
指定的IS( ID ='question" + q.QuestionId
),但是它没有用,我也尝试访问任何动态生成的控件,但我总是空值。如果我生成控件的方式不正确,请告诉我最好的方法是什么,因为我对asp.net还是陌生的。
I used a repeater and bound it to the questions list and the choices instead of generating the questions dynamically. 我使用了一个转发器,并将其绑定到问题列表和选项,而不是动态生成问题。
Like this 像这样
<div runat="server" id="questionsList" >
<asp:Repeater ID="rptQuestionsEng" runat="server" Visible="False">
<ItemTemplate>
<asp:Label runat="server" ID="lblQuestion" Text='<%# Bind("QuestionEng") %>'></asp:Label>
<asp:RadioButtonList runat="server" ID="rblQuestionEng"
</asp:RadioButtonList>
</ItemTemplate>
</asp:Repeater>
</div>
Code behind: 后面的代码:
//To bound the questions
rptQuestionsEng.DataSource = _survey.Questions;
rptQuestionsEng.DataBind();
//To bound the choices
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
if (rblLanguage.SelectedValue.Equals("1"))
{
foreach (Question q in _survey.Questions)
{
q.answers = _blSurvey.GetAnswers(q.TypeOfQuestion);
var rbl = (RadioButtonList)e.Item.FindControl("rblQuestionEng");
if (!string.IsNullOrEmpty(q.QuestionEng))
{
rbl.DataTextField = "AnswerEng";
rbl.DataValueField = "AnswerId";
rbl.DataSource = q.answers;
rbl.DataBind();
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.