Click here to Skip to main content
Licence 
First Posted 10 Jun 2006
Views 75,151
Bookmarked 60 times

ASP.Net Poll Server Control

By | 10 Jun 2006 | Article
A custom server control for polling your site users.
Title:       Site Poll Control
Author:      Michael Eccleston
Email:       mike@ecclestonsoftware.com
Environment: VB.Net, ASP.Net
Keywords:    Control, Database, Polls, Triggers, Stored Procedures
Level:       Intermediate
Description: ASP.Net Server Control
Section      ASP.Net
SubSection   Controls

Sample Image - maximum width is 600 pixels

Introduction

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

Properties:
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.
Method:
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.

SAMPLE CODE

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

History

License

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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionVersion with database... PinmemberEngineer Bill10:23 1 Feb '12  
QuestionDatabase Driven example in C# PinmemberMember 840690618:39 22 Dec '11  
Questionhelp!!! PinmemberMember 84964952:15 17 Dec '11  
GeneralMy vote of 1 Pinmemberankitasingh2455:25 15 Apr '11  
GeneralMy vote of 5 PinmemberThanh2084:10 8 Jan '11  
QuestionHow to use database Pinmemberrizwan123:42 8 Aug '10  
GeneralNo download Link !!!!11 Pinmemberab_dc0:54 31 Aug '09  
GeneralRe: No download Link !!!!11 PinmemberMike Eccleston9:13 31 Aug '09  
GeneralRe: No download Link !!!!11 Pinmemberab_dc8:47 2 Sep '09  
QuestionHow to run using a sql database?? Pinmemberrubble250:19 9 Jul '09  
AnswerRe: How to run using a sql database?? PinmemberMike Eccleston9:25 2 Sep '09  
GeneralMy vote of 2 Pinmembervuductuan34@gmail.com18:16 8 Jun '09  
GeneralGood Work Pinmemberdhaval.upadhyay18:44 18 Jan '09  
GeneralA Bug In Your Project PinmemberMember 24842913:24 9 Jan '09  
QuestionAccessing Functions Pinmemberslap_maxwell8:40 24 Nov '08  
GeneralDisplaying data from SQL PinmemberMember 34990520:11 9 Jan '08  
Questioncan you help me!!! PinmemberMember 404680223:37 8 Jan '08  
Generalthanks for your wonderful work.. PinmemberMember 404680223:35 8 Jan '08  
Generaldatabase driven Pinmemberyuori1:38 31 Dec '07  
QuestionHow to get selected answer from this control... PinmemberNothingIsPermanent1:34 29 Jun '07  
AnswerRe: How to get selected answer from this control... PinmemberMike Eccleston2:45 29 Jun '07  
GeneralRe: How to get selected answer from this control... PinmemberNothingIsPermanent20:00 29 Jun '07  
Generaldatabase example PinmemberVinhV7:55 6 Jun '07  
QuestionDatabase Driven Example PinmemberAzzabraham Lincoln11:58 13 Feb '07  
AnswerRe: Database Driven Example PinmemberMike Eccleston14:41 13 Feb '07  

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.

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