Sunday, February 6, 2011

Creating Simple Poll Vote system using text file in back end.

On Code bind :

using System;
using System.Data;
using System.Configuration;
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.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int yes;
        int no;
        string filename = Server.MapPath("filesave.txt");
        StreamReader objreader = new StreamReader(filename);
        string line = objreader.ReadLine();
        string[] args= line.Split('|');
        yes = int.Parse(args[0].ToString());
        no = int.Parse(args[1].ToString());
        label1.Text = Convert.ToString(yes + no);
        objreader.Close();
    }
    protected void rdboncheckedchanged(object sender, EventArgs e)
    {
        if (rdbone.Checked)
        {
            getPercentage(0);//yes=0;No=1;
        }
    }
    protected void rdbtwooncheckedchanged(object sender, EventArgs e)
    {
        if (rdbtwo.Checked)
        {
            getPercentage(1);//yes=0;No=1;
        }
    }
 
    public void getPercentage(int value)
    {
        string filename = Server.MapPath("filesave.txt");
        StreamReader objreader = new StreamReader(filename);
        string line = objreader.ReadLine();
        int yes;
        int no;
        string[] args= line.Split('|');
        yes = int.Parse(args[0].ToString());
        no = int.Parse(args[1].ToString());
        double percYes = 0.0;
        double percNo = 0.0;
        if (value == 0)
        {
            yes = yes + 1;
            percYes = 100 * Math.Round((Convert.ToDouble(yes) / Convert.ToDouble((no + yes))), 2);
            percNo = 100 * Math.Round((Convert.ToDouble(no) / Convert.ToDouble((no + yes))), 2);
        }
        else if (value == 1)
        {
            no = no + 1;
            percYes = 100 * Math.Round((Convert.ToDouble(yes) / Convert.ToDouble((no + yes))), 2);
            percNo = 100 * Math.Round((Convert.ToDouble(no) / Convert.ToDouble((no + yes))), 2);
        }
        objreader.Close();
        StreamWriter objWriter = new StreamWriter(filename);
        objWriter.WriteLine(yes + "|" + no);
        objWriter.Close();
        rdbone.Visible = false;
        rdbtwo.Visible = false;
        img1.Visible = true;
        img2.Visible = true;
        label1.Text = Convert.ToString(yes + no);
        img1.Width = Convert.ToInt32(percYes);
        img2.Width = Convert.ToInt32(percNo);
        lbl1.Text = Convert.ToInt32(percYes) + "%";
        lbl2.Text = Convert.ToInt32(percNo) + "%";
    }
}


On .aspx page :

<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
#clsbold
{
font-weight:bold;
font-family:Trebuchet MS
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div  id="clsbold">
    <p>Do you like Asp.net??? </p>
    <table>
    <tr>
    <td style="width: 40px; height: 40px;">Yes
    <br /><br />
    No
    </td>
    <td style="width: 13px; text-align:center; height: 40px; ">:
     <br /><br />
     :
    </td>
    <td style="width: 200px; height: 40px;">
     <asp:RadioButton ID="rdbone" GroupName="yesno" runat="server" OnCheckedChanged="rdboncheckedchanged"
AutoPostBack="true"/>
     <img src="poll.gif" alt="" runat="server" id="img1" height="20" visible="false"/>
     <asp:Label ID="lbl1"  runat="server" ></asp:Label>
     <br />
    <br />
    <asp:RadioButton ID="rdbtwo" GroupName="yesno" runat="server" OnCheckedChanged="rdbtwooncheckedchanged"
AutoPostBack="true"/>
      <img src="poll.gif" alt="" runat="server" id="img2" height="18"  visible="false"/>
      <asp:Label ID="lbl2"  runat="server" ></asp:Label>
    </td>
    </tr>
    </table>
    <div style="height:30px; margin-top:10px;">
    Total Poll Votes: <asp:Label runat="server" ID="label1" Text=""></asp:Label>
        </div>
        </div>
    </form>
</body>
</html>

No comments:

Post a Comment