i am self-studying asp.net and was trying some small projects to help understand the concepts better.
i am creating a simple e-learning website using html and asp.net(c#), that has a fixed number of multiple choices questionnaire on one of its pages,
i create the questionnaire by choosing random questions, out of a database table ,then creating a string that will hold the html code containing the questions properly formatted (questions in labels and answers as radio buttons),before inserting it into the innerhtml of a div tag.
all the above being done on pageload();
the problem is that i need to check the answers against the answer sheet (i already stored the correct answers in an array),but since the html code get added after page load, the code behind cant see the tags or their ids.
The question:
i’ve been told i can access the html tags using request.form[“Q#”] ?
can some1 please explain to me..how it can be done ?
and any other possible ways to do the same task ? (read the note below plz)
Note:
i know i can create the questionnaire structure in html, and then add the questions/answers into labels.
or creating hidden fields with runat server properties and then storing the answers into them, and checking the answer sheet against them with server side code.
It’s pretty much exactly what you’ve written. Request.Form is an associative array with one record for each form field on your page. The array is keyed by the name attribute of the input field. For server side controls this usually defaults to the control’s ClientId… But if you’re generating the HTML yourself, anything goes.
However, from the sound of things there are better ways to accomplish what you’re after.
One option would be to use a Repeater control, and to set its datasource to an array containing the information about the questions. You can then do something like this:
Edit:
Here’s a quick explanation of how the Request.Form collection is populated.
A typical request for a page works like this:
The client sends a request to the page to the server.
The server executes the Asp.Net page life cycle (init, load, control events, render).
The server takes the HTML generated by Asp.Net and sends it to the client.
When the client want’s to send more information back to the server, it does so by submitting a special form generated by Asp.Net. This results in the value of each input control being attached to the request sent to Asp.Net:
The client sends a POST request to the server (with form values attached).
The server executes the Asp.Net page life cycle (init, load, control events, render).
The server takes the HTML generated by Asp.Net and sends it to the client.
As you can see, if Request.Form is going to have a value, it is set before PageInit begins (much less PageLoad).