Creating an ASP.NET 2.0 Polling User Control: Design Decisions and the Data ModelBy Scott Mitchell
Many websites include some sort of polling user interface through which visitors can cast their vote on the topic du jour. The typical polling user interface includes some question text, like "What is your favorite programming language?", along with a series of radio buttons listing possible answers, like "C#", "Visual Basic", "Pascal", and so forth. The visitor can select an answer and then cast his vote. After voting, the interface changes to show the current voting breakdown per answer, as the screen shots below illustrate.
This article is the first in a three-part series that examines how to build a data-driven reusable polling User Control. In this first part we'll look at the design decisions made in creating this control as well as the polling data model. The second installment will step through the process of building the polling user interface while the third and final installment will examine the administration portion. Read on to learn more!
Build or Buy?
I assume you are reading this article for either one of two reasons: either you need a polling solution for your ASP.NET 2.0 website or you are interested in seeing another developer's approach to building a resuable polling User Control (or maybe both). If you are reading this for educational purposes, then read ahead! But if you are reading because you need to add polling capabilities to your website, I would encourage you to consider all your options before settling on this polling User Control. Also be sure to closely read the "Design Decisions" section to make sure that my poll control's intent and functionality aligns with your requirements.
One option is to build your own polling control from the ground up (and maybe that is what you intend to do while using this polling User Control as a template). Before building your own polling control or using the polling User Control provided at the end of this article, it's worthwhile to examine all available options. There are third-party polling controls out there and those may meet your needs better than the one presented here. And if you are considering building your own polling control, keep in mid that oftentimes it is much more affordable (in terms of man hours spent) to buy a third-party control or to integrate an existing freeware option that to build your own solution from the ground up.
Unfortunately, there are few third-party polling implementations available. Yes, there are some, but there are far more charting-, grid-, and navigation-related controls on the market than there are polling controls. Regardless, the polling controls out there do deserve some investigation. Peter Blum offers a very thorough commercial implementation called Peter's Polling Package. I've not used Peter's Polling Package, but I have used other products of his (Peter's Date Package and Professional Validation and More) and can testify to the quality and top-notch support of his products. At the time of this writing (August 2007), Peter's Polling Package sells for $50 per web server.
There are also a handful of freeware polling controls available (in addition to the one we will examine and create in this article and the next two installments). Check out the Voting category in the ASP.NET Control Gallery on Microsoft's ASP.NET website for more information.
When creating the polling User Control I needed to decide on what features the control would implement. For example, what steps would a page developer need to take to have a poll displayed on a page in their website? How would a page developer indicate the poll question and answer set? Who could take the poll: any visitor or just authenticated users? What measures (if any) would be put in place to prevent ballot stuffing?
The following bullet points outline the key design decisions that guided the development of the polling User Control. Of course, these design decisions can be altered or reversed by modifying the code, and you are welcome to modify, extend, and customize the polling control as much or as little as needed.
- Preferred simplicity over elegance - when designing the polling User Control I opted for simpler approaches rather than necessarily choosing the approach that was in line with best practices. The most striking example of this is my decision to use SqlDataSource controls and to execute SQL queries directly from the presentation layer. Ideally, there would be a polling control API where all database communications would occur, and the User Control would interface solely with this middle tier.
- Database-backed poll data - the poll, its possible answers, and the users' responses are stored in a SQL Server database.
Alternatively, I could have opted to use an XML file or a flat file of some sort, but databases are, in my opinion, easier
to work with and superior to XML or flat files when running reports or summarizing data. The download at the end of
this article includes a SQL Server 2005 Express Edition database in the
App_Datafolder. However, you can use your own database by moving the data schema to your database of choice and then updating the connection string information in
- The page developer must specify what poll to display - some polling controls have a date range associated
with each poll; the polling control then automatically displays the "current" poll. This is advantageous because a
page developer can create a number of polls in the database, specify non-overlapping date ranges, and then have
the polls automatically appear without needing to modify the page on the web server. Such approaches, however, only
allow the "current" poll to be displayed. What if you want to show one poll on one page, and a different one on another?
Or what if you want to show two different polls on the same page? To allow for this flexibility, I designed the polling
User Control so that it included a
PollIDproperty. The User Control displays the poll as specified by this ID. To change the displayed poll, you will need to update the User Control's
PollIDaccordingly and push these changes up to the web server.
- Integration with ASP.NET 2.0's Membership system - the polling system uses the Membership system to identify the currently logged on user. For more on the Membership system, read Examining ASP.NET 2.0's Membership, Roles, and Profile.
- User Control Deployment - I created the polling control as a User Control in order to simplify deployment and use. The code available for download at the end of this article includes detailed installation and deployment instructions, but in short adding the polling control to a new website involves adding the poll-related tables to the database and the polling User Control to the website. To display a poll in a web page, just drag the polling User Control from the Solution Explorer onto the Design surface in Visual Studio. From there you can declaratively or programmatically specify what poll to display. That's all there is to it!
- Web-based administration - in addition to the polling interface, the download at the end of the article includes a series of optional ASP.NET web pages that serve as a web-based administration section. These pages include an interface to add new polls, edit and delete existing polls, and to view the current or past polls' results.
Examining the Polling Data Model
The poll, its possible answers, and the users' responses are stored in three tables in a SQL Server database. The schema for these three tables follows:
|An auto-increment, primary key.|
|The poll's text (i.e., "What is your favorite programming language?").|
|An auto-increment, primary key.|
|A foreign key tying the answer to a poll.|
|The answer text (i.e., "Visual Basic", "C#", "Pascal", and so on).|
|Indicates the sorting of poll answers. Lower |
|A foreign key linking the response to a particular poll answer. |
|A foreign key linking the response to a particular user. |
Polls table houses a row for each poll in the system. It's
DisplayText field captures the
poll's question (i.e., "What is your favorite programming language?"). The
PollAnswers table has a record for
each unique poll answer (i.e., "Visual Basic", "C#", "Pascal", an so forth). Each answer is related back to a poll via
The answers for a poll are sorted by the
SortOrder column in ascending order. Lastly, the
table serves as a mapping between poll answers and users. It associated a particular user with a particular answer. This
mapping allows us to determine if a particular user has taken a particular poll and can also be used to examine a specific
user's voting history. No secret ballots here!
The complete polling User Control and associated web pages are available for download at the end of this article. You are invited to download and try out the demo application. In the next installment of this article we'll get down to implementing the polling User Control and in the third and final installment we'll examine the associated administration pages.
Until then... Happy Programming!