?? pollresult.aspx.cs
字號(hào):
?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()); //接收輸入的數(shù)據(jù),Request控件;
string Path = Request.PhysicalApplicationPath + @"\App_Data\savepoll.dat"; //.dat是2進(jìn)制文本文件
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); //寫入二進(jìn)制流
bw1.Write(010);
bw1.Write(010);
bw1.Close();
}
else
{
BinaryReader br = new BinaryReader(new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); //創(chuàng)建文件
int poll1 = br.ReadInt32(); //讀文件流(頭四個(gè)字節(jié))
int poll2 = br.ReadInt32();
int poll3 = br.ReadInt32();
if (i == 0) poll1++;
else if (i == 1) poll2++;
else if (i == 2) poll3++;
br.Close(); //關(guān)閉文件
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將投票總數(shù)以字符串的形式顯示在頁(yè)面上
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)); //把新的數(shù)據(jù)寫入文件
bw2.Write(poll1);
bw2.Write(poll2);
bw2.Write(poll3);
bw2.Close();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -