Search code examples
c#asp.netgridviewcheckboxlistradiobuttonlist

Dynamically adding checkboxlist or radiobuttonlist with variable length to gridview


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:

  • Display 4 options if db value for multioptions is 'false' and total option is 4
  • Display 5 options if db value for multioptions is 'false' and total option is 5

Check Box List:

  • Display 4 options if db value for multioptions is 'true' and total option is 4
  • Display 5 options if db value for multioptions is 'true' and total option is 5

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.


Solution

  • You can achieve this requirement in the following way.

    1. You can use nested controls (gridview + checkbox list)
    2. First create a gridview control for the questions only
    3. Inside that gridview, create a template field & drop a checkbox list control
    4. Now, you have a gridview with checkbox list for each row/question (part of problem is solved)
    5. You should be having a database tables of TblQuestions & TblAnswers respectivley
    6. Now just drop a datasource (SQLDataSource, ObjectDataSource) inside the template field we created earlier & specify your answers SQL query (select * from TblAnswers where questionId=@questionId) & create a select parameter for the question ID on your datasource
    7. Inside RowDataBoundEvent of your gridview, you can use FindControl to find the checkboxList & the datasource control
    8. You 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.