This tutorial will show you how to create an online poll that all visitors can partake in, and view results in # of votes and percentage format. Written in VB.NET
Poll systems can be very useful for gathering a large amount of data quickly. They usually require no registration, and are mainly used to gather such information as opinions or trends. Visitors to your website are usually willing to take part in Polls because they usually do not require any personal information, and people are interested in other's views.
In this tutorial, you will learn how to create an online voting system where anyone can vote, and we can see the results of the poll - including a percentage of the answers.
The system will use XML for storage of votes, and AJAX to enable instant results. We will start by creating the structure of the XML file:
<?xml version="1.0" encoding="utf-8"?> <Poll>
<Vote>
<Name>Paul</Name> <Choice>Obama</Choice> </Vote> <Vote>
<Name>Mike</Name> <Choice>McCain</Choice> </Vote> </Poll> |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
Next, we can start on the ASPX page. Add in the ScriptManager and UpdatePanel tags:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
</ContentTemplate> </asp:UpdatePanel> </form> |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
Now we can build our Poll form inside the UpdatePanel, so that AJAX can reload it without performing a full PostBack.
We will be using a TextBox, RadioButtons, Buttons, and a Label and Literal control. Our ASPX page will look something like this:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Literal ID="litResults" runat="server" visible="false"/> What is your name? <asp:TextBox ID="txtName" runat="server" /><br /> Who is your favorite Candidate?<br /> <asp:RadioButtonList ID="radVote" runat="server">
<asp:ListItem>Obama</asp:ListItem> <asp:ListItem>McCain</asp:ListItem> </asp:RadioButtonList> <asp:Button ID="butVote" runat="server" Text="Vote"
onclick="butVote_Click" /><br /> <asp:Label ID="lblStatus" runat="server" /><br /> <asp:Button ID="butResults" runat="server" Text="Show Results"
onclick="butResults_Click" /> </ContentTemplate> </asp:UpdatePanel> </form> |
Notice we have two buttons on our page - one to submit the vote, and the other to show the current results.
First, we will code the submit button. We want this button to check that a name has been entered and that an option has been selected:
Protected Sub butVote_Click(ByVal sender As Object, ByVal e As EventArgs)
If txtName.Text = "" Then
lblStatus.Text = "Please enter your name." ElseIf radVote.SelectedItem Is Nothing Then
lblStatus.Text = "Please vote." Else
countVote(radVote.SelectedItem.ToString()) End If End Sub |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
The submit button doesn't perform anything but validation, we then call a method to submit the vote to the XML file:
Protected Sub countVote(ByVal theVote As String)
Try
Dim xmlDoc As XDocument = XDocument.Load(Server.MapPath("Poll.xml"))
xmlDoc.Element("Poll").Add(New XElement("Vote", New XElement("Name", txtName.Text), New XElement("Choice", theVote)))
xmlDoc.Save(Server.MapPath("Poll.xml")) lblStatus.Text = "Thank you for your vote." readXML() Catch
lblStatus.Text = "Sorry, unable to process request. Please try again." End Try End Sub |
This method uses LINQ to first open the XML file and then add a new element to it with the values the user submitted. We then save the updated XML file and output the current results to the page, by calling the readXML method:
Protected Sub readXML()
Dim xmlDoc As XDocument = XDocument.Load(Server.MapPath("Poll.xml")) Dim votes = From vote In xmlDoc.Descendants("Vote") _
Select Name = vote.Element("Name").Value, Vote = vote.Element("Choice").Value Dim mCount As Integer = 0 Dim oCount As Integer = 0 For Each vote In votes
If vote.Vote = "McCain" Then
mCount += 1 ElseIf vote.Vote = "Obama" Then
oCount += 1 End If Next vote Dim theTotal As Double = mCount + oCount Dim mPercent As Double = (mCount / theTotal) * 100 Dim oPercent As Double = (oCount / theTotal) * 100 litResults.Visible = True litResults.Text = "Obama: " & oCount & " votes (" & oPercent & "%).<br />" litResults.Text = litResults.Text & "McCain: " & mCount & " votes (" & mPercent & "%).<br /><br />" End Sub |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
Again, this method makes use of LINQ to open the XML file and get all data within. It then loops through all the data and counts the number of votes for each candidate, then performs a simple calculation to determine the percentage for each. Finally, we output the results to the web page, using the Literal control.
We also code the show results button to call the readXML method:
| Protected Sub butResults_Click(ByVal sender As Object, ByVal e As EventArgs)
readXML() End Sub |
The entire code-behind should like this:
Partial Class _Default
Inherits System.Web.UI.Page Protected Sub butVote_Click(ByVal sender As Object, ByVal e As EventArgs)
If txtName.Text = "" Then
lblStatus.Text = "Please enter your name." ElseIf radVote.SelectedItem Is Nothing Then
lblStatus.Text = "Please vote." Else
countVote(radVote.SelectedItem.ToString()) End If End Sub Protected Sub countVote(ByVal theVote As String)
Try
Dim xmlDoc As XDocument = XDocument.Load(Server.MapPath("Poll.xml"))
xmlDoc.Element("Poll").Add(New XElement("Vote", New XElement("Name", txtName.Text), New XElement("Choice", theVote)))
xmlDoc.Save(Server.MapPath("Poll.xml")) lblStatus.Text = "Thank you for your vote." readXML() Catch
lblStatus.Text = "Sorry, unable to process request. Please try again." End Try End Sub Protected Sub readXML()
Dim xmlDoc As XDocument = XDocument.Load(Server.MapPath("Poll.xml")) Dim votes = From vote In xmlDoc.Descendants("Vote") _
Select Name = vote.Element("Name").Value, Vote = vote.Element("Choice").Value Dim mCount As Integer = 0 Dim oCount As Integer = 0 For Each vote In votes
If vote.Vote = "McCain" Then
mCount += 1 ElseIf vote.Vote = "Obama" Then
oCount += 1 End If Next vote Dim theTotal As Double = mCount + oCount Dim mPercent As Double = (mCount / theTotal) * 100 Dim oPercent As Double = (oCount / theTotal) * 100 litResults.Visible = True litResults.Text = "Obama: " & oCount & " votes (" & oPercent & "%).<br />" litResults.Text = litResults.Text & "McCain: " & mCount & " votes (" & mPercent & "%).<br /><br />" End Sub Protected Sub butResults_Click(ByVal sender As Object, ByVal e As EventArgs)
readXML() End Sub End Class |
Looking for more ASP.NET Tutorials? Click Here!