Click here to Skip to main content
11,704,315 members (64,428 online)
Click here to Skip to main content

ASP.Net Poll Server Control

, 10 Jun 2006 108.2K 4.5K 61
Rate this:
Please Sign up or sign in to vote.
A custom server control for polling your site users.
<!---------------------------------------------------------------------------><!------------------------------- STEP 1 --------------------------->
Title:       Site Poll Control
Author:      Michael Eccleston
Environment: VB.Net, ASP.Net
Keywords:    Control, Database, Polls, Triggers, Stored Procedures
Level:       Intermediate
Description: ASP.Net Server Control
Section      ASP.Net
SubSection   Controls
<!------------------------------- STEP 2 ---------------------------><!-- Include download and sample image information. -->

Sample Image - maximum width is 600 pixels

<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (


etc) -->


A recent project I worked on required the ability to present site visitors with a poll question. In an effort to learn more about custom server controls that inherit from a CompositeControl, I generated a custom server control to provide this functionality. The effort to create this control was fairly simple and I am providing the control to anybody else that may find it helpful in their projects.

The Poll Control allows you to place the control in your webform, set a few properties and add the available answers and current answer count to the PollAnswers collection and you get the above results on your page. Add the PollControl.dll to your toolbox and then place the control on your form. The look of the control is controlled by CSS stylesheet classes.

The control was designed with a Sql Server Express database as the back end data store. The database consists of three tables, PollQuestions, PollAnswers and PollResults. There are triggers on insert and delete of the PollResults table that maintain the vote results automatically. There is also a stored procedure for inserting or deleting poll results. Aside from the initial setup of the control to display it on your form, all you need to provide is a call to the InsertResult stored procedure in the CastVote event.

If you are interested in learning about triggers and stored procedures in a SQL Server Express database, the code source file may provide you with some learning material. The information is fairly easy to understand and may provide you with a starting point to your own triggers and stored procedures. I have provided all of the database scripts so that you can duplicate the database, and I have provided the source code to call the stored procedures.

Using the control

CanVote If True the question and answers are presented in the Vote Mode with a Cast Vote button.
If False the results are displayed as shown above.
ShowPrecentages If True in the results show the vote percentages.
If False in the results show the vote count.
PollQuestion The poll question to present the visitor.
CSSCaption The stylesheet class to use for the PollControl Caption and Question.
CSSPollClass The stylesheet class to use for the PollControl.
ImagesFolder The folder location of the images to use for the result bars.
VoteCaption The text that you want to proceed the vote count.
AddPollAnswer() AddPollAnswer( PollId, AnswerId, AnswerText, AnswerCount )
Event: The CastVote event is raised by clicking on the Cast Vote button when the control is in Vote View
CastVote() CastVote( PollId, AnswerId, MemberId )
Sample code in the code behind is shown below.


// The control above was populated with the code shown below.

If Not Page.IsPostBack then
    With PollControl1
        .CanVote = False
        .ShowPercentage = True
        .PollQuestion = "What is your poll question ?"
        .CSSCaption = "PollCaption"
        .CSSPollClass = "PollBack"
        .ImagesFolder = "Images/"
        .VoteCaption = "Votes Cast :"

        ' Loop to populate the PollAnswer Collection

        .AddPollAnswer( PollId, AnswerId, AnswerText, AnswerCount )


    End With
End If

Points of Interest

The design of this control provided a learning experience with custom server controls and SQL Server Express triggers and stored procedures.


<!------------------------------- That's it! --------------------------->


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Mike Eccleston
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questionhide count vote in header Pin
b.paseban8-May-13 23:18
memberb.paseban8-May-13 23:18 
GeneralMy vote of 1 Pin
eduveliz261213-Jun-12 8:00
membereduveliz261213-Jun-12 8:00 
QuestionVersion with database... Pin
Engineer Bill1-Feb-12 10:23
memberEngineer Bill1-Feb-12 10:23 
QuestionDatabase Driven example in C# Pin
Member 840690622-Dec-11 18:39
memberMember 840690622-Dec-11 18:39 
Questionhelp!!! Pin
Member 849649517-Dec-11 2:15
memberMember 849649517-Dec-11 2:15 
GeneralMy vote of 1 Pin
ankitasingh24515-Apr-11 5:25
memberankitasingh24515-Apr-11 5:25 
GeneralMy vote of 5 Pin
Thanh2088-Jan-11 4:10
memberThanh2088-Jan-11 4:10 
QuestionHow to use database Pin
rizwan18-Aug-10 23:42
memberrizwan18-Aug-10 23:42 
GeneralNo download Link !!!!11 Pin
ab_dc31-Aug-09 0:54
memberab_dc31-Aug-09 0:54 
GeneralRe: No download Link !!!!11 Pin
Mike Eccleston31-Aug-09 9:13
memberMike Eccleston31-Aug-09 9:13 
GeneralRe: No download Link !!!!11 Pin
ab_dc2-Sep-09 8:47
memberab_dc2-Sep-09 8:47 
QuestionHow to run using a sql database?? Pin
rubble259-Jul-09 0:19
memberrubble259-Jul-09 0:19 
AnswerRe: How to run using a sql database?? Pin
Mike Eccleston2-Sep-09 9:25
memberMike Eccleston2-Sep-09 9:25 
GeneralMy vote of 2 Pin
vuductuan34@gmail.com8-Jun-09 18:16
membervuductuan34@gmail.com8-Jun-09 18:16 
GeneralGood Work Pin
dhaval.upadhyay18-Jan-09 18:44
memberdhaval.upadhyay18-Jan-09 18:44 
GeneralA Bug In Your Project Pin
Member 24842919-Jan-09 3:24
memberMember 24842919-Jan-09 3:24 
GeneralRe: A Bug In Your Project Pin
zaxbowow23-Dec-13 16:55
memberzaxbowow23-Dec-13 16:55 
QuestionAccessing Functions Pin
slap_maxwell24-Nov-08 8:40
memberslap_maxwell24-Nov-08 8:40 
GeneralDisplaying data from SQL Pin
Member 34990529-Jan-08 0:11
memberMember 34990529-Jan-08 0:11 
Questioncan you help me!!! Pin
Member 40468028-Jan-08 23:37
memberMember 40468028-Jan-08 23:37 
Generalthanks for your wonderful work.. Pin
Member 40468028-Jan-08 23:35
memberMember 40468028-Jan-08 23:35 
Generaldatabase driven Pin
yuori31-Dec-07 1:38
memberyuori31-Dec-07 1:38 
QuestionHow to get selected answer from this control... Pin
NothingIsPermanent29-Jun-07 1:34
memberNothingIsPermanent29-Jun-07 1:34 
AnswerRe: How to get selected answer from this control... Pin
Mike Eccleston29-Jun-07 2:45
memberMike Eccleston29-Jun-07 2:45 
GeneralRe: How to get selected answer from this control... Pin
NothingIsPermanent29-Jun-07 20:00
memberNothingIsPermanent29-Jun-07 20:00 
Generaldatabase example Pin
VinhV6-Jun-07 7:55
memberVinhV6-Jun-07 7:55 
QuestionDatabase Driven Example Pin
Azzabraham Lincoln13-Feb-07 11:58
memberAzzabraham Lincoln13-Feb-07 11:58 
AnswerRe: Database Driven Example Pin
Mike Eccleston13-Feb-07 14:41
memberMike Eccleston13-Feb-07 14:41 
GeneralRe: Database Driven Example Pin
mxcmxc19-Apr-07 18:53
membermxcmxc19-Apr-07 18:53 
GeneralRe: Database Driven Example Pin
mxcmxc23-Apr-07 21:37
membermxcmxc23-Apr-07 21:37 
GeneralRe: Database Driven Example Pin
mxcmxc13-May-07 21:47
membermxcmxc13-May-07 21:47 
GeneralRe: Database Driven Example Pin
rosastar24-Mar-08 20:27
memberrosastar24-Mar-08 20:27 
GeneralRe: Database Driven Example Pin
s4sadiatabassum10-Nov-09 20:50
members4sadiatabassum10-Nov-09 20:50 
QuestionRe: Database Driven Example Pin
sibercyberia15-May-07 21:20
membersibercyberia15-May-07 21:20 
AnswerRe: Database Driven Example Pin
dschips16-May-07 8:04
memberdschips16-May-07 8:04 
GeneralRe: Database Driven Example Pin
javivarela5-Jun-11 0:21
memberjavivarela5-Jun-11 0:21 
GeneralCloseType [modified] Pin
ihles9-Feb-07 4:46
memberihles9-Feb-07 4:46 
GeneralRe: CloseType Pin
Mike Eccleston9-Feb-07 5:39
memberMike Eccleston9-Feb-07 5:39 
GeneralRe: CloseType Pin
ihles9-Feb-07 7:56
memberihles9-Feb-07 7:56 
QuestionLinking the poll to a different back-end Pin
atomiton4-Feb-07 6:11
memberatomiton4-Feb-07 6:11 
AnswerRe: Linking the poll to a different back-end Pin
Mike Eccleston4-Feb-07 6:46
memberMike Eccleston4-Feb-07 6:46 
GeneralRe: Linking the poll to a different back-end Pin
atomiton4-Feb-07 14:57
memberatomiton4-Feb-07 14:57 
GeneralSQL Script Pin
Geert van Horrik4-Jul-06 23:12
memberGeert van Horrik4-Jul-06 23:12 
GeneralRe: SQL Script Pin
Mike Eccleston6-Jul-06 2:46
memberMike Eccleston6-Jul-06 2:46 
GeneralRe: SQL Script Pin
cmug11-Mar-07 16:47
membercmug11-Mar-07 16:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 10 Jun 2006
Article Copyright 2006 by Mike Eccleston
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid