[Back to: The ADefWebserver DotNetNuke HELP WebSite]
Download: SimpleSurvey_01.00.00_Install.zip (this link contains the install and source) (note: If using DNN4 install and run LinqPrep first. If using DNN5 follow these directions first)
Source:
App_Code
SimpleSurvey
DesktopModules
When the Simple Survey module is installed and placed on a page, the administrator uses the Edit Survey button to create a new survey.
The administrator can indicate if each question is Required or Visible.
Each question has a type. For Dropdown, Radio Buttons, and Check Boxes, the Edit Items link appears and allows the administrator to enter possible options.
11 question types are available including the Image type which allows the survey taker to upload a image. The questions can also be sorted using the arrow keys.
The survey taker is allowed to return to the survey and change their responses.
The survey validates each response to ensure the value entered is the correct type.
Items of type: Dropdown, Radio Buttons, and Check Boxes, are tabulated on the Survey Results page.
The Administrator Survey Results page allows the administrator to see all response data.
All the data for the module is stored in 3 tables.
A simple form is used to give the Administrator the ability to insert new questions.
When the type is set to Dropdown, Radio Buttons, or Check Boxes, the Edit Items link appears. Clicking this link brings up a form that is bound to the SurveyChoices class that is stored in ViewState:
#region ViewState [Serializable] public class SurveyChoiceOption { public int SurveyChoiceID { get; set; } public string choice { get; set; } } public List<SurveyChoiceOption> SurveyChoices { get { if (ViewState["SurveyChoices"] == null) { return new List<SurveyChoiceOption>(); } else { return (List<SurveyChoiceOption>)ViewState["SurveyChoices"]; } } set { ViewState["SurveyChoices"] = value; } } #endregion
When an item is inserted, this code is used:
#region lvSurveyChoices_ItemInserting protected void lvSurveyChoices_ItemInserting(object sender, ListViewInsertEventArgs e) { ListViewItem objListViewItem = (ListViewItem)e.Item; TextBox objTextBox = (TextBox)objListViewItem.FindControl("choiceTextBox"); SurveyChoiceOption objSurveyChoiceOption = new SurveyChoiceOption(); objSurveyChoiceOption.SurveyChoiceID = 0; objSurveyChoiceOption.choice = objTextBox.Text.Replace(",",""); List<SurveyChoiceOption> tmpSurveyChoices = SurveyChoices; tmpSurveyChoices.Add(objSurveyChoiceOption); SurveyChoices = tmpSurveyChoices; DisplaySurveyChoices(); } #endregion
When an item is deleted, this code is used:
#region lvSurveyChoices_ItemDeleting protected void lvSurveyChoices_ItemDeleting(object sender, ListViewDeleteEventArgs e) { SurveyChoices.RemoveAt(e.ItemIndex); DisplaySurveyChoices(); } #endregion
When a question is saved, this code is used:
#region InsertButton_Click protected void InsertButton_Click(object sender, EventArgs e) { SimpleSurveyDataContext SimpleSurveyDataContext = new SimpleSurveyDataContext(); // Get highest Sort Order var intHighestSortOrder = from DataItem in SimpleSurveyDataContext.SurveyDataItems where DataItem.ModuleID == ModuleId orderby DataItem.SortOrder select DataItem; // Insert the question SurveyDataItem objSurveyDataItem = new SurveyDataItem(); objSurveyDataItem.DataName = DataNameTextBox.Text; objSurveyDataItem.Type = ddlTypeInsert.SelectedValue; objSurveyDataItem.ModuleID = ModuleId; objSurveyDataItem.Required = RequiredCheckBox.Checked; objSurveyDataItem.Visible = VisibleCheckBox.Checked; objSurveyDataItem.SortOrder = (intHighestSortOrder.FirstOrDefault() == null) ? 0 : Convert.ToInt32(intHighestSortOrder.Max(x => x.SortOrder).Value) + 1; SimpleSurveyDataContext.SurveyDataItems.InsertOnSubmit(objSurveyDataItem); SimpleSurveyDataContext.SubmitChanges(); int SurveyDataItemID = objSurveyDataItem.SurveyDataItemID; if (SurveyChoices.Count > 0) { foreach (SurveyChoiceOption objSurveyChoiceOption in SurveyChoices) { SurveyChoice objSurveyChoice = new SurveyChoice(); objSurveyChoice.SurveyDataItemID = SurveyDataItemID; objSurveyChoice.choice = objSurveyChoiceOption.choice; SimpleSurveyDataContext.SurveyChoices.InsertOnSubmit(objSurveyChoice); SimpleSurveyDataContext.SubmitChanges(); } } // Reset Insert line DataNameTextBox.Text = ""; ddlTypeInsert.SelectedIndex = 0; BackToQuestions(); DisplaySurveyQuestions(); } #endregion
A ListView is used to display the current questions. The following ItemTemplate is used to format the display:
<ItemTemplate> <tr style=""> <td nowrap="nowrap"> <asp:Button ID="DeleteButton" runat="server" OnClientClick='if (!confirm("Are you sure you want to delete? Any answers will also be deleted!") ){return false;}' CommandName="Delete" Text="Delete" CommandArgument='<%# Eval("SurveyDataItemID") %>' /> <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" /> </td> <td> <asp:Label ID="DataNameLabel" runat="server" Text='<%# Eval("DataName") %>' /> </td> <td align="center"> <asp:Label ID="TypeLabel" runat="server" Text='<%# Eval("Type") %>' /> </td> <td align="center"> <asp:CheckBox ID="RequiredCheckBox" runat="server" Checked='<%# Eval("Required") %>' Enabled="false" /> </td> <td align="center"> <asp:CheckBox ID="VisibleCheckBox" runat="server" Checked='<%# Eval("Visible") %>' Enabled="false" /> </td> <td align="center" nowrap="nowrap"> <asp:Label ID="SortOrderLabel" runat="server" Text='<%# Eval("SortOrder") %>' Visible="False" /> <asp:ImageButton ID="imgUp" runat="server" CommandName="Sort" CommandArgument='<%# Eval("SurveyDataItemID") %>' ImageUrl="~/DesktopModules/SimpleSurvey/Images/action_up.gif" /> <asp:ImageButton ID="imgDown" runat="server" CommandName="Sort" CommandArgument='<%# Eval("SurveyDataItemID") %>' ImageUrl="~/DesktopModules/SimpleSurvey/Images/action_down.gif" /> </td> </tr> </ItemTemplate>
The ItemDataBound event provides additional formatting:
#region lvSurveyItems_ItemDataBound protected void lvSurveyItems_ItemDataBound(object sender, ListViewItemEventArgs e) { ListView objListView = (ListView)sender; // Only do this if we are editing the ListView if (objListView.EditIndex > -1) { // Only do this if we are editing this item if (e.Item.ID == objListView.EditItem.ID) { Label TypeLabel = (Label)e.Item.FindControl("TypeLabel"); LinkButton lnkEditItems = (LinkButton)e.Item.FindControl("lnkEditItems"); // Show the Edit Items if this question has item choices if ( TypeLabel.Text == "Dropdown" || TypeLabel.Text == "Radio Buttons" || TypeLabel.Text == "Check Boxes" ) { lnkEditItems.Visible = true; } } } else { // We are not editing // Determine if the Sort buttons should show // If this item is the first item, hide the up button if (e.Item.ID == "ctrl0") { ImageButton imgUp = (ImageButton)e.Item.FindControl("imgUp"); imgUp.ImageUrl = "~/DesktopModules/SimpleSurvey/Images/action_blank.gif"; imgUp.Enabled = false; } // If this item is the last item, hide the up down if (e.Item.ID == String.Format("ctrl{0}", (intSurveyQuestions - 1).ToString())) { ImageButton imgDown = (ImageButton)e.Item.FindControl("imgDown"); imgDown.ImageUrl = "~/DesktopModules/SimpleSurvey/Images/action_blank.gif"; imgDown.Enabled = false; } } } #endregion
The ItemCommand that is fired whenever any button on the ListView control is pressed. This method handles the Deleting and Sorting.
#region lvSurveyItems_ItemCommand protected void lvSurveyItems_ItemCommand(object sender, ListViewCommandEventArgs e) { // Deleting if (e.CommandName == "Delete") { Button objButton = (Button)e.CommandSource; int intSurveyDataItemID = Convert.ToInt32(objButton.CommandArgument); SimpleSurveyDataContext SimpleSurveyDataContext = new SimpleSurveyDataContext(); // Get the Question var objDataItem = from DataItem in SimpleSurveyDataContext.SurveyDataItems where DataItem.SurveyDataItemID == intSurveyDataItemID select DataItem; // Get the SurveyChoices var colSurveyChoices = from SurveyChoices in SimpleSurveyDataContext.SurveyChoices where SurveyChoices.SurveyDataItemID == intSurveyDataItemID select SurveyChoices; // Get the Answers var colSurveyDataAnswers = from SurveyDataAnswers in SimpleSurveyDataContext.SurveyDataAnswers where SurveyDataAnswers.SurveyDataItemID == intSurveyDataItemID select SurveyDataAnswers; // Delete them all SimpleSurveyDataContext.SurveyDataItems.DeleteOnSubmit(objDataItem.FirstOrDefault()); SimpleSurveyDataContext.SurveyChoices.DeleteAllOnSubmit(colSurveyChoices); SimpleSurveyDataContext.SurveyDataAnswers.DeleteAllOnSubmit(colSurveyDataAnswers); SimpleSurveyDataContext.SubmitChanges(); DisplaySurveyQuestions(); } // Sorting if (e.CommandName == "Sort") { ListView objListView = (ListView)sender; // Only do this if we are not editing the ListView if (objListView.EditIndex == -1) { int? intCurrentSortOrder = 0; int intPreviousSurveyDataItemID = 0; int intNextSurveyDataItemID = 0; // Get Button information ImageButton objImageButton = (ImageButton)e.CommandSource; int intSurveyDataItemID = Convert.ToInt32(objImageButton.CommandArgument); SimpleSurveyDataContext SimpleSurveyDataContext = new SimpleSurveyDataContext(); //Get current SortID intCurrentSortOrder = (from SurveyDataItems in SimpleSurveyDataContext.SurveyDataItems where SurveyDataItems.SurveyDataItemID == intSurveyDataItemID select SurveyDataItems).FirstOrDefault().SortOrder; if (objImageButton.ID == "imgUp") { //Up intPreviousSurveyDataItemID = (from SurveyDataItems in SimpleSurveyDataContext.SurveyDataItems where SurveyDataItems.SortOrder < intCurrentSortOrder orderby SurveyDataItems.SortOrder descending select SurveyDataItems).FirstOrDefault().SurveyDataItemID; SwitchQuestions(intSurveyDataItemID, intPreviousSurveyDataItemID); } else { //Down intNextSurveyDataItemID = (from SurveyDataItems in SimpleSurveyDataContext.SurveyDataItems where SurveyDataItems.SortOrder > intCurrentSortOrder orderby SurveyDataItems.SortOrder ascending select SurveyDataItems).FirstOrDefault().SurveyDataItemID; SwitchQuestions(intSurveyDataItemID, intNextSurveyDataItemID); } DisplaySurveyQuestions(); } } } #endregion
The following method is used to handle the Updating:
#region lvSurveyItems_ItemUpdating protected void lvSurveyItems_ItemUpdating(object sender, ListViewUpdateEventArgs e) { ListView objListView = (ListView)sender; ListViewItem objListViewItem = (ListViewItem)objListView.EditItem; Label lblSurveyDataItemID = (Label)objListViewItem.FindControl("lblSurveyDataItemID"); TextBox DataNameTextBox = (TextBox)objListViewItem.FindControl("DataNameTextBox"); CheckBox RequiredCheckBox = (CheckBox)objListViewItem.FindControl("RequiredCheckBox"); CheckBox VisibleCheckBox = (CheckBox)objListViewItem.FindControl("VisibleCheckBox"); SimpleSurveyDataContext SimpleSurveyDataContext = new SimpleSurveyDataContext(); // Get the Question var objDataItem = (from DataItem in SimpleSurveyDataContext.SurveyDataItems where DataItem.SurveyDataItemID == Convert.ToInt32(lblSurveyDataItemID.Text) select DataItem).FirstOrDefault(); // Update the Question objDataItem.DataName = Strings.Left(DataNameTextBox.Text, 200); objDataItem.Required = RequiredCheckBox.Checked; objDataItem.Visible = VisibleCheckBox.Checked; SimpleSurveyDataContext.SubmitChanges(); // Delete all existing SurveyChoices var colSurveyChoices = from CurrentSurveyChoices in SimpleSurveyDataContext.SurveyChoices where CurrentSurveyChoices.SurveyDataItemID == Convert.ToInt32(lblSurveyDataItemID.Text) select CurrentSurveyChoices; SimpleSurveyDataContext.SurveyChoices.DeleteAllOnSubmit(colSurveyChoices); SimpleSurveyDataContext.SubmitChanges(); // If there are SurveyChoices, add them if (SurveyChoices.Count > 0) { // Add all the SurveyChoices in the ViewState foreach (SurveyChoiceOption objSurveyChoiceOption in SurveyChoices) { SurveyChoice objSurveyChoice = new SurveyChoice(); objSurveyChoice.SurveyDataItemID = Convert.ToInt32(lblSurveyDataItemID.Text); objSurveyChoice.choice = objSurveyChoiceOption.choice; SimpleSurveyDataContext.SurveyChoices.InsertOnSubmit(objSurveyChoice); SimpleSurveyDataContext.SubmitChanges(); } } objListView.EditIndex = -1; DisplaySurveyQuestions(); } #endregion
Buy DotNetNuke Modules from Snowcovered |
DotNetNuke™ is a registered trademark of DotNetNuke Corporation.