Asp.net| Design | voting | network | online
system function design and database design
1. system function design and database design
1.1 System Function Design
Network online voting system to achieve a simple function, as follows:
The management of the voting project;
Add a voting item;
Deletion of voting items;
(a) vote on the project;
View the voting status of the item.
1.2 Database Design
The database design of this system is relatively simple, only need to store voting information. Create a database in SQL Server 2000 with the name "Webvotedb" and create a polling table votes in the database. Where the Voteid field stores the voting item ID, the item field stores the name of the voting item, and the Votecount field stores the number of votes per item. Create a voting item table the votes of the action interface is shown in Figure 1.
Voting items table votes needs to store the name of the voting item and its number of votes, and the table's field descriptions are shown in table 1.
Figure 1 Creating a voting item table votes's operating interface
Table 1 votes table
Field name
|
Data type
|
Field description
|
Key reference
|
Notes
|
Treeid |
Int |
Vote Item ID |
Pk |
Primary KEY (auto add one) |
Item |
VARCHAR (200) |
The name of the voting item
|
|
|
Votecount
|
Int
|
Number of votes
|
|
|
The online voting function is one of the most common functions of web application, and it is also a common functional module for Web application development. When an administrator or user of a Web site brings up new ideas and suggestions or a new product, they may need to determine whether the new ideas, suggestions, or new products meet the needs of the user or customer through a user or customer vote, and The website also can do some actual investigation work through the website online voting function. The online polling system introduced in this chapter also displays voting information in an intuitive graphical interface, and can also be viewed in a timely manner.
II. Implementation of the voting system
After creating the database required by the system, the implementation of the online voting system can be divided into the following 3 parts:
(1) The realization part of the stored procedure;
(2) The realization part of the database access layer;
(3) The implementation part of the function page.
The detailed implementation of the above 3 sections is described in detail below. First, create a Web site in Microsoft Visual Studio. NET 2005, named "Webvote."
2.1 Stored Procedure Design
Create stored Procedures pr_getvotes, Pr_getsinglevote, Pr_addvote, Pr_updatevote, and Pr_deletevote in the database webvotedb. which
Pr_getvotes to obtain information on all voting items from the voting item table votes;
Pr_getsinglevote to obtain information about a voting item from the Voting item table votes;
Pr_addvote adds a new record to the voting item table votes;
Pr_updatevote to update the number of votes in the voting project;
Pr_deletevote obtains the deletion of a voting item from the Voting item table votes.
The procedure code for each of the above stored procedures is as follows:
/* Stored Procedures pr_getvotes * *
CREATE PROCEDURE Pr_getvotes As SELECT * from votes ORDER by Voteid
/* Stored Procedures pr_getsinglevote * *
CREATE PROCEDURE Pr_getsinglevote (@VoteID int) As SELECT votes.* from votes WHERE Voteid = @VoteID
/* Stored Procedures pr_addvote * *
CREATE PROCEDURE pr_addvote (@Item varchar (100)) As INSERT into votes (item,itemcount) VALUES (@Item, 0) return @ @Identity
/* Stored Procedures pr_updatevote * *
CREATE PROCEDURE pr_updatevote (@VoteID int) As UPDATE votes SET votecount = votecount + 1 WHERE Voteid = @VoteID
/* Stored Procedures pr_deletevote * *
CREATE PROCEDURE pr_deletevote (@VoteID int) As DELETE votes WHERE Voteid = @VoteID |
2.2 Database Access Layer design
Add a class vote to the votes of the Access polling table in the application webvote that encapsulates the method of selecting, adding, modifying, and deleting records in the table votes of the voting items. which
Method Getvotes () obtains the information of all voting items from the voting item table votes;
Method Addvote (String sitem) adds a new record to the voting item table votes;
Method Updatevote (int nvoteid) to update the number of votes participating in the voting project;
Method Deletevote (int nvoteid) Gets the deletion information from the voting item table votes.
The program code for class vote is as follows:
public class Vote { Public SqlDataReader getvotes () { Define Class SqlHelper Sqlhelper.sqlhelper SQLHelper = new Sqlhelper.sqlhelper (); Define the DataReader that holds the results obtained from the database SqlDataReader dr = null; Try {//Execute stored Procedure Sqlhelper.runproc ("Pr_getvotes", out DR); }
catch (Exception ex) {//Throw execution Database exception Systemerror.createerrorlog (ex. message); throw new Exception (ex. Message, ex); }
Returns the results obtained from the database
return (DR); }
public int addvote (String sitem) {//Definition class SqlHelper Sqlhelper.sqlhelper SQLHelper = new Sqlhelper.sqlhelper (); To create a parameter to access a database Sqlparameter[] Paramlist = { Sqlhelper.createinparam ("@Item", Sqldbtype.varchar,100,sitem) }; Try {//Execute stored Procedure Return (Sqlhelper.runproc ("Pr_addvote", paramlist)); } catch (Exception ex) {//Throw execution Database exception Systemerror.createerrorlog (ex. message); throw new Exception (ex. Message, ex); } }
public void updatevote (int nvoteid) {//Definition class SqlHelper Sqlhelper.sqlhelper SQLHelper = new Sqlhelper.sqlhelper (); To create a parameter to access a database Sqlparameter[] Paramlist = {Sqlhelper.createinparam ("@VoteID", SqlDbType.Int, 4,nvoteid)}; Try {//Execute stored Procedure Sqlhelper.runproc ("Pr_updatevote", paramlist); } catch (Exception ex) {//Throw execution Database exception Systemerror.createerrorlog (ex. message); throw new Exception (ex. Message, ex); } }
public void deletevote (int nvoteid) {//Definition class SqlHelper Sqlhelper.sqlhelper SQLHelper = new Sqlhelper.sqlhelper (); To create a parameter to access a database Sqlparameter[] Paramlist = { Sqlhelper.createinparam ("@VoteID", SqlDbType.Int, 4,nvoteid) }; Try {//Execute stored Procedure Sqlhelper.runproc ("Pr_deletevote", paramlist); } catch (Exception ex) {//Throw execution Database exception Systemerror.createerrorlog (ex. message); throw new Exception (ex. Message, ex); } } } |
System home Page Design
Add a new Web page to the application webvote and name it default.aspx, and its code-behind file is Default.aspx.cs.
Adds 3 hyperlink controls to the page Default.aspx, named Itemmanagelink, Onlinevotelink, Viewvotelink. They implement the jump Vote Project Management page voteitemmanage.aspx, voting page webonlinvote.aspx, voting results page showvoteinfo.aspx. The page Default.aspx design interface is shown in Figure 2.
Figure 2 Design interface for page Default.aspx
The HTML Design code for the page default.aspx is as follows:
runat= "Server" font-bold= "True" Voting project management runat= "Server" font-bold= "True" web online voting runat= "Server" font-bold= "True" to view voting results |
After the online voting system runs, the system default page default.aspx the initialization interface as shown in Figure 3, at which point 3 link buttons are displayed.
Figure 3 Initialization interface for voting page Default.aspx
Voting Project management page Design
Adds a new Web page to the application webvote. and named Voteitemmanage.aspx, its code-behind file is VoteItemManage.aspx.cs file.
1. Page Design
Adds a list control, a button control, a TextBox control, and a ImageButton control to the page voteitemmanage.aspx, whose names are itemlist, ADDBTN, Item and DELETEBTN. Control Itemlist displays all the data in the Voting items table; The control ADDBTN implementation adds a new voting item; Control item is used to enter a new voting item name; Control DELETEBTN Delete a voting item. The page itemmanage.aspx design interface is shown in Figure 4.
Figure 4 page voteitemmanage.aspx Design interface
page voteitemmanage.aspx HTML design code is as follows:
Network online polling system
cssclass= "Selectsta"/> alternatetext= "Delete this item" runat= "Server" Commandname= "Delete"/> Cssclass= "Inputcss" > Cssclass= "Buttoncss" > |
2. Page initialization
The page voteitemmanage.aspx calls the function Page_Load (Object Sender,eventargs e) initialization, which calls the function Bindvotelistdata () gets all the voting items from the database polling table votes , and binds the obtained data to the list control itemlist. The program code for the function Page_Load (Object Sender,eventargs e) and function Bindvotelistdata () is as follows:
private void Page_Load (object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{//binding data for a list of voting items
Bindvotelistdata ();
}
}
private void Bindvotelistdata ()
{//Get all the data for the voting item
Webvote.vote Vote = new Vote ();
SqlDataReader recv = vote. Getvotes ();
Set the Text property and Value property of a list control
Itemlist.datatextfield = "Item";
Itemlist.datavaluefield = "Voteid";
Set the data source for the control and bind the control's data
Itemlist.datasource = recv;
Itemlist.databind ();
Recv. Close (); Turn off data readers
} |
After the online voting system is run, the Voteitemmanage.aspx initialization interface of the voting project management page is shown in Figure 5, and the project information for the vote is displayed.
Figure 5 The initialization interface for the voteitemmanage.aspx of the Voting project management page
3. Add Features
Click the Add new Voting item button in the page voteitemmanage.aspx to trigger the event Addbtn_click (object sender, System.EventArgs e), which implements adding a new voting item. The program code for event Addbtn_click (object sender, System.EventArgs e) is as follows:
private void Addbtn_click (object sender, System.EventArgs e) { if (Item.Text.Length > 0) {//Definition class Webvote.vote Vote = new Vote (); Try {//Add new data item Vote. Addvote (Item.Text.Trim ()); Bindvotelistdata (); Display action Results Information Response.Write (""); }
catch (Exception ex) {//Show failure, error message in Add operation Response.Redirect ("~/desktopmodules/errorpage.aspx?") Errorurl= " + Aspnet2system.redirecterrorurl (Request.rawurl) + "&errormessage=" + ex. Message.replace ("\ n", "")); } } } |
4. Delete Feature
Click the "X" button in the page voteitemmanage.aspx to trigger the event Deletebtn_click (object sender, System.EventArgs e), which implements the deletion of the selected voting item. The program code for event Deletebtn_click (object sender, System.EventArgs e) is as follows:
protected void Deletebtn_click (object sender, ImageClickEventArgs e)
{
if (Itemlist.selectedindex <=-1)
{//Display action result information
Response.Write ("");
Return
}
Defining classes
Webvote.vote Vote = new Vote ();
Try
{//delete data
Vote. Deletevote (Int32.Parse (Itemlist.selectedvalue));
rebind data
Bindvotelistdata ();
}
catch (Exception ex)
{//Show failure, error message in delete operation
Response.Redirect ("~/desktopmodules/errorpage.aspx?") Errorurl= "
+ Aspnet2system.redirecterrorurl (Request.rawurl)
+ "&errormessage=" + ex. Message.replace ("\ n", ""));
}
} |
voting page Design
Add a new Web page to the application webvote and name it webonlinevote.aspx, and its code-behind file is a WebOnlineVote.aspx.cs file.
1. Page Design
Adds a data grid control, two button controls, and a Label control to the page webonlinevote.aspx, which are named Votelist, Votebtn, Showvote, and Votemessage respectively. Control votelist is used to display all the items that participate in the voting; control votebtn the vote of the user; the control showvote the user to view the poll; the control votemessage displays the results of the user's vote. The page webonlinvote.aspx design interface is shown in Figure 6.
Figure 6 Design interface for page webonlinvote.aspx
The HTML Design code for the page webonlinvote.aspx is as follows:
<%@ Page language= "C #" autoeventwireup= "true"
Codefile= "WebOnlinVote.aspx.cs" inherits= "Webonlinvote"%>
<HTML> <HEAD> <title> Network online polling system </title> </HEAD>
<asp:datagrid id= "votelist" cssclass= "Gbtext" runat= "Server"
autogeneratecolumns= "False" datakeyfield= "Voteid" >
<Columns>
<asp:templatecolumn itemstyle-width= "200" >
<ItemTemplate> <%# DataBinder.Eval (Container.DataItem, "Item")%>
</ItemTemplate> </asp:TemplateColumn>
<asp:templatecolumn itemstyle-width= "100" >
<ItemTemplate>
<asp:checkbox id= "Votecheck" runat= "Server" > </asp:CheckBox>
</ItemTemplate> </asp:TemplateColumn>
</Columns>
<footerstyle backcolor= "#FFFFCC" forecolor= "#330099"/>
<selecteditemstyle backcolor= "#FFCC66" font-bold= "True"
Forecolor= "#663399"/>
<pagerstyle backcolor= "#FFFFCC" forecolor= "#330099"
horizontalalign= "Center"/>
<itemstyle backcolor= "White" forecolor= "#330099"
Forecolor= "#FFFFCC"/>
</asp:datagrid>
<asp:button id= "votebtn" runat= "server" width= "100"
text= "I want to vote" > </asp:button>
<asp:button id= "Showvote" runat= "server" width= "100"
text= "View Polls" > </asp:button>
<asp:label id= "Votemessage" runat= "Server" visible= "False"
Forecolor= "Red" font-bold= "True" and voted successfully!!! </asp:Label> </td>
</HTML> |
1. Page initialization
Page Webonlinvote.aspx calls the function Page_Load (Object Sender,eventargs e) initialization, which calls the function Bindvotelistdata () to obtain information about all voting items from the database polling table votes , and sets the obtained data to the data source of the data grid control votelist. The program code for the function Page_Load (Object Sender,eventargs e) and function Bindvotelistdata () is as follows:
private void Page_Load (object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{//Binding voting items
Bindvotelistdata ();
Votemessage.visible = false;
}
}
private void Bindvotelistdata ()
{//Get all the data
Webvote.vote Vote = new Vote ();
SqlDataReader recv = vote. Getvotes ();
Set the data source for the control and bind the data
Votelist.datasource = recv;
Votelist.databind ();
Recv. Close (); Turn off data readers
} |
After the online voting system is run, the voting page webonlinvote.aspx the initialization interface shown in Figure 7, which displays the information of the project being voted on.
Figure 7 Initialization interface for voting page webonlinvote.aspx
2. Voting function
The user clicks the "I want to vote" button and the "View vote" button in the page webonlinvote.aspx to trigger the event Votebtn_click (object sender, System.EventArgs e), and event Showvote_click (object sender, System.EventArgs e), which implement the user voting function and view the voting function respectively. In a polling event, the event first checks which items the user has voted on, and then changes the number of votes for the item. In the view polling event, the event is redirected to the page showvoteinfo.aspx. The program code for event Votebtn_click (object sender, System.EventArgs e), and event Showvote_click (object sender, System.EventArgs e) is as follows:
private void Votebtn_click (object sender, System.EventArgs e)
{//Definition class
Webvote.vote Vote = new Vote ();
Try
{//Add user's vote item
foreach (DataGridItem item in Votelist.items)
{//Find the selection control for each voting item
CheckBox check = (checkbox) item. FindControl ("Votecheck");
if (check!= null)
{//stating that the user has voted, you need to add this ticket
if (check. Checked = = True)
{//Modify the number of votes in the database
Vote. Updatevote (Int32.Parse (
Votelist.datakeys[item. ItemIndex]. ToString ()));
Votemessage.visible = true; Displays the results of a user voting operation
}
}
}
Display action Results Information
Response.Write ("<script> Window.alert") ('
Vote for success, thank you for your participation!!! ') </script>);
}
catch (Exception ex)
{//Show failure, error message in the Modify operation
Response.Redirect ("~/desktopmodules/errorpage.aspx?") Errorurl= "
+ Aspnet2system.redirecterrorurl (Request.rawurl)
+ "&errormessage=" + ex. Message.replace ("\ n", ""));
}
}
private void Showvote_click (object sender, System.EventArgs e)
{//Guide to view poll results page
Response.Redirect ("~/showvoteinfo.aspx");
} |
Display voting results page design
Add a new Web page to the application webvote and name it showvoteinfo.aspx, and its code-behind file is a ShowVoteInfo.aspx.cs file.
1. Page Design
Adds a data grid control, a label control, and a button control to the page showvoteinfo.aspx, which are named Votelist, Votemessage, webonlinevotebtn. Control votelist is used to show the voting status of the items participating in the voting, and to calculate the percentage of each vote item; Control Votemessage Displays the total number of votes the user votes; Control webonlinevotebtn implement voting page webonlinvote.aspx. The page showvoteinfo.aspx design interface is shown in Figure 8.
Figure 8 Design interface for page showvoteinfo.aspx
The HTML Design code for the page showvoteinfo.aspx is as follows:
<%@ Page language= "C #" autoeventwireup= "true"
Codefile= "ShowVoteInfo.aspx.cs" inherits= "Showvoteinfo"%>
<HTML> <HEAD> <title> Network online polling system </title> </HEAD>
<asp:datagrid id= "votelist" runat= "Server" cssclass= "Normal"
autogeneratecolumns= "False" datakeyfield= "Voteid" >
<Columns>
<asp:templatecolumn headertext= "Voting project"
<itemstyle width= "200px" > </ItemStyle>
<ItemTemplate> <%# DataBinder.Eval (Container.DataItem, "Item")%>
</ItemTemplate> </asp:TemplateColumn>
<asp:templatecolumn headertext= "Percentage of total votes"
<itemstyle width= "300px" > </ItemStyle>
<ItemTemplate>
<asp:image id= "Voteimage" runat= "Server" height= "width=" <%#
Formatvoteimage (Formatvotecount (DataBinder.Eval) (
Container.DataItem, "Votecount"). ToString ()))%> '
Mageurl= "Images/vote.gif" >
</asp:Image>
<%# Formatvotecount (DataBinder.Eval (Container.DataItem,
"Votecount"). ToString ())%>%
</ItemTemplate> </asp:TemplateColumn>
<asp:templatecolumn headertext= "Number of votes"
<itemstyle width= "100px" > </ItemStyle>
<ItemTemplate>
<asp:label id= "Votecount" runat= "Server"
<%# DataBinder.Eval (Container.DataItem, "Votecount")%>
</asp:Label>
</ItemTemplate> </asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:label id= "Votemessage" runat= "Server" forecolor= "Red"
Width= "100%" > </asp:Label>
<asp:button id= "webonlinevotebtn" runat= "server" width= "100"
Text= "Back to voting page" cssclass= "Buttoncss"
> </asp:button>
</HTML> |
2. Page initialization
The page showvoteinfo.aspx calls the function Page_Load (Object Sender,eventargs e) initialized. The function calls the function bindvotelistdata () to get all the voting items from the database polling table votes and bind the obtained data to the data grid control votelist. The function Page_Load (Object Sender,eventargs E) also calls the function setvotetotal () to get the total number of votes from the database. The program code for the function Page_Load (Object Sender,eventargs e), function setvotetotal (), and function Bindvotelistdata () is as follows:
int votetotal = 0;
private void Page_Load (object sender, System.EventArgs e) {//Set total number of votes votetotal Setvotetotal (); if (! Page.IsPostBack) {//Show details of the user's vote Bindvotelistdata (); Votemessage.text = "Total number of votes for:" + votetotal.tostring (); } }
private void Setvotetotal () {//Get all the data Webvote.vote Vote = new Vote (); SqlDataReader recv = vote. Getvotes (); votetotal = 0;
Read each item that participates in the vote and calculate the total number of votes
while (recv. Read ()) {//calculate the sum of them Votetotal + = Int32.Parse (recv["Votecount"). ToString ()); } Recv. Close (); }
private void Bindvotelistdata () {//Get Data Webvote.vote Vote = new Vote (); SqlDataReader recv = vote. Getvotes ();
Set the data source for the control and bind the control's data
Votelist.datasource = recv; Votelist.databind ();
Recv. Close (); } |
When the page showvoteinfo.aspx is initialized (that is, when the data grid control votelist binding data), the function Formatvotecount (String votecount) and function formatvoteimage are called respectively (int Votecount) To calculate the percentage of each voting item and the length of the image (drawing a scale picture). The program code for the function Formatvotecount (String votecount) and function formatvoteimage (int votecount) is as follows:
public int Formatvotecount (String votecount) {//If the vote is not voted on if (votecount.length <= 0) {//Return 0 percent return (0); } if (Votetotal > 0) {//returns the actual percentage Return ((Int32.Parse (votecount) * 100/votetotal)); } return (0); }
public int formatvoteimage (int votecount) {//Returns the length of the image as a percentage Return (Votecount * 3); } |
After the online voting system runs, the initialization interface showing the voting results page showvoteinfo.aspx is shown in Figure 9, showing the results of each item.
Figure 9 The voting results page at some point showvoteinfo.aspx