principskitse (ikke just releasekode) :
// aspx-fil
<%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="PederSample._Default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Default</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="
http://schemas.microsoft.com/intellisense/ie5"> </HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<asp:Repeater id="rpQuestions" runat="server">
<HeaderTemplate>
<b>List of questions</b><br>
<br>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblQuestionId" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.QuestionId") %>'>
</asp:Label>
<asp:Label ID="lblQuestionText" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.QuestionText") %>'>
</asp:Label>
<asp:PlaceHolder ID="phQuestionControls" Runat="server"></asp:PlaceHolder>
<asp:Button ID="btnAnswer" Runat="server" Text="Besvar" CommandName="ANSWER" CommandArgument='<%# DataBinder.Eval(Container, "DataItem.QuestionId") %>'>
</asp:Button>
</ItemTemplate>
<SeparatorTemplate>
<hr>
</SeparatorTemplate>
</asp:Repeater></P>
<P>
<asp:Button id="btnAnswerAll" runat="server" Text="Besvar"></asp:Button></P>
</form>
</body>
</HTML>
// code-behind
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace PederSample
{
public class _Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnAnswerAll;
protected System.Web.UI.WebControls.Repeater rpQuestions;
protected QuestionCollection Questions
{
get
{
return Session["Questions"] as QuestionCollection;
}
set
{
Session["Questions"] = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Questions = new QuestionCollection();
for(int i=0;i<10;i++)
{
Questions.Add(new Question(i, String.Format("Spørgsmål nummer {0}", i)));
}
}
BindRepeater();
}
private void BindRepeater()
{
if(Questions != null)
{
rpQuestions.DataSource = Questions;
rpQuestions.DataBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.rpQuestions.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.rpQuestions_ItemDataBound);
this.rpQuestions.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.rpQuestions_ItemCommand);
this.btnAnswerAll.Click += new System.EventHandler(this.btnAnswerAll_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void rpQuestions_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
{
if(e.CommandName == "ANSWER")
{
// lidt haxet - bare så du kan se at du kan få fat i det... Response.Write bør ikke bruges
Response.Write(String.Format("Spørgsmål nummer {0} er blevet besvaret<br>", e.CommandArgument));
}
}
private void rpQuestions_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Question q = e.Item.DataItem as Question;
PlaceHolder ph = e.Item.FindControl("phQuestionControls") as PlaceHolder;
switch(q.QuestionType)
{
case QuestionTypes.Type1 :
TextBox tb = new TextBox();
tb.ID="ctrlAnswer";
tb.Text = q.Answer;
if(q.QuestionState == QuestionStates.Answered)
tb.Enabled = false;
ph.Controls.Add(tb);
break;
case QuestionTypes.Type2:
DropDownList ddList = new DropDownList();
ddList.ID = "ctrlAnswer";
ddList.Items.Add("svar1");
ddList.Items.Add("svar2");
ddList.Items.Add("svar3");
foreach(ListItem li in ddList.Items)
{
if(li.Text == q.Answer)
li.Selected = true;
}
ddList.Items.Insert(0,"choose");
if(q.QuestionState == QuestionStates.Answered)
{
ddList.Enabled = false;
}
ph.Controls.Add(ddList);
break;
default :
throw new ApplicationException("unknown questiontype");
}
}
}
private void btnAnswerAll_Click(object sender, System.EventArgs e)
{
bool unvalidatedQuesetons = false;
foreach(RepeaterItem item in rpQuestions.Items)
{
Label lblQuestionid = item.FindControl("lblQuestionId") as Label;
int questionId = int.Parse(lblQuestionid.Text);
Question question = Questions.GetQuestion(questionId);
string answer = String.Empty;
if(question.QuestionType == QuestionTypes.Type1)
{
// find tekstboksen
object o = item.FindControl("ctrlAnswer");
answer = ((TextBox)item.FindControl("ctrlAnswer")).Text;
}
else if(question.QuestionType == QuestionTypes.Type2)
{
// find listen
string _answer = ((DropDownList)item.FindControl("ctrlAnswer")).SelectedItem.Text;
answer = _answer != "choose" ? _answer : String.Empty;
}
question.Answer = answer;
if(question.IsValid)
{
question.QuestionState = QuestionStates.Answered;
}
else
{
unvalidatedQuesetons = true;
}
}
// binding igen da tilstand er ændret
BindRepeater();
// ... gem sagerne
if(!unvalidatedQuesetons)
Response.Write("You are done! - continue to next page");
else
Response.Write("You are not done! - fix errors");
}
}
public class QuestionCollection : CollectionBase
{
public Question this[int index]
{
get{return List[index] as Question;}
set{List[index] = value;}
}
public int Add(Question question)
{
return List.Add(question);
}
public Question GetQuestion(int questionId)
{
foreach(Question question in List)
{
if(question.QuestionId == questionId)
{
return question;
}
}
return null;
}
}
public enum QuestionTypes
{
Type1,
Type2
}
public enum QuestionStates
{
New,
Answered
}
public class Question
{
private QuestionStates _questionState;
private QuestionTypes _questionType;
private string _questionText;
private int _questionId;
private string _answer;
public bool IsValid
{
get
{
if(Answer.Trim().Length > 0)
return true;
return false;
}
}
public string QuestionText
{
get{return _questionText;}
set{_questionText = value;}
}
public string Answer
{
get{return _answer;}
set{_answer = value;}
}
public int QuestionId
{
get{return _questionId;}
}
public QuestionTypes QuestionType
{
get{return _questionType;}
}
public QuestionStates QuestionState
{
get{return _questionState;}
set{_questionState = value;}
}
public Question(int questioinId, string questionText)
{
this._questionId = questioinId;
this._questionText = questionText;
if(QuestionId %2 == 0)
_questionType = QuestionTypes.Type1;
else
_questionType = QuestionTypes.Type2;
}
}
}
mvh