⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pollresult.aspx.cs

📁 随着计算机科学的发展
💻 CS
字号:
using System;
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.IO;
//命名空间
public partial class pollresult : System.Web.UI.Page
{
    float poll1ratio = 0F;
    float poll2ratio = 0F;
    float poll3ratio = 0F;

    protected void Page_Load(object sender, EventArgs e)
    {
        Image1.Height = 15;
        Image2.Height = 15;
        Image3.Height = 15;  //初始化图片高度
        int i = int.Parse(Request.QueryString["result"].ToString());  //接收输入的数据,Request控件;
        string Path = Request.PhysicalApplicationPath + @"\App_Data\savepoll.dat";  //.dat是2进制文本文件
        if (!File.Exists(Path))
        {
            FileStream fs = File.Create(Path);  //打开文件
            fs.Close();
            BinaryWriter bw1 = new BinaryWriter(new FileStream(Path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite));
            bw1.Write(010); //写入二进制流
            bw1.Write(010);
            bw1.Write(010);
            bw1.Close();
        }
        else
        {
            BinaryReader br = new BinaryReader(new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));  //创建文件
            int poll1 = br.ReadInt32();  //读文件流(头四个字节)
            int poll2 = br.ReadInt32();
            int poll3 = br.ReadInt32();
            if (i == 0) poll1++;
            else if (i == 1) poll2++;
            else if (i == 2) poll3++;
            br.Close();  //关闭文件

            int pollsum = poll1 + poll2 + poll3;

            poll1ratio = Convert.ToSingle((float)poll1 / pollsum);  //得到比率
            lbyes.Text = Math.Round((poll1ratio * 100), 2).ToString() + "%";  //得到百分比

            poll2ratio = Convert.ToSingle((float)poll2 / pollsum);
            lbno.Text = Math.Round((poll2ratio * 100), 2).ToString() + "%";

            poll3ratio = Convert.ToSingle((float)poll3 / pollsum);
            lbunknow.Text = Math.Round((poll3ratio * 100), 2).ToString() + "%";

            lbsum.Text = pollsum.ToString();  //j将投票总数以字符串的形式显示在页面上

            Image1.Width = (int)(poll1ratio * 100);  //将得到的百分比作为图片的宽度
            Image2.Width = (int)(poll2ratio * 100);
            Image3.Width = (int)(poll3ratio * 100);

            BinaryWriter bw2 = new BinaryWriter(new FileStream(Path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite));  //把新的数据写入文件
            bw2.Write(poll1);
            bw2.Write(poll2);
            bw2.Write(poll3);
            bw2.Close();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -