views:

226

answers:

4

hello, 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.

+2  A: 

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 ?

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.

if(Request.Form["Q0"] == answers[0]) {
    //Answer is correct
}

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:

foreach(item as RepeaterItem in repeater) {
    if(item.FindControl("radioButtonListId").SelectedValue == answer) {
        //Answer correct
    }
}

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

AaronSieb
I think that the key passed in to Request.Form has to be the same as the "name" given to the input controls, not the clientID.For example, if the following was checked *<input id="Radio1" type="radio" name="Test" value="Test 1" />* then Request.Form["Test"] would return "Test 1"
Greg
Aaron: so if i use Request.Form["Q1"] , with Q1 being the name of the radiobuttonsgroup of Question #1 answers , the line will return the value of the chosen radio button? also wouldn't request.form["Q1"] , check against the html tag ids before the page loads ? remember i am loading the html tags on pageload..
Madi D.
@Madi D. During the first page load the forms collections will be empty (because the user hasn't specified any input). When the page posts back, the forms collection will have everything the user has entered into the input controls on the page.
AaronSieb
@Greg Ugh, you're right. Fixed the answer.
AaronSieb
+1  A: 

Since you question string is formatted html, try a literal control rather than a label. Labels can try to "escape" your html formatting and will enclose your html string in a span tag. And rather than a div, try using an html panel. It will render the same html, make it easier to insert your literals and radio buttons after the fact.

Joel Coehoorn
Joel , thx for your advice.will keep it into consideration, but my main issue is accessing the chosen answers when the user clicks submit.
Madi D.
A: 

Sorry, but what are you doing?? You're constructing the HTML code on the server-side and then add it to a div or label or whatever?? I guess this is not the right approach.

First create an appropriate model. Construct classes like

  • QuestionAnswerItem which is a class containing a question (text etc..), a few choices (as a list of strings for instance) and the correct answer.
  • Questionaire class which holds a list of QuestionAnswerItem objects

You should then use a Repeater (or other DataBound control) and then use list controls like the CheckBoxList and RadioButtonList. Check on the MSDN documentation for the Repeater control and it's DataSource property and DataBind() method. I'm pretty sure it will help you.

Accessing the chosen answers should then be possible by iterating over the repeater items and finding your RadioButtonList or CheckBoxList controls (probably using the FindControl(...)) method. Once you have your control reference, you can iterate over it and check the "Selected" property.

Using this approach you can get rid of the Request.Form[...]. I had to use it really rarely till now.

Juri
A: 

In ASP.NET, there are "web server controls" that generally replace the traditional HTML controls.

For example, <input type="radio"/> in HTML would be a <asp:RadioButton runat="server" /> as a server control. There are also web server controls that do not map to a traditional HTML control in a 1 to 1 manner, such as a RadioButtonList.

These make it easier to access your form data in the code behind.

http://support.microsoft.com/kb/306459

Greg
@Greg: i know all about the controls, i've used them to create the 1st template of the questionnaire and it was simple and easy, the problem came when i tried to become a smartie and complicate it even more.and i cant add <asp:> tags on page load, becoz they wont get compiled on page creation.
Madi D.