Web-based Surveys Made Easy

xiaoxiao2021-03-06  123

IntroductionMost Web masters, at one time or another, are asked to create an online survey for their company's web site. This is not a terribly difficult task with just plain HTML and traditional ASP, but suppose you'd like to be able to create a survey quickly - I mean really quickly - and you do not want to be stuck with the same look and feel you used last time Rather, you want to create a survey with completely new questions and a fresh new look and feel. That Might Be a bit time consuming, depending on how much of Your Original Code You're Able To Reuse.

Suppose there is the could be added to an asp.net web unedad a Simple Declarative Syntax Like , and poof !, you've got a survey. Wouldn ' t that make life easier? Well, that control exists. It's called WebSurvey, and it's an ASP.NET Server Control I created specifically for the purpose of generating customizable web-based surveys quickly and easily.

To add such surveys to your Web site, all you need is copy of the assembly (the .dll file, available at the end of this article) and some basic knowledge of XML. In this article I will show you how you can use WebSurvey TO Quickly and Easily Add Surveys to your Web Site. in A Future Article I Will Examine The Inner Workings of The Websurvey Control in More Detail.

Using the WebSurvey ControlGenerating a survey with the WebSurvey control is surprisingly easy. If you've ever worked with an ASP.NET Web page before, and if you can create a little bit of well-formed XML, you should have the survey up and Running in no time. to begin, you'll ued to create an asp.net page to house the Websurvey Control.

<% @ Register tagprefix = "sstchur" namespace = "sstchur.web.survey" assembly = "sstchur.web.survey"%> <% @ page language = "c #"%>

The first thing to to TO notice is the @Register directive that registers the control. This directive tells the ASP.NET Web page that any declarative Web control syntax prefixed with sstchur references a control defined in the sstchur.web.survey assembly and namespace. (The sstchur.web. Survey Assembly Contains The Class for the Websurvey Control, and this assembly will need to be added to your web copflication's / bin directory.)

If you are using Visual Studio .NET, you can add the survey control to the Toolbox and then drag and drop it onto an ASP.NET Web page's Designer. This will automatically add the @Register directive and the declarative syntax in the ASP.NET WEB Page's HTML PORTION. You'll Also Need To Add The assembly to the project's references folder. (This Has The Effect of Adding the file to the application's / bin Directory.)

In the HTML portion you'll find the declarative syntax for the WebSurvey control, Wherever you place this syntax is where the control will be rendered You'll notice a few of the control's properties have been.. assigned declaratively (you could also use the Properties pane in the Designer in VS.NET). The WebSurvey control derives from System.Web.UI.WebControls.WebControl, meaning that it has all of the properties that a standard ASP.NET Web control has, such as BackColor, Font, Height, Width, and so on In addition, WebSurvey has two other required properties:.. SurveyFile - relative path to the XML file that contains the survey's questions AnswersFile - relative path to the XML file that will "This File NEED NOT INITIALLY".

Creating the survey.xml File As it stands right now, the sample code we examined above will not run if the SurveyFile, survey.xml, has not yet been created. If the WebSurvey control can not find the specified SurveyFile, it will throw a .

Begin by opening notepad or the text editor of your choاit.xml That Contains The Following XML:

please enter your first name: with this sample survey file , we now have a working survey example. If you revisit the page, instead of receiving a FileNotFoundException, you'll instead see a screen similar to the one shown to the left. Granted, it's not terribly exciting at this point, but we ' LL Take Care of That A Little Later ON.

For now, let's examine the tags in this XML file and the attributes that go along with them. The tag is the XML document's root element and has three attributes, all of which are required.

redirecturl - The file to display once the survey has been completed It could be the same file (as in the example) or a completely different file allowrepeats -.. When set to true, this attribute tells the WebSurvey to allow the same user . to complete the survey multiple times Any other value is interpreted as false and will not allow repeats This is accomplished by checking for the existence of a cookie defined by the cookiename attribute cookiename -.. The name of the cookie to be generated in the .

The Element CAN Consist of a Number of Elements, Specifying a Set of Questions on One Particular Page. That IS, Each ... pair specifies ONE Page of Questions in the survey. note that every survey requires AT LEAST one tag Each element has three attributes.id (required) -. A unique ID assigned to the tag It can be any valid string, but should be unique. From any other tag's id. nextImage (jpg or gif) Used as the "next" or "finish" Button. if you have only one page of qustions, IT ACTS AS A . Finish (or Submit) button If you have multiple pages, it acts as a Next button for all pages except the last page backimage -. This attribute is only applicable if you have multiple pages of questions in your survey Otherwise, it is. Meaningless and ignored. if You Have Multiple tags, this attribute is required on all tags except the first one.

Each element can consist of a variable number of elements. Each element represents a question the user is asked. The element has a variant number of attributes (depending on the type of question we ' Re De DEALING WITH. Here Area of ​​The More Germane Attributes.

id - A unique ID to represent this question This can be essentially anything, but I would recommend something that describes the question type - Can be one of 4 types:.. mcss (multiple choice, single selection); mcms (multiple choice , multiple selection); shortans (short answer);... or essay required - true if the question if required Any other value is interpreted as false rows - Applicable only to questions of type essay This attribute defines how many rows (. the height) of the Text Area cols -. Applicable to question of type shortans and essay This attributes defined how many columns (the width) of the TextBox.Inside the element you'll find a element.. The element contains the text that is displayed for the question HTML markup is valid here, but keep in mind that we're dealing with XML so XML formatting rules apply -. all tags need to be closed (for example,
Needs to be
) And Properly Nested, And Attribute Values ​​Must Be within Quo TATION MARKS.

For A More Extensive Explanation of Websurvey Tags and Attributes, Visit My Controls Web Site At http://www.sycbp.com/controls/websurvey/.

An Example SurveyTo demonstrate the survey XML file, let's look at a complete, multi-page survey example. Before we look at the updated survey.xml file, I took a moment to gussy up the ASP.NET Web page's HTML syntax, which improves The Appearance of The Survey Results. I used a bit of css and some tables for positioning.

<% @ Register tagprefix = "sstchur" namespace = "sstchur.web.survey" assembly = "sstchur.web.survey"%> <% @ page language = "c #"%>