简体   繁体   English

保存来自asp.net中2个或更多UserControls的值?

[英]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. 问题是我有50多个问题,我将它们分为3组,将每个问题组放入“用户控件”中,并且我试图保存每个用户控件中的值。

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. 其中Survey class是class,其中包含所有调查信息。


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 创建一个问题页面,该页面接受包含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]) 在问题页面上实现一个[下一个]和[上一个]按钮,让用户导航到具有不同QuestionID的URL(第一个问题是没有[上一个]的特殊情况)
  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. 然后将使用插入的groupID来引用问题页面。

If you only have 2 days for your implementation, then I would certainly settle for the simple one-question-per-page solution. 如果您只有2天的实施时间,那么我一定会选择简单的每页一个问题的解决方案。

This is easily solvable through UserControl public properties. 通过UserControl公共属性可以轻松解决此问题。

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). 这里的重点是强调通过子控件的公开属性获取所需的信息(相同的概念可以应用于代码中的任何父/子级别)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM