温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:大学生调查投票系统源码
当前文件:
StudentVote/Result.aspx.cs,打开代码结构图
StudentVote/Result.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.Data.SqlClient; 12
13
public partial class Result : System.Web.UI.Page 14
{ 15
protected void Page_Load(object sender, EventArgs e) 16
{ 17
SqlData da = new SqlData(); 18
SqlDataReader dr = da.ExceRead("select * from tb_Vote where ID="+Request["ID"]+""); 19
dr.Read(); 20
string strContent = dr["Content"].ToString(); 21
dr.Close(); 22
decimal decNumAll = GetNumAll(strContent);//得到所有票数,C#在除法运算中需用decimal类型,否则将会把小数点后的数去掉 23
Response.Write(GetResult(strContent, decNumAll)); 24
} 25
26
/// <summary> 27
/// 显示图象 28
/// </summary> 29
/// <param name="strContent">相关主题下的内容</param> 30
/// <param name="decNumAll">所有的票数</param> 31
/// <returns></returns> 32
public string GetResult(string strContent, decimal decNumAll) 33
{ 34
string[] arrContent = strContent.Split('|'); 35
string strBody = "<table width=100% border=1>\n"; 36
foreach (string strContentIN in arrContent) 37
{ 38
string strItemName = strContentIN.Split(',')[1].ToString();//得到选项名称 39
decimal decItemNum = Convert.ToDecimal(strContentIN.Split(',')[0]);//得到选项的投票数 40
decimal decPercent = GetPercent(decItemNum, decNumAll) * 100;//得到百分比 41
string strPercent = decPercent.ToString();//将百分比转为字符型 42
if (strPercent.Length > 5)//如果百分比结果长度超过5位 43
{ 44
strPercent = strPercent.Substring(0, 5);//将百分比的余数截短为“00.00” 45
} 46
strBody += "<tr><td width=100>" + strItemName + "</td><td width=50>" + decItemNum.ToString() + "票</td><td><img src=Images/bar1.gif height=10 width=" + strPercent + "%>" + strPercent + "%</td></tr>\n"; 47
} 48
strBody += "</table>"; 49
return strBody; 50
} 51
52
53
/// <summary> 54
/// 票数求和 55
/// </summary> 56
/// <param name="strNum">当前所要操作的字串</param> 57
/// <returns></returns> 58
public decimal GetNumAll(string strNum) 59
{ 60
decimal decNumAll = 0; 61
string[] arrNum = strNum.Split('|'); 62
foreach (string strNumIN in arrNum) 63
{ 64
decNumAll += Convert.ToInt32(strNumIN.Split(',')[0].ToString());//截取第零位 65
} 66
return decNumAll; 67
} 68
69
/// <summary> 70
/// 百分比 71
/// </summary> 72
/// <param name="decItem">当前选项本身的票数</param> 73
/// <param name="decNumAll">所有的票数</param> 74
/// <returns></returns> 75
public decimal GetPercent(decimal decItem, decimal decNumAll) 76
{ 77
if (decNumAll == 0)//如果总票数是零 78
{ 79
decNumAll++;//加一,避免除0出错 80
} 81
decimal decPercent = decItem / decNumAll; 82
return decPercent; 83
} 84
} 85





}
}