Booonz
Booonz

Reputation: 95

Submit form fails

I have a page that which is an exam page,it selects all MCQ questions with their answers... and in server side page load i add div.innerHtml += "html tags " + question + radio buttons containing answers and it keeps selecting from database till all questions are rendered in client side, the thing is i add all these questions and tags in form and in its end there is a input submit button which should submit all radio buttons values but even if i set form action i cant get the radio buttons values...

Here is some code :

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>";
}

Upvotes: 0

Views: 158

Answers (2)

Sunil Chavan
Sunil Chavan

Reputation: 524

Do not Use InnerHtml. Its Wrong. Use Inbuild HtmlPage Class

Use Like this for Example

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

Upvotes: 1

Adrian Iftode
Adrian Iftode

Reputation: 15663

Remove the server form element (<form runat="server" ). If that is present, this new form is discarded.

Also you are using the same name for different input controls.

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

So you need to have unique names for the radio too.

Upvotes: 0

Related Questions