Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. ASP.NET - Create a questionnaire/test on the fly.

ASP.NET - Create a questionnaire/test on the fly.

Scheduled Pinned Locked Moved ASP.NET
csharphtmlasp-netdatabasehelp
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SmartSpider
    wrote on last edited by
    #1

    Hello Everyone, I am a newbie to the world of ASP.NET and I am trying to create a web based questionnaire/test. The user clicks on a link to start the test and the page that presents itself has questions and appropriate input control (text box or checkboxes, etc) for the user to answer. Based on the type of test that user selects, I have to present the form which gets questions from a database and it can be any number of questions. I do not know a good way to generate all the required text boxes dynamically, because, I have an aspx file, which just has the page skeleton (header and all) but the remaining content has to be set by the cs file for the form. I have found a way of doing this, but am not sure if (I don't think this is) this is the right way - in my cs file, I create the entire html form using StringBuilder and then set the innerHTML of a div in my aspx. I don't even know if this will work whent he user completes the test and submits it (haven't gotten that far yet) - i.e. I do not know if I'd be able to retrieve the values from text boxes, which are named like txtAnsToQues1, txtAnsToQues2, etc. Any help or a right direction is appreciated. (I did search the forum first and came up with some dynamic form results, but none answered what I am looking for - perhaps this is too basic?) Thanks, ~R

    P 1 Reply Last reply
    0
    • S SmartSpider

      Hello Everyone, I am a newbie to the world of ASP.NET and I am trying to create a web based questionnaire/test. The user clicks on a link to start the test and the page that presents itself has questions and appropriate input control (text box or checkboxes, etc) for the user to answer. Based on the type of test that user selects, I have to present the form which gets questions from a database and it can be any number of questions. I do not know a good way to generate all the required text boxes dynamically, because, I have an aspx file, which just has the page skeleton (header and all) but the remaining content has to be set by the cs file for the form. I have found a way of doing this, but am not sure if (I don't think this is) this is the right way - in my cs file, I create the entire html form using StringBuilder and then set the innerHTML of a div in my aspx. I don't even know if this will work whent he user completes the test and submits it (haven't gotten that far yet) - i.e. I do not know if I'd be able to retrieve the values from text boxes, which are named like txtAnsToQues1, txtAnsToQues2, etc. Any help or a right direction is appreciated. (I did search the forum first and came up with some dynamic form results, but none answered what I am looking for - perhaps this is too basic?) Thanks, ~R

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      The easiest way to approach this would be to use a data-bound control like a ListView to display the questions and corresponding input controls. Within the ItemTemplate for your data-bound control, you can declare an instance of each type of input control you want to use (TextBox, CheckBox etc.) Then at run-time, override the ItemDataBound event for your control and show/hide each input control for the current question being rendered as appropriate. On postback, to save the user's input, loop through the Items collection for the control, use FindControl to locate the correct control containing the user's input, then save the information to your database (or whatever)

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      S 1 Reply Last reply
      0
      • P pmarfleet

        The easiest way to approach this would be to use a data-bound control like a ListView to display the questions and corresponding input controls. Within the ItemTemplate for your data-bound control, you can declare an instance of each type of input control you want to use (TextBox, CheckBox etc.) Then at run-time, override the ItemDataBound event for your control and show/hide each input control for the current question being rendered as appropriate. On postback, to save the user's input, loop through the Items collection for the control, use FindControl to locate the correct control containing the user's input, then save the information to your database (or whatever)

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        S Offline
        S Offline
        SmartSpider
        wrote on last edited by
        #3

        Paul, Appreciate your response - it reinforces the fact that I am a newbie (but I'll get there) :-). I am going to read in detail on the ListView to see how and if I can use it in my case. I need to present a beautiful looking form, with (dynamic number of) categories, each having (dynamic number of) questions and each question needs to have an appropriate (let's say its only text box for now) control against it. And yes, on postback, I need to go through each answer and save it in a DB. The form needs to look beautiful and organized in catgories with a layout something like ------------- Category One | ------------- --> Question 1. In context of gravitational force, what is the difference between 'g' and 'G'? Answer 1. [Text Box] --> Question 2. What is the formula for the gravitational pull between two bodies of mass M1 & M2 that are x distance apart? --> Answer 2. [Text Box] ------------- Category Two | ------------- --> Question 1. In context of gravitational force, what is the difference between 'g' and 'G'? Answer 1. [Text Box] --> Question 2. What is the formula for the gravitational pull between two bodies of mass M1 & M2 that are x distance apart? --> Answer 2. [Text Box] Will the ListView allow for the tables (that's how I am grouping categories and ques within), images, etc? Thanks,

        P 1 Reply Last reply
        0
        • S SmartSpider

          Paul, Appreciate your response - it reinforces the fact that I am a newbie (but I'll get there) :-). I am going to read in detail on the ListView to see how and if I can use it in my case. I need to present a beautiful looking form, with (dynamic number of) categories, each having (dynamic number of) questions and each question needs to have an appropriate (let's say its only text box for now) control against it. And yes, on postback, I need to go through each answer and save it in a DB. The form needs to look beautiful and organized in catgories with a layout something like ------------- Category One | ------------- --> Question 1. In context of gravitational force, what is the difference between 'g' and 'G'? Answer 1. [Text Box] --> Question 2. What is the formula for the gravitational pull between two bodies of mass M1 & M2 that are x distance apart? --> Answer 2. [Text Box] ------------- Category Two | ------------- --> Question 1. In context of gravitational force, what is the difference between 'g' and 'G'? Answer 1. [Text Box] --> Question 2. What is the formula for the gravitational pull between two bodies of mass M1 & M2 that are x distance apart? --> Answer 2. [Text Box] Will the ListView allow for the tables (that's how I am grouping categories and ques within), images, etc? Thanks,

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #4

          To display a list of questions grouped by category you would need 2 ListView controls, one nested inside the ItemTemplate of the other. Have a look at this[^] open-source ASP.NET Questionnaire application. I've used it in the past as a building-block for creating a survey website.

          Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

          S 1 Reply Last reply
          0
          • P pmarfleet

            To display a list of questions grouped by category you would need 2 ListView controls, one nested inside the ItemTemplate of the other. Have a look at this[^] open-source ASP.NET Questionnaire application. I've used it in the past as a building-block for creating a survey website.

            Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

            S Offline
            S Offline
            SmartSpider
            wrote on last edited by
            #5

            I was unable to download the sample from the link you pointed me to, but I read a little about ListView control at another website and so far it looks promising. In the sample I read, they had used and asp:AccessDataSource in the aspx file itself and simply pointed to the mdb file with a SELECT query. In my case, I need to select categories from table 1, loop over categories and select questions for each. Not sure how to do this... Paul - please know, your help and time are much appreciated :) Many thanks, ~R

            P 1 Reply Last reply
            0
            • S SmartSpider

              I was unable to download the sample from the link you pointed me to, but I read a little about ListView control at another website and so far it looks promising. In the sample I read, they had used and asp:AccessDataSource in the aspx file itself and simply pointed to the mdb file with a SELECT query. In my case, I need to select categories from table 1, loop over categories and select questions for each. Not sure how to do this... Paul - please know, your help and time are much appreciated :) Many thanks, ~R

              P Offline
              P Offline
              pmarfleet
              wrote on last edited by
              #6

              Bind your parent ListView control that is displaying the categories to a SELECT query that fetches these categories. Handle the ItemDataBound[^] event for your parent ListView control. This will fire each time data for an individual category is bound to the list control. The following code demonstrates how to handle this event and use LINQ to get all orders for the child list control associated with a customer from the parent list control. It uses data from the Northwind database. The parent list control is bound to a SqlDataSource control.

              protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
              {
              string customerID = ((System.Data.DataRowView)((ListViewDataItem)e.Item).DataItem)["CustomerID"].ToString();
              ListView listView2 = e.Item.FindControl("ListView2") as ListView;

                  using (NorthwindModel.NorthwindEntities context = new NorthwindModel.NorthwindEntities())
                  {
                      var orders = (from o in context.Orders
                                    where o.Customers.CustomerID == customerID
                                    select o).ToList();
              
                      listView2.DataSource = orders;
                      listView2.DataBind();
                  }
              }
              

              Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

              S 2 Replies Last reply
              0
              • P pmarfleet

                Bind your parent ListView control that is displaying the categories to a SELECT query that fetches these categories. Handle the ItemDataBound[^] event for your parent ListView control. This will fire each time data for an individual category is bound to the list control. The following code demonstrates how to handle this event and use LINQ to get all orders for the child list control associated with a customer from the parent list control. It uses data from the Northwind database. The parent list control is bound to a SqlDataSource control.

                protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
                {
                string customerID = ((System.Data.DataRowView)((ListViewDataItem)e.Item).DataItem)["CustomerID"].ToString();
                ListView listView2 = e.Item.FindControl("ListView2") as ListView;

                    using (NorthwindModel.NorthwindEntities context = new NorthwindModel.NorthwindEntities())
                    {
                        var orders = (from o in context.Orders
                                      where o.Customers.CustomerID == customerID
                                      select o).ToList();
                
                        listView2.DataSource = orders;
                        listView2.DataBind();
                    }
                }
                

                Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                S Offline
                S Offline
                SmartSpider
                wrote on last edited by
                #7

                Paul, Cannot thank you enough... but Thanks! ~R

                1 Reply Last reply
                0
                • P pmarfleet

                  Bind your parent ListView control that is displaying the categories to a SELECT query that fetches these categories. Handle the ItemDataBound[^] event for your parent ListView control. This will fire each time data for an individual category is bound to the list control. The following code demonstrates how to handle this event and use LINQ to get all orders for the child list control associated with a customer from the parent list control. It uses data from the Northwind database. The parent list control is bound to a SqlDataSource control.

                  protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
                  {
                  string customerID = ((System.Data.DataRowView)((ListViewDataItem)e.Item).DataItem)["CustomerID"].ToString();
                  ListView listView2 = e.Item.FindControl("ListView2") as ListView;

                      using (NorthwindModel.NorthwindEntities context = new NorthwindModel.NorthwindEntities())
                      {
                          var orders = (from o in context.Orders
                                        where o.Customers.CustomerID == customerID
                                        select o).ToList();
                  
                          listView2.DataSource = orders;
                          listView2.DataBind();
                      }
                  }
                  

                  Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                  S Offline
                  S Offline
                  SmartSpider
                  wrote on last edited by
                  #8

                  Hi Paul, I would have posted this as a separate question, but then I would have needed to explain what I am doing and how I am binding the nested ListViews... so I thought I'd try sending my question to you and hope for an answer. So I have nested ListViews - the parent displays the categories, and in the ItemTemplate of this parent, I have another ListView that displays the questions under that category - now is there a way for me to get the category name bound to parent listview, in the child listview? I can achieve this by modifying my SQL and populate the data source of the child lv with category name as well, but I don't think that's the right way (unecessarily retrieving the category name again, when, it is already available). Note - the category name is not available in any control of parent lv - it is just displayed using <%#Eval%> within a Appreciate your help - hope I am not pushing it. ~R

                  P 1 Reply Last reply
                  0
                  • S SmartSpider

                    Hi Paul, I would have posted this as a separate question, but then I would have needed to explain what I am doing and how I am binding the nested ListViews... so I thought I'd try sending my question to you and hope for an answer. So I have nested ListViews - the parent displays the categories, and in the ItemTemplate of this parent, I have another ListView that displays the questions under that category - now is there a way for me to get the category name bound to parent listview, in the child listview? I can achieve this by modifying my SQL and populate the data source of the child lv with category name as well, but I don't think that's the right way (unecessarily retrieving the category name again, when, it is already available). Note - the category name is not available in any control of parent lv - it is just displayed using <%#Eval%> within a Appreciate your help - hope I am not pushing it. ~R

                    P Offline
                    P Offline
                    pmarfleet
                    wrote on last edited by
                    #9

                    Yes. You can use a data binding expression to do this. Below I have posted the code for a page that displays a hierarchical list of customers with orders from the sample Northwind database using ADO.NET Entity Framework. I use a databinding expression in the child list control displaying order details to display the customer's country against every order. The databinding expression is a bit clumsy (lots of calls to .Parent) but it works. ASPX code: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

                    • <%#Eval("CustomerID") %>
                    • <%#Eval("OrderID") %> <%#((NorthwindModel.Customers)((ListViewDataItem)Container.Parent.Parent.Parent).DataItem).Country %>

                    Code-behind file: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { using (NorthwindModel.NorthwindEntities context = new NorthwindModel.NorthwindEntities()) { ListView1.DataSource = from c in context.Customers orderby c.CustomerID select c; ListView1.DataBind(); } } protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) { string customerID = ((NorthwindModel.Customer

                    S 1 Reply Last reply
                    0
                    • P pmarfleet

                      Yes. You can use a data binding expression to do this. Below I have posted the code for a page that displays a hierarchical list of customers with orders from the sample Northwind database using ADO.NET Entity Framework. I use a databinding expression in the child list control displaying order details to display the customer's country against every order. The databinding expression is a bit clumsy (lots of calls to .Parent) but it works. ASPX code: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

                      • <%#Eval("CustomerID") %>
                      • <%#Eval("OrderID") %> <%#((NorthwindModel.Customers)((ListViewDataItem)Container.Parent.Parent.Parent).DataItem).Country %>

                      Code-behind file: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { using (NorthwindModel.NorthwindEntities context = new NorthwindModel.NorthwindEntities()) { ListView1.DataSource = from c in context.Customers orderby c.CustomerID select c; ListView1.DataBind(); } } protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) { string customerID = ((NorthwindModel.Customer

                      S Offline
                      S Offline
                      SmartSpider
                      wrote on last edited by
                      #10

                      Aha! The Container.Parent - thanks a lot Paul. I did find a workaround, but this is definitely looks sleeker. My workaround: In the ItemDataBound of parent lv, where I bind the child lv's data, I modified my SQL - I now first retrieve the category name using:

                      string catgName = ((System.Data.DataRowView)((ListViewDataItem)e.Item).DataItem)["quesCatgName"].ToString();

                      Then set my SQL to:

                      sqlStr = "SELECT quesId, quesText, quesHint, '" + catgName + "' as quesCatgName FROM....."

                      This way, the category is available as 'quesCatgName' in the child lv - dunno if this causes a greater overhead - I will modify my code to use the approach you sent. Thanks, ~R

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups