我有一个页面,这是一个考试页面,它用他们的答案选择所有MCQ问题... 并在服务器端页面加载我添加div.innerHtml + =“html标签”+问题+包含答案的单选按钮,它一直从数据库中选择,直到所有问题都在客户端呈现,问题是我添加所有这些问题和标签表单,最后有一个输入提交按钮,应该提交所有单选按钮值,但即使我设置表单操作我不能获得单选按钮值...
以下是一些代码:
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>";
}
答案 0 :(得分:1)
不要使用InnerHtml。这是不对的。
使用Inbuild 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);
答案 1 :(得分:0)
删除服务器表单元素(<form runat="server"
)。如果存在,则丢弃此新表单。
此外,您对不同的输入控件使用相同的名称。
name=\"question" + iString + "a\" value=\""
所以你也需要为收音机命名。