in my developing application of online test, i have stumbled upon a new possibility today about variable length radiobuttonlist/checkboxlist options. As in general/common systems we have only 4 options(which even i feel is very rigid and incomplete), my client have asked me to create a test which on display gives 4 or 5 options(multiple or single select aka radio or check box) based on DB entry.
Now DB is not a problem ,everything is fine except the fact that how to variate the lenght of control or show that particular control based on given data in gridview. For example our gridview have a Lable for Question and Question No. and two controls checkboxlist and radiobuttonlist for options to be filled by user, so in short he wants to:
Radio Button List:
Check Box List:
So this is the general idea, and i cant figure out how to pt these things in my gridview, Which is "needed to show this data as an OMR sheet" and i wouldn't even bothered you guys if it was to be a normal test with a Next question button where we display only single question at a time or a simple static 4 option question.
You can achieve this requirement in the following way.
TblQuestions
& TblAnswers
respectivleyselect * from TblAnswers where questionId=@questionId)
& create a select parameter for the question ID on your datasourceRowDataBoundEvent
of your gridview, you can use FindControl
to find the checkboxList & the datasource controlYou can then specify select parameter with gridview's row Id & bind the checkbox list with desired answers
protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Find your controls
CheckBoxList yourChkBoxList = e.Row.FindControl("yourChkBoxListName") as CheckBoxList;
ObjectDataSource odsTemp=e.Row.FindControl("yourDataSourceName") as ObjectDataSource;
if (odsTemp !=null)
{
odsTemp.SelectParameters[0].DefaultValue = yourGridView.DataKeys[e.Row.RowIndex][0].ToString();
yourGridView.DataBind();
}
}
}
Update
You would not use a label control for questions. You would fetch them from database & bind questions to a gridview.