PDA

View Full Version : ایجاد فرم داینامیک جهت امتحان آنلاین



project80888
دوشنبه 07 بهمن 1392, 10:15 صبح
سلام دوستان
کدی که در قسمت پایین قرار دادم جهت ایجاد یه فرم سوال

سوالی که داشتم به نظر شما از چه کنترلی می توانم استفاده کنیم تا بتوانم هم صفحه بندی انجام بدهم وهم جستجوی کنترل و مقدار انتخاب شده توسط کاربر را بتوانم دریافت کنم ؟
با توجه به اینکه تعداد سوالات متغییر است و می توانند تعداد آن زیاد باشد

آیا روشی که برای ایجاد کنترلها استفاده کردم بهینه است ؟





int questionNum = 0;
int totalQuestions = 0;
bool IsAdmin = true;
protected System.Web.UI.WebControls.Button SubmitButton;

/// <summary>
/// خواندن عنوان فرم
/// </summary>
/// <param name="QuizTitle"></param>
private void ReadQuizTitle(string QuizTitle)
{


TableRow tr = new TableRow();
TableCell aCell = new TableCell();

Table1.Rows.Add(tr);
aCell.Text = "<H1><font color=\"green\">" + QuizTitle + "</font></H1>\n";
tr.Cells.Add(aCell);
}
/// <summary>
/// اضافه کردن سوالات
/// </summary>
private void FillQuestion()
{
int Formid = 5;
BL_Question obj = new BL_Question();
DataTable dtQuestion = new DataTable();
obj.BaseFormId = Formid;
obj.IsAdmin = IsAdmin;
dtQuestion = obj.SelectALLQuestionByFormID();
if (dtQuestion != null)
{
ReadQuizTitle("لطفا اطلاعات زیر را بادقت وارد کنید");
for (int NumberOfQuestions = 0; NumberOfQuestions < dtQuestion.Rows.Count; NumberOfQuestions++)
{


string typeAnswer = dtQuestion.Rows[NumberOfQuestions]["AnswerTypeId"].ToString();
string QuestionsID = dtQuestion.Rows[NumberOfQuestions]["QuestionId"].ToString();
// create a row for the question and read it from the database
TableRow tr = new TableRow();
Table1.Rows.Add(tr);
TableCell aCell = new TableCell();
// get the text for the question and stick it in the cell
aCell.Text = "<b>" + NumberOfQuestions + ".</b> " + dtQuestion.Rows[NumberOfQuestions]["Question"].ToString();
tr.Cells.Add(aCell);
// add a blank row to pad between question and choices
TableRow blankRow = new TableRow();
TableCell cellPad = new TableCell();
cellPad.BorderWidth = 5;
blankRow.Cells.Add(cellPad);
Table1.Rows.Add(blankRow);
//---------------------------------------------------------------------------------------
AddAnswer(QuestionsID, typeAnswer, NumberOfQuestions);
//---------------------------------------------------------------------------------------
TableRow spacer = new TableRow();
TableCell spacerCell = new TableCell();
spacerCell.Height = 30;
spacer.Cells.Add(spacerCell);
Table1.Rows.Add(spacer); // add a spacer



}


}




}
/// <summary>
/// اضافه کردن گزینه های سوال
/// </summary>
/// <param name="QuestionsID">کد سوال</param>
/// <param name="typeAnswer">نوع سوال</param>
/// <param name="NumberOfQuestions">شماره سوال</param>
private void AddAnswer(string QuestionsID, string typeAnswer, int NumberOfQuestions)
{

BL_Answer objAnswer = new BL_Answer();
DataTable dtAnswer = new DataTable();
objAnswer.QuestionsID_FK = QuestionsID.Toint32();
objAnswer.IsAdmin = IsAdmin;
dtAnswer = objAnswer.SelectALLAnswer();
if (dtAnswer != null)
{

if (typeAnswer == "0")
{
TableRow tr2 = new TableRow();
Table1.Rows.Add(tr2);
// create a cell for the choice
TableCell aCell3 = new TableCell();
aCell3.Width = 1000;
// align the choices on the left
aCell3.HorizontalAlign = HorizontalAlign.Left;
tr2.Cells.Add(aCell3);

CheckBoxList chbl = new CheckBoxList();
chbl.DataSource = dtAnswer;
chbl.DataTextField = "Answer";
chbl.DataValueField = "AnswerId";

chbl.ID = "chbl" + NumberOfQuestions.ToString();
chbl.Visible = true;
chbl.DataBind();
aCell3.Controls.Add(chbl);





return;
}

else if (typeAnswer == "1")
{
TableRow tr2 = new TableRow();
Table1.Rows.Add(tr2);
TableCell aCell3 = new TableCell();
aCell3.Width = 1000;
aCell3.HorizontalAlign = HorizontalAlign.Left;
tr2.Cells.Add(aCell3);

RadioButtonList rdbl = new RadioButtonList();
rdbl.DataSource = dtAnswer;
rdbl.DataTextField = "Answer";
rdbl.DataValueField = "AnswerId";
rdbl.ID = "rdbl" + NumberOfQuestions.ToString();
rdbl.Visible = true;
rdbl.DataBind();

aCell3.Controls.Add(rdbl);
return;
}

else if (typeAnswer == "2")
{
TableRow tr2 = new TableRow();
Table1.Rows.Add(tr2);
TableCell aCell3 = new TableCell();
aCell3.Width = 1000;
aCell3.HorizontalAlign = HorizontalAlign.Left;
tr2.Cells.Add(aCell3);

DropDownList drpl = new DropDownList();
drpl.DataSource = dtAnswer;
drpl.DataTextField = "Answer";
drpl.DataValueField = "AnswerId";
drpl.ID = "rdbl" + NumberOfQuestions.ToString();
drpl.Visible = true;
drpl.DataBind();

aCell3.Controls.Add(drpl);
return;
}



for (int AnswerCount = 0; AnswerCount < dtAnswer.Rows.Count; AnswerCount++)
{


TableRow tr2 = new TableRow();
Table1.Rows.Add(tr2);
// create a cell for the choice
TableCell aCell3 = new TableCell();
aCell3.Width = 1000;
// align the choices on the left
aCell3.HorizontalAlign = HorizontalAlign.Left;
tr2.Cells.Add(aCell3);
// create a radio button in the cell


switch (typeAnswer)
{


case "3":
{
TextBox txt = new TextBox();
txt.ValidationGroup = "txtGroup" + QuestionsID;
txt.Text = dtAnswer.Rows[AnswerCount]["Answer"].ToString();
txt.ID = "txt" + NumberOfQuestions.ToString() + Convert.ToChar(AnswerCount + 65);
txt.Visible = true;
aCell3.Controls.Add(txt);

}

break;
case "4":
{
TextBox txt = new TextBox();
txt.TextMode = TextBoxMode.MultiLine;
txt.CssClass = "textarea";
txt.ValidationGroup = "txtGroup" + QuestionsID;
txt.Text = dtAnswer.Rows[AnswerCount]["Answer"].ToString();
txt.ID = "txt" + NumberOfQuestions.ToString() + Convert.ToChar(AnswerCount + 65);
txt.Visible = true;
aCell3.Controls.Add(txt);

}

break;
case "5":
{
FileUpload fi = new FileUpload();
fi.CssClass = "fileuploader";
fi.ID = "fi" + NumberOfQuestions.ToString() + Convert.ToChar(AnswerCount + 65);
fi.Visible = true;
aCell3.Controls.Add(fi);

}

break;


default:
break;
}



}
}


}
/// <summary>
/// اضافه کردن دکمه ارسال
/// </summary>
private void AddSubmitButton()
{
SubmitButton = new Button();
this.Controls.Add(SubmitButton);
SubmitButton.Width = 100;
SubmitButton.Height = 25;
SubmitButton.Visible = true;
SubmitButton.Text = "Done";
SubmitButton.Style.Add("runat", "server");
TableRow ButtonRow = new TableRow();
TableCell ButtonCell = new TableCell();
ButtonRow.Cells.Add(ButtonCell);
ButtonCell.Controls.Add(SubmitButton);
Table1.Rows.Add(ButtonRow);
}