To read the article online, visit

Creating an ASP.NET 2.0 Polling User Control: Design Decisions and the Data Model

By 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.

Design Decisions

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_Data folder. 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 Web.config accordingly.
  • 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 PollID property. 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 PollID accordingly and push these changes up to the web server.
  • Voting limited to authenticated users - most website polls allow any visitor to cast a vote and then use cookies or some other technique to reduce ballot stuffing. I say "reduce" instead of "prevent" because if anonymous visitors are allowed to vote then it is possible to delete cookies and revote or simply go to a different computer a revote. To combat this I decided to limit my polling control such that only authenticated users could vote. A user's vote is recorded in a database table associating the user with the poll and poll answer selected. This makes it easy to ensure that a single user does not vote multiple times on one poll. Also, it allows for a user to review their voting history, or for an administrator to examine how frequently a particular users votes or to run other user-specific reports. (Note: No such user-specific reports are included in the administration pages, but the data is there and you are welcome to create your own report pages.)
  • 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.
While these design goals molded the polling control, every effort was made to make the code and infrasturcture flexible so as to allow these design goals to be modified. For example, in the polling User Control's code-behind class there is a method that determines whether a visitor can take the poll. I have implemented this so that it only allows authenticated users who have not yet taken the poll in question to submit their vote; however, you could tweak this logic to allow any visitor to take the poll. By making such a change, you would want to use a cookie or Session variables or some measure to reduce ballot stuffing, but the point is that such modifications are possible. (Note: These extension points are identified in the source code by comments.)

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:

ColumnData TypeNotes
PollID   Primary KeyintAn auto-increment, primary key.
DisplayTextnvarchar(500)The poll's text (i.e., "What is your favorite programming language?").

ColumnData TypeNotes
PollAnswerID   Primary KeyintAn auto-increment, primary key.
PollIDintA foreign key tying the answer to a poll.
DisplayTextnvarchar(500)The answer text (i.e., "Visual Basic", "C#", "Pascal", and so on).
SortOrderintIndicates the sorting of poll answers. Lower SortOrder values answer appear before higher ones.

ColumnData TypeNotes
PollAnswerID   Primary KeyintA foreign key linking the response to a particular poll answer. UserID and PollAnswerID form a composite primary key.
UserID   Primary KeyuniqueidentifierA foreign key linking the response to a particular user. UserID and PollAnswerID form a composite primary key.

The 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 PollID. The answers for a poll are sorted by the SortOrder column in ascending order. Lastly, the UserResponses 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!

Looking Forward...

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!

  • By Scott Mitchell


  • Download the Polling User Control and demo application (ZIP format)
  • Article Information
    Article Title: ASP.NET.Creating an ASP.NET 2.0 Polling User Control: Design Decisions and the Data Model
    Article Author: Scott Mitchell
    Published Date: August 22, 2007
    Article URL:

    Copyright 2018 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers