从c#代码后面访问动态创建的html元素

时间:2015-12-09 13:07:26

标签: c# html asp.net

我正在为一系列问题动态生成单选按钮,每个问题至少有3个选项作为答案。 我在名为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);
                }
            }

我尝试将FindControl()与字符串问题和问题ID一起使用,因为它是我为字段集ID ='question" + q.QuestionId指定的IS,但它没有用, 我也尝试访问任何动态生成的控件,但我总是得到null。 如果我生成控件的方式不正确,请告诉我最好的方法是什么,所以我是asp.net的新手。提前致谢。

1 个答案:

答案 0 :(得分:0)

我使用了转发器并将其绑定到问题列表和选项,而不是动态生成问题。

喜欢这个

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

代码背后:

            //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();
                      }
                    }
                  }
                }
              }