8/12/2019 Votingcontrol.net
1/10
This application creates a custom voting control for ASP.NET Web applications. The objective is to
encapsulate the entire functionality of voting or polling procedure inside a custom user-defined control
so that it can be reutilized across different Web applications with ease. The various steps involved in the
creation of the voting control application are:
1. Creating the Web site
2. Creating the Web user control (.ascx file)
3. Registering the control in the Web.Config file
4. Using the custom control in Default.aspx Web page
5. Running the Web site
CREATING THE WEB SITE
To create the Web site, you need to perform the following steps:
1. Open Microsoft Visual Studio and selectFileNew Web Siteto display the New Web Site
dialog box, as shown on next page:
C
Major Project:Voting Control for Asp.Net
Appendix
8/12/2019 Votingcontrol.net
2/10
464 Programming in C#
The New Web Site Dialog Box
2. Select the ASP.NET Web Siteoption under the Templatessection and enter the name of the
Web site (say VotingApp) next to the Locationfield.
3. Click OKto create the Web site. The Default.aspxpage opens with the default code, as shown
below:
The Default.aspx Page
8/12/2019 Votingcontrol.net
3/10
Appendix C: Major Project: Voting Control for Asp.Net 465
Creating the Web user control (.ascx file)
Custom Web user controls in ASP.NET are created in .ascx files and not .aspx. To create the custom
voting control, you need to perform the following steps:
1. Right-click the name of the Web site in the Solution Explorerand select the AddNew Item
option from the shortcut menu. The Add New Itemdialog box appears, as shown below:
The Add New Item Dialog Box
2. Select the Web User Controloption under the Templatessection and click Add to add a new
Web user control to the Web site, as shown below:
The WebUserControl.ascx file with Default Code
8/12/2019 Votingcontrol.net
4/10
466 Programming in C#
Now, we need to add the voting or polling functionality to the newly created Web user control. Switch
to the Designmode and add the voting related controls, as shown below:
Design View of WebUserControl.ascx
Well understand the significance of each of the controls present in the WebUserControl.ascxfile
later as we continue to develop this application. The following is the source code contained in the
WebUserControl.ascxfile:
WebUserControl.ascx
8/12/2019 Votingcontrol.net
5/10
Appendix C: Major Project: Voting Control for Asp.Net 467
As shown in the above code, there are two buttons in our custom voting control; one for submitting
the vote while the other for viewing the voting results. To provide relevant functionality to these button
controls, we must add the corresponding C# code in their code-behind files. The following is the code
for the WebUserControl.ascx.csfile:
using System;
using System.IO;
using System.Net;using System.Data;
using System.Configuration;using System.Collections;using System.Web;
using System.Web.Security;using System.Web.UI;
using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;using System.Drawing;
public partial class WebUserControl : System.Web.UI.UserControl
{ protected void Page_Load(object sender, EventArgs e) {
Label2.Visible = false; Label3.Visible = false;
Label4.Visible = false; }
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) {
} protected void Button1_Click(object sender, EventArgs e) {
if (Request.Cookies[State] == null) {
foreach (ListItem item in RadioButtonList1.Items) {
if (item.Selected == true) {
FileStream fs1 = new FileStream(d:\\VotingApp\\Result.txt, FileMode.Append, FileAccess.Write); StreamWriter sw1 = new StreamWriter(fs1);
sw1.WriteLine(item.Value); sw1.Flush(); sw1.Close();
fs1.Close();
HttpCookie HC = new HttpCookie(State); HC.Values[State] = Set;
HC.Expires = DateTime.Now.AddDays(2); Response.Cookies.Add(HC);
Label4.Visible = true;
Label4.ForeColor = Color.Green;
8/12/2019 Votingcontrol.net
6/10
468 Programming in C#
Label4.Text = You have voted successfully!!;
}
}
}
else
{
Label4.Visible = true;
Label4.ForeColor = Color.Red;
Label4.Text = You seem to have already voted! A voter can vote only once!!;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
int yes = 0;
int no = 0;
FileStream fs2 = new FileStream(d:\\VotingApp\\Result.txt, FileMode.Open, FileAccess.Read);
StreamReader sr2 = new StreamReader(fs2); sr2.BaseStream.Seek(0, SeekOrigin.Begin);
string str = sr2.ReadLine();
while (str != null)
{
if (str == yes)
{
yes = yes + 1;
}
if (str == no)
{
no = no + 1;
} str = sr2.ReadLine();
}
sr2.Close();
fs2.Close();
float a = (float)yes / (yes + no) * 100;
int aresult = (int)a;
int bresult = 100 - aresult;
Label2.Visible = true;
Label2.Text = Convert.ToString(aresult) + % Yes votes;
Label3.Visible = true;
Label3.Text = Convert.ToString(bresult) + % No votes;
}}
WebUserControl.ascx.cs
In the above code, the FileStream and StreamWriter type objects are used to store the voting results
submitted by the Web user at d:\\VotingApp\\Result.txt location. The same file is later referenced
using FileStream and StreamWriter type objects to generate the overall polling results. The above code
also makes use of the HttpCookie class to add cookie support to the Web site in order to prevent users
from posting duplicate votes.
8/12/2019 Votingcontrol.net
7/10
Appendix C: Major Project: Voting Control for Asp.Net 469
Note: The WebUserControl.ascx file must be stored inside a subdirectory of the main Web site
directory; otherwise the compiler might generate an error at the time of building the Web site.
Registering the control in the Web.Config file
To enable the Web forms to make use of the custom control, it is required to register the control in theWeb.Configfile. To do so, open the Web.Configfile and add the following code inside it:
The above code specifies a tag prefix, tag name and the source location for the custom voting
control.
Using the custom control in the Default.aspx Web page
Now that the custom voting control is created and registered, it is time to use it in the Default.aspxpageof the Web site. Add the following code in the Default.aspxpage:
Custom Control Demo Web Site
Default.aspx
In the above code, the following statement uses the custom voting control to add voting functionality
to the Web site:
8/12/2019 Votingcontrol.net
8/10
470 Programming in C#
The following figure shows the design view of the Default.aspxpage:
Design View of Default.aspx
Running the Web site
PressF5to start building the Web site; the Default.aspxWeb page appears, as shown below:
The Default.aspx Web page
8/12/2019 Votingcontrol.net
9/10
Appendix C: Major Project: Voting Control for Asp.Net 471
Now, select a voting option and press the Votebutton to register your vote. A voting confirmation
message appears, as shown below:
The Voting Confirmation Message
You can click the Click Here to button to view the voting results, as shown below:
Voting Results
8/12/2019 Votingcontrol.net
10/10
472 Programming in C#
Now, let us try to cast another vote. Since, we had added cookie support to our Web site; it will
prevent us from casting multiple votes, as shown below:
Testing the Cookie Feature