开发者

save values from 2 or more UserControls in asp.net?

I am working in Fast Survey Project!!

i have no time for design a database and all these things ,i have only one table and i have to fill the answers , i have to finish it within two days.

the开发者_运维知识库 problem is I have more than 50 questions and i divided them into 3 groups , i putted each question group in User Control, and i am trying to save the values from each user control.

i want to save theme temporarily after each user control and when the user reach the last user control and click finish button it should save all the answers one time .

so is there any idea?? I was thinking to use something like global file !!

but i don't whether it works or not but i am working on it now and i hope to find any idea from you people.


You can temporarily save in session:

var survey = new Survey();
Survey.Name = textBoxName.Text;
Session["answer"] = survey.

Then later in all forms you can use:

var survey = Session["answer"] as Survey;
if (survey == null)
   //error, redirect to start

Where Survey class is class, which contains all survey info.


Other example,

//Form1, question 1
List<string> survey = new List<string>();
survey.Add(textBox1.Text);
Session["survey"] = survey;

//Form2, question2
var survey = Session["survey"] as List<string>;
survey.Add(textBox1.Text);

//Form...n... final save
var survey = Session["survey"] as List<string>;
for(int i=0; i< survey.Count;i++)
{
    SaveAnswer("someId", i, survey(i));
}


Here is one solution

  1. Create a question page which accepts a querystring containing questionID
  2. Save question responses on the session.
  3. Implement a [next] and [previous] button on the question page that let's the user navigate to a url with a different questionID (first question is a special case that doesn't have [previous])
  4. Create a result page
  5. When there isn't any [next] question available, change button caption to [finish] and point to result page
  6. For result page. Retrieve correct answers from your table and retrieve user answers from session. Compare and display result

If you want to display a page with a group of up to three question on each page, then it becomes slightly more complicated. The question page would then be referenced with the groupID instaed.

If you only have 2 days for your implementation, then I would certainly settle for the simple one-question-per-page solution.


This is easily solvable through UserControl public properties.

Here is how i would solve this:

  1. Each user control that display question will have two properties - Question and Answer.
  2. On the Page Submit button handler i will include the following logic block

// this dictionary will eventually be serialized and kept in session 
// throughout the whole Q&A session
var questionsAndAnswers = new Dictionary();
foreach(var control in MyUserControls) 
{
    questionsAndAnswers.Add(control.Question, control.Answer);
}
this.Session["q&a"] = questionsAndAnswers;

Something like that should do the work.

The main point here is the emphasis on getting the needed information through exposed properties from your child controls (the same concept can be applied on any parent/child level in your code).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜