【问题标题】:Accessing dynamically created html elements from c# code behind从后面的 c# 代码访问动态创建的 html 元素
【发布时间】:2015-12-27 16:59:06
【问题描述】:

我正在为问题列表动态生成单选按钮,每个问题至少有 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() 与字符串 question 和问题的 id 一起使用,因为它是我为字段集 (ID ='question" + q.QuestionId) 指定的 IS,但它不起作用, 我也尝试访问任何动态生成的控件,但我总是得到空值。 如果我生成控件的方式不正确,请告诉我最好的方法是什么,因为我对 asp.net 很陌生。提前致谢。

【问题讨论】:

  • 纯 HTML 元素是 not 控件。他们完全生活在客户端。但是,如果它们是表单元素(单选按钮是)并且它们位于 &lt;form&gt; 标记内,那么当您提交页面时,它们将包含在发布到服务器的值中,您可以通过以下方式访问网页中可用的Request.Form 属性(或者System.Web.HttpContext.Current.Request.Form)。

标签: c# html asp.net


【解决方案1】:

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

这样

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多