To Examples
voting-machine2.aspx Source Code
<!-- voting-machine2.aspx Example Keep track of vote totals in a server text file. --> <%@ Page Language="C#" %> <%@ Import Namespace="System.IO" %> <script runat=server> public string path = e:/ectserver/sjost/Database/vote-totals.txt; public void btnClearTotals_Click(object sender, EventArgs e) { // Reset vote totals. StreamWriter sw = new StreamWriter(path); sw.WriteLine(0); sw.WriteLine(0); sw.Close( ); litResults.Text = Totals reset.; } public void btnCastVote_Click(object sender, EventArgs e) { // Read current totals. StreamReader sr = new StreamReader(path); int mcCainTotal = int.Parse(sr.ReadLine( )); int obamaTotal = int.Parse(sr.ReadLine( )); sr.Close( ); // Update totals. if (radMcCain.Checked) { mcCainTotal++; } else if (radObama.Checked) { obamaTotal++; } // Write results back to file. StreamWriter sw = new StreamWriter(path); sw.WriteLine(mcCainTotal); sw.WriteLine(obamaTotal); sw.Close( ); litResults.Text = Vote recorded.; } public void btnShowTotals_Click(object sender, EventArgs e) { // Read current totals from file. StreamReader sr = new StreamReader(path); string mcCainTotal = sr.ReadLine( ); string obamaTotal = sr.ReadLine( ); sr.Close( ); // Display results. litResults.Text = Total Votes<br /> + ===========<br /> + Obama: + obamaTotal + <br /> + McCain: + mcCainTotal; } </script> <html> <head> <title>voting-machine2.aspx Example</title> <link rel=stylesheet class=text/css href=examples.css /> </head> <body> <h2>voting-machine2.aspx Example</h2> <h3>Keep track of vote totals in a server text file.</h3> <form id=frmQuiz runat=server> <table> <tr> <td> <asp:RadioButton ID=radObama GroupName=Candidates Text=Barack Obama Value=obama AutoPostBack=false runat=server /><br /> </td> <td> <asp:Image ID=imgObama runat=server ImageURL = images/obama.jpg Height=120px Width=95px /> </td> </tr> <tr> <td> <asp:RadioButton ID=radMcCain GroupName=Candidates Text=John Mccain Value=mccain AutoPostBack=false runat=server /><br /> </td> <td> <asp:Image ID=imgMcCain runat=server ImageURL = images/mccain.jpg Height=120px Width=95px /> </td> </tr> </table> <p><asp:Button runat=server id=btnCastVote Text=Cast Vote Width=240px OnClick=btnCastVote_Click /></p> <p><asp:Button runat=server id=btnShowResult Text=Show Vote Totals Width=240px OnClick=btnShowTotals_Click /></p> <p><asp:Button runat=server id=btnClearTotals Text=Clear Vote Totals Width=240px OnClick=btnClearTotals_Click /></p> <p><asp:Literal ID=litResults Text= runat=server /></p> </form> </body> </html>