📄 pollview.aspx.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;
public partial class pollView : sp.userbase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
void bind()
{
if (sp.PageValidate.IsNumber(Request.QueryString["id"]))
{
string id = Request.QueryString["id"];
string sql = "select * from sp_answer where for_question='" + id + "' ;";
sql += "select *,(select sum(amount) from sp_answer where for_question=sp_question.id) as amount from sp_question where id=" + id + "";
DataSet ds = sp.Base.ExecuteSql4Ds(sql);
DataTable dt = ds.Tables[0];//答案表
DataTable dt1 = ds.Tables[1];//问题表
if (Request.QueryString["act"] == "result")
{
bindresult(dt, dt1);
}
else
{
bindvote(dt, dt1);
}
}
}
/// <summary>
/// 绑定结果
/// </summary>
void bindresult(DataTable dt, DataTable dt1)
{
string s = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=" + dt1.Rows[0]["v_css"] + " >\n";
s += "<tr><td>" + dt1.Rows[0]["v_title"] + "</td></tr>";
decimal decNumAll = Convert.ToDecimal(dt1.Rows[0]["amount"]);//得到所有票数,
for (int i = 0; i < dt.Rows.Count; i++)
{
decimal decItemNum = Convert.ToDecimal(dt.Rows[i]["amount"]);//得到当前票数
decimal decPercent = GetPercent(decItemNum, decNumAll)*100 ;//得到百分比
string strPercent = decPercent.ToString();//将百分比转为字符型
if (strPercent.Length > 5)//如果百分比结果长度超过5位
{
strPercent = strPercent.Substring(0, 5);//将百分比的余数截短为“00.00”
}
s += "<tr><td>" +dt.Rows[i]["answer_cn"]+"</td>";
s += "<td align=\"left\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td width=\"10\" height=\"20\"><img src=\"pic/voteBar_l.gif\" alt=\"\"/></td>";
s += "<td><img src=\"pic/voteBar_m.gif\" height=\"20\" width=\""+strPercent+"px\" alt=\"\"/></td>";
s += "<td width=\"10\"><img src=\"pic/voteBar_r.gif\" alt=\"\"/></td>";
s += "<td style=\"font-size:8pt;font-family:arial\"> " + strPercent + "% (" + decItemNum.ToString() + ")</td>";
s += "</table></td></tr>";
}
s += "</table>";
this.Literal1.Text = s;
}
/// <summary>
/// 绑定投票选项
/// </summary>
void bindvote(DataTable dt, DataTable dt1)
{
Buttonvote.Visible = true;
string s = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class="+dt1.Rows[0]["v_css"]+" >\n";
s += "<tr><td>" + dt1.Rows[0]["v_title"] + "</td></tr>";
string votetype = dt1.Rows[0]["multiple"].ToString() == "True" ? "checkbox" : "radio";
for (int i = 0; i < dt.Rows.Count; i++)
{
string votelangluage = Request.QueryString["language"] == "en" ? dt.Rows[i]["answer_en"].ToString() : dt.Rows[i]["answer_cn"].ToString();
s += "<tr><td><input type=\"" + votetype + "\" value=\""+dt.Rows[i]["id"].ToString()+"\" name=\"item\"/>" + votelangluage + "</td></tr>";
}
s += "</table>";
this.Literal1.Text = s;
}
//------------------------ 百分比 ------------------------
//decItem:当前选项本身的票身;decNumAll:所有的票数
public decimal GetPercent(decimal decItem, decimal decNumAll)
{
if (decNumAll == 0)//如果总票数是零
{
decNumAll++;//加一,避免除0出错
}
decimal decPercent = decItem / decNumAll;
return decPercent;
}
protected void Buttonvote_Click(object sender, EventArgs e)
{
string[] item = Request.Form["item"].Split(',');
if (item.Length > 0)
{
string sql = "update sp_answer set amount=amount+1 where id="+item[0];
for (int i = 1; i < item.Length; i++)
{
sql += " or id=" + item[i];
}
Response.Write(sql);
sp.Base.ExecuteSql(sql);
}
sp.MessageBox.Show(this,"谢谢您的参与!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -