Sie sind auf Seite 1von 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-dened 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 le)
3. Registering the control in the Web.Cong le
4. Using the custom control in Default.aspx Web page
5. Running the Web site
To create the Web site, you need to perform the following steps:
1. Open Microsoft Visual Studio and select File New Web Site to display the New Web Site
dialog box, as shown on next page:
Major Project:
Voting Control for Asp.Net
464 Programming in C#
The New Web Site Dialog Box
2. Select the ASP.NET Web Site option under the Templates section and enter the name of the
Web site (say VotingApp) next to the Location eld.
3. Click OK to create the Web site. The Default.aspx page opens with the default code, as shown
The Default.aspx Page
Appendix C: Major Project: Voting Control for Asp.Net 465
Creating the Web user control (.ascx le)
Custom Web user controls in ASP.NET are created in .ascx les 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 Explorer and select the Add New Item
option from the shortcut menu. The Add New Item dialog box appears, as shown below:
The Add New Item Dialog Box
2. Select the Web User Control option under the Templates section and click Add to add a new
Web user control to the Web site, as shown below:
The WebUserControl.ascx le with Default Code
466 Programming in C#
Now, we need to add the voting or polling functionality to the newly created Web user control. Switch
to the Design mode and add the voting related controls, as shown below:
Design View of WebUserControl.ascx
Well understand the signicance of each of the controls present in the WebUserControl.ascx le
later as we continue to develop this application. The following is the source code contained in the
WebUserControl.ascx le:
<%@ Control Language=C# AutoEventWireup=true
CodeFile=WebUserControl.ascx.cs Inherits=WebUserControl %>
<asp:Label ID=Label1 runat=server Font-Size=Larger Text=Do you like this
Web site?></asp:Label>&nbsp;<br />
<asp:RadioButtonList ID=RadioButtonList1 runat=server
<asp:ListItem Selected=true Text=Yes
<asp:ListItem Selected=false Text= No
<asp:Button ID=Button1 runat=server OnClick=Button1_Click Text=Vote />
<asp:Label ID=Label4 runat=server ForeColor=Black Text=Label
Width=559px></asp:Label><br />
<br />
<br />
<br />
<asp:Button ID=Button2 runat=server OnClick=Button2_Click Text=Click Here to View the Poll
Results!! /><br />
<br />
<br />
<asp:Label ID=Label2 runat=server Text=Label></asp:Label><br />
<asp:Label ID=Label3 runat=server Text=Label></asp:Label>
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 les. The following is the code
for the WebUserControl.ascx.cs le:
using System;
using System.IO;
using System.Net;
using System.Data;
using System.Conguration;
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.
StreamWriter sw1 = new StreamWriter(fs1);
HttpCookie HC = new HttpCookie(State);
HC.Values[State] = Set;
HC.Expires = DateTime.Now.AddDays(2);
Label4.Visible = true;
Label4.ForeColor = Color.Green;
468 Programming in C#
Label4.Text = You have voted successfully!!;
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();
oat a = (oat)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;
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 le 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.
Appendix C: Major Project: Voting Control for Asp.Net 469
Note: The WebUserControl.ascx le 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.Cong le
To enable the Web forms to make use of the custom control, it is required to register the control in the
Web.Cong le. To do so, open the Web.Cong le and add the following code inside it:
<add tagPrex=cc tagName=Custom src=~/VotingControl/WebUserControl.ascx/>
The above code species a tag prex, tag name and the source location for the custom voting
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.aspx page
of the Web site. Add the following code in the Default.aspx page:

<%@ Page Language=C#
AutoEventWireup=true CodeFile=Default.aspx.cs Inherits=_Default %>
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
<html xmlns= >
<head id=Head1 runat=server>
<title>Custom Control Demo Web Site</title>
<form id=form1 runat=server>
<asp:Label ID=Label1 runat=server Font-Bold=True Font-Size=XX-Large
Text=Voting Control Application Width=409px></asp:Label><br />
<br />
<cc:Custom ID=ID_C runat=server />
In the above code, the following statement uses the custom voting control to add voting functionality
to the Web site:
<cc:Custom ID=ID_C runat=server />
470 Programming in C#
The following gure shows the design view of the Default.aspx page:
Design View of Default.aspx
Running the Web site
Press F5 to start building the Web site; the Default.aspx Web page appears, as shown below:
The Default.aspx Web page
Appendix C: Major Project: Voting Control for Asp.Net 471
Now, select a voting option and press the Vote button to register your vote. A voting conrmation
message appears, as shown below:
The Voting Conrmation Message
You can click the Click Here to button to view the voting results, as shown below:
Voting Results
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