0

我有一个页面,它是一个考试页面,它选择所有 MCQ 问题及其答案......并在服务器端页面加载中添加 div.innerHtml += “html tags” + question + 包含答案的单选按钮,它一直在选择从数据库直到所有问题都在客户端呈现,问题是我在表单中添加了所有这些问题和标签,最后有一个输入提交按钮,它应该提交所有单选按钮值但即使我设置表单操作我也无法获得单选按钮值...

这是一些代码:

using (SqlDataReader sr1 = command1.ExecuteReader())
{
    cc.InnerHtml = "<form id=\"form2\" name=\"form2\" method=\"post\" action=\"\">";
    while (sr1.Read())
    {
        i++;
        iString = i.ToString();
        questionText = sr1["text"].ToString();
        question_first_wrong_answer = sr1["first_wrong_answer"].ToString();
        question_second_wrong_answer = sr1["second_wrong_answer"].ToString();
        question_right_answer = sr1["right_answer"].ToString();
        questionValue = (int)sr1["value"];
        c = answerPos.Next(0, 10);

        cc.InnerHtml += "<span>" + iString + "- " + questionText +"</span>"
                     + "<br />"
                     + "<table style=\"width:77%; margin-left: 47px;\">"
                     + "<tr>"
                     + "    <td style=\"width: 210px\">"
                     + " <input type=\"radio\" name=\"question" + iString + "a\" value=\"" + question_first_wrong_answer + "\" />" + question_first_wrong_answer + "</td>"
                     + "<td style=\"width: 210px\">"
                     + "<input type=\"radio\"  name=\"question" + iString + "a\" value=\"" + question_second_wrong_answer + "\" />" + question_second_wrong_answer + "</td>"
                     + "<td style=\"width: 210px\">"
                     + "<input type=\"radio\"  name=\"question" + iString + "a\" value=\"" + question_right_answer + "\" />" + question_right_answer + "</td>"
                     + "</tr>"
                     + "</table>"
                     + "<br />";

    }
    sr1.Close();
    cc.InnerHtml += "<input id=\"Submit1\" type=\"submit\" value=\"Submit\" />"
                 + "</form>";
}
4

2 回答 2

1

不要使用 InnerHtml。这是错的。使用内置HtmlPage

使用这样的例子

Page.Controls.Add( 
  new LiteralControl(@"<html>\r\n<body>\r\n
    <h1>Welcome to my Homepage!</h1>\r\n"));
HtmlForm Form1 = new HtmlForm();
Form1.ID = "Form1";
Form1.Method = "post";
Form1.Controls.Add(
  new LiteralControl("\r\nWhat is your name?\r\n"));
TextBox TextBox1 = new TextBox();
TextBox1.ID = "txtName";
Form1.Controls.Add(TextBox1);
Form1.Controls.Add(
  new LiteralControl("\r\n<br />What is your gender?\r\n"));
DropDownList DropDownList1 = new DropDownList();
DropDownList1.ID = "ddlGender";
ListItem ListItem1 = new ListItem();
ListItem1.Selected = true;
ListItem1.Value = "M";
ListItem1.Text = "Male";
DropDownList1.Items.Add(ListItem1);
ListItem ListItem2 = new ListItem();
ListItem2.Value = "F";
ListItem2.Text = "Female";
DropDownList1.Items.Add(ListItem2);
ListItem ListItem3 = new ListItem();
ListItem3.Value = "U";
ListItem3.Text = "Undecided";
DropDownList1.Items.Add(ListItem3);
Form1.Controls.Add(
  new LiteralControl("\r\n<br /> \r\n"));
Button Button1 = new Button();
Button1.Text = "Submit!";
Form1.Controls.Add(Button1);
Form1.Controls.Add(
  new LiteralControl("\r\n</body>\r\n</html>"));
Controls.Add(Form1);
于 2012-04-28T05:50:43.030 回答
0

删除服务器表单元素 ( <form runat="server")。如果存在,则丢弃此新表格。

此外,您对不同的输入控件使用相同的名称。

name=\"question" + iString + "a\" value=\""

因此,您也需要为收音机设置唯一名称。

于 2012-04-28T06:07:44.613 回答