Develop a Survey Application: Part II

Managing Questions and Answers

CodeTalk

LANGUAGES: VB.NET

ASP.NET VERSIONS: 2.0

 

Develop a Survey Application: Part II

Managing Questions and Answers

 

By Bipin Joshi

 

In Part I of this series we designed the database to store information for our survey application. We developed a master page for all the administrative Web forms. We also developed a Web form for managing survey definitions. Any survey consists of a set of questions and possible choices. The administrator needs to define questions and their choices for a particular survey. Additionally, participants of a survey need to be specified so that invitations can be sent to them. This is precisely the theme of this second installment in our series.

 

Managing Survey Questions and Choices

Now we ll proceed to develop two more Web forms: ManageQuestions.aspx and ManageChoices.aspx. The former allows you to manage questions belonging to a survey; the latter allows you to manage choices of objective-type questions. To start, add these two Web forms to the Admin folder of the Web site. The general layout of the ManageQuestions.aspx is shown in Figure 1.

 


Figure 1: Layout of ManageQuestions.aspx.

 

There is a DropDownList at the top of the Web form that lists all the available surveys. When a survey is selected, the questions belonging to that survey are displayed in a DetailsView control. The administrator can then add, edit, or delete the questions.

 

To design this Web form, drag and drop two SQL data source controls on the Web form. Configure the first (SqlDataSource1) SQL data source control to select SurveyID and Title from the Survey table. Similarly, configure the other SQL data source control (SqlDataSource2) to select all the rows from the SurveyQuestions table that match the selected SurveyID. To add this WHERE condition you must use the Add WHERE Clause dialog box of the SQL data source configuration wizard. Figure 2 shows this dialog with the WHERE condition specified.

 


Figure 2: The Add WHERE Clause dialog box.

 

Also, ensure that the Generate INSERT, UPDATE, and DELETE statements checkbox is checked in the Advanced SQL Generation Options dialog box (see Figure 11 in Part I). Now, drag and drop a DropDownList control and set its DataSourceID property to SqlDataSource1. Set its DataTextField and DataValueField properties to Title and SurveyID, respectively. Also, set its AutoPostBack property to True.

 

Next, drag and drop a DetailsView control on the Web form and set its DataSourceID property to SqlDataSource2. From the smart tag of the DetailsView enable paging, inserting, editing, and deleting. The Question and AnswerType fields of the DetailsView control are template fields. The Question template contains a multi-line Textbox, whereas the AnswerType field contains a DropDownList with required options. Convert these two fields into TemplateFields using the Fields dialog box. Creating the Question template field is fairly straightforward. Simply set the TextMode property of the Textbox from EditItemTemplate and InsertItemTemplate to MultiLine. To create the AnswerType template field, drag and drop a DropDownList control in EditItemTemplate and InsertItemTemplate, and add three items to both DropDownLists, as shown in Figure 3.

 


Figure 3: Adding items to the DropDownList.

 

Note that the Value property of the Single Choice ListItem is set to S, that of Multiple Choices is set to M, and that of Text is set to T. Figure 4 shows these DropDownLists after adding the items. Now, select the Edit DataBindings option from the smart tag of both the DropDownLists and bind the SelectedValue property to the AnswerType column (Figure 5).

 


Figure 4: EditItemTemplate and InsertItemTemplate of DetailsView.

 


Figure 5: Data binding the SelectedValue property of DropDownList.

 

The DetailsView does not include SurveyID column. However, we need to set this column during INSERT or UPDATE operations because each question must belong to some survey. This is done by handling ItemInserting and ItemUpdating events. Figure 6 shows these event handlers.

 

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,

 ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs)

 Handles DetailsView1.ItemInserting

e.Values("SurveyID") = DropDownList1.SelectedValue

End Sub

Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object,

 ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs)

 Handles DetailsView1.ItemUpdating

e.NewValues("SurveyID") = DropDownList1.SelectedValue

End Sub

Figure 6: Handling ItemInserting and ItemUpdating event handlers in ManageQuestions.aspx.

 

The event handlers set the SurveyID parameter from the Values and NewValues collections with the one selected in the DropDownList. Listing One shows the complete markup of ManageQuestions.aspx. Now, let s develop the ManageChoices.aspx; Figure 7 shows the general layout of this Web form.

 


Figure 7: Layout of ManageChoices.aspx.

 

The Web form consists of a DropDownList presenting a list of surveys. Below the DropDownList is a GridView that displays all the objective-type questions from the selected survey. Selecting a question displays its choices in a DetailsView, wherein you can add, edit, or delete them.

 

To design this Web form, add three SQL data source controls on the Web form. Configure the first to select all the records from the Survey table. Configure the second to fetch all the questions matching the selected SurveyID. Finally, configure the third to select all the choices from the SurveyChoices table for the selected QuestionID from the GridView. Add the WHERE conditions for the second and the third SQL data source control, as previously discussed. Also, ensure that the Generate INSERT, UPDATE, and DELETE statements checkbox is checked in the Advanced SQL Generation Options dialog box.

 

Now, drag and drop a DropDownList and bind it with SqlDataSource1. Similarly, drag and drop a GridView and a DetailsView and bind them with SqlDataSource2 and SqldataSource3, respectively.

 

When any of the questions is selected in the Grid that does not have any choices added yet, we display the DetailsView in insert mode otherwise we display it in read-only mode. This is achieved by handling the DataBound event of DetailsView, as shown in Figure 8.

 

Protected Sub DetailsView1_DataBound(ByVal sender As Object,

 ByVal e As System.EventArgs) Handles DetailsView1.DataBound

If DetailsView1.Rows.Count = 0 Then

  DetailsView1.ChangeMode(DetailsViewMode.Insert)

End If

End Sub

Figure 8: Handling the DataBound event of DetailsView.

 

The event handler checks the row count of the DetailsView control. If it is zero, the mode of the DetailsView is changed to Insert using the ChangeMode method.

 

The DetailsView doesn t include a QuestionID column; nevertheless, it is required while inserting or updating a record. The ItemInserting and ItemUpdating event handlers do the trick, as before. Figure 9 shows these event handlers.

 

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,

 ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs)

 Handles DetailsView1.ItemInserting

e.Values("QuestionID") = GridView1.SelectedValue

End Sub

Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object,

 ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs)

 Handles DetailsView1.ItemUpdating

e.NewValues("QuestionID") = GridView1.SelectedValue

End Sub

Figure 9: The ItemInserting and ItemUpdating event handlers in ManageChoices.aspx.

 

The event handlers set the QuestionID parameter from the Values and NewValues collections with the one selected in the GridView. Listing Two, shows the complete markup of ManageChoices.aspx.

 

Managing Survey Participants

Managing survey participants is fairly easy. Figure 10 shows how this Web form looks. It consists of a DropDownList that presents all the surveys. Upon selecting a survey, all the participants of that survey are displayed in a DetailsView. Using the DetailsView you can add, edit, or delete participants.

 


Figure 10: Layout of ManageParticipants.aspx.

 

To design the Web form, drag and drop two SQL data source controls on it. Configure the first to select all the records from the Survey table; configure the other to select all the participants from the SurveyParticipants table that match the selected survey. Then drag and drop a DropDownList control on the Web form and set its properties, as in the previous case. Drag and drop a DetailsView control on the Web form and set its DataSourceID property to SqlDataSource2. When you add a new survey it won t have any participants. In that case, the DetailsView should be shown in insert mode; otherwise, it should be shown in read-only mode. This is achieved by handling the DataBound event of DetailsView (see Figure 11).

 

Protected Sub DetailsView1_DataBound(ByVal sender As Object,

 ByVal e As System.EventArgs) Handles DetailsView1.DataBound

If DetailsView1.Rows.Count = 0 Then

  DetailsView1.ChangeMode(DetailsViewMode.Insert)

End If

End Sub

Figure 11: Handling the DataBound event of DetailsView.

 

The code should be familiar because we used similar code in ManagedChoices.aspx. The SurveyID column of the SurveyParticipants table is not displayed in the DetailsView, but we need to supply it during inserts and updates. This is achieved by handling the ItemInserting and ItemUpdating events of the DetailsView (see Figure 12).

 

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,

 ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs)

 Handles DetailsView1.ItemInserting

  e.Values("SurveyID") = DropDownList1.SelectedValue

End Sub

Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object,

 ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs)

 Handles DetailsView1.ItemUpdating

  e.NewValues("SurveyID") = DropDownList1.SelectedValue

End Sub

Figure 12: Handling the ItemInserting and ItemUpdating events of DetailsView.

 

The event handlers set the SurveyID parameter from the Values and NewValues collections with the one selected in the DropDownList. Listing Three shows the complete markup of ManageParticipants.aspx. This completes the major part of our administrative pages.

 

Conclusion

In this installment we developed Web forms for managing survey questions and their choices. A question can have single choice, multiple choice, or free text answers. Depending on the answer type, we need to render RadioButtons, CheckBoxes, or TextBoxes when the user takes the survey. Each survey has participants who are notified about the survey by e-mail. We developed a Web form for managing these participants. In Part III we ll see how to send invitations to the participants, generate the survey form, and generate survey statistics.

 

The sample code for this series is available for download.

 

Bipin Joshi is the founder and owner of BinaryIntellect Consulting (http://www.binaryintellect.com), where he conducts professional training programs on .NET technologies. He is the author of Developer s Guide to ASP.NET 2.0 (http://www.binaryintellect.com/books) and co-author of three WROX books on .NET 1.x. He writes regularly for http://www.DotNetBips.com, a community Web site he founded in the early days of .NET. He is a Microsoft MVP, MCAD, MCT, and member of ASPInsiders. He jots down his thoughts about .NET, life, and Yoga at http://www.bipinjoshi.com. He also conducts workshops on Yoga and Meditation, where he helps IT professionals develop a positive personality. You can contact him at mailto:[email protected].

 

Begin Listing One Markup of ManageQuestions.aspx

<%@ Page Language="VB" MasterPageFile="~/Admin/AdminMasterPage.master" AutoEventWireup="false" CodeFile="ManageQuestions.aspx.vb" Inherits="Admin_ManageQuestions" title="Untitled Page" %>

DataTextField="Title" DataValueField="SurveyID">

CellPadding="4" DataKeyNames="QuestionID" DataSourceID="SqlDataSource2" ForeColor="#333333"

GridLines="None" Height="50px" Width="100%">

ReadOnly="True" SortExpression="QuestionID" />

Single Choice

Multiple Choices

Text

Single Choice

Multiple Choices

Text

SelectCommand="SELECT [SurveyID], Develop a Survey Application: Part II FROM [Survey] ORDER BY Develop a Survey Application: Part II">

DeleteCommand="DELETE FROM [SurveyQuestions] WHERE [QuestionID] = @QuestionID"

InsertCommand="INSERT INTO [SurveyQuestions] ([SurveyID], [Question], [AnswerType]) VALUES (@SurveyID, @Question, @AnswerType)"

SelectCommand="SELECT * FROM [SurveyQuestions] WHERE ([SurveyID] = @SurveyID)"

UpdateCommand="UPDATE [SurveyQuestions] SET [SurveyID] = @SurveyID, [Question] = @Question, [AnswerType] = @AnswerType WHERE [QuestionID] = @QuestionID">

Type="Int32" />

End Listing One

 

Begin Listing Two Markup of ManageChoices.aspx

<%@ Page Language="VB" MasterPageFile="~/Admin/AdminMasterPage.master" AutoEventWireup="false" CodeFile="ManageChoices.aspx.vb" Inherits="Admin_ManageChoices" title="Untitled Page" %>

Text="Select Survey :">

DataTextField="Title" DataValueField="SurveyID">

AutoGenerateColumns="False" CellPadding="4" DataKeyNames="QuestionID" DataSourceID="SqlDataSource2"

ForeColor="#333333" GridLines="None" PageSize="5" Width="100%">

ReadOnly="True" SortExpression="QuestionID">

CellPadding="4" DataKeyNames="ChoiceID" DataSourceID="SqlDataSource3" ForeColor="#333333"

GridLines="None" Height="50px" Width="100%">

ReadOnly="True" SortExpression="ChoiceID" />

SelectCommand="SELECT [SurveyID], Develop a Survey Application: Part II FROM [Survey]">

SelectCommand="SELECT * FROM [SurveyQuestions] WHERE (([SurveyID] = @SurveyID) AND ([AnswerType] <> @AnswerType))">

Type="Int32" />

DeleteCommand="DELETE FROM [SurveyChoices] WHERE [ChoiceID] = @ChoiceID" InsertCommand="INSERT INTO [SurveyChoices] ([QuestionID], [Choice]) VALUES (@QuestionID, @Choice)"

SelectCommand="SELECT * FROM [SurveyChoices] WHERE ([QuestionID] = @QuestionID)"

UpdateCommand="UPDATE [SurveyChoices] SET [QuestionID] = @QuestionID, [Choice] = @Choice WHERE [ChoiceID] = @ChoiceID">

Type="Int32" />

End Listing Two

 

Begin Listing Three Markup of ManageParticipants.aspx

<%@ Page Language="VB" MasterPageFile="~/Admin/AdminMasterPage.master" AutoEventWireup="false" CodeFile="ManageParticipants.aspx.vb" Inherits="ManageParticipants" title="Untitled Page" %>

DataTextField="Title" DataValueField="SurveyID">

CellPadding="4" DataKeyNames="ContactID" DataSourceID="SqlDataSource2" ForeColor="#333333"

GridLines="None" Height="50px" Width="100%">

ReadOnly="True" SortExpression="ContactID" />

SelectCommand="SELECT [SurveyID], Develop a Survey Application: Part II FROM [Survey]">

DeleteCommand="DELETE FROM [SurveyParticipants] WHERE [ContactID] = @ContactID"

InsertCommand="INSERT INTO [SurveyParticipants] ([SurveyID], [Name], [Email]) VALUES (@SurveyID, @Name, @Email)"

SelectCommand="SELECT * FROM [SurveyParticipants] WHERE ([SurveyID] = @SurveyID)"

UpdateCommand="UPDATE [SurveyParticipants] SET [SurveyID] = @SurveyID, [Name] = @Name, [Email] = @Email WHERE [ContactID] = @ContactID">

Type="Int32" />

End Listing Three

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish