using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace test
{
public partial class votes : System.Web.UI.Page
{
static string strconn = "Data Source=localhost;database=BOOKS;integrated security=SSPI";
SqlConnection conn = new SqlConnection(strconn);
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
calVote();
}
}
protected void button1_Click(object sender, EventArgs e)
{
}
protected void btnVote_Click(object sender, EventArgs e)
{
//注意要获取最近的IP,使用order by ip desc
string ipSQL = "SELECT ip,time from vote where ip='" +
Request.ServerVariables["REMOTE_ADDR"].ToString() + "' order by id desc";
TimeSpan span = new TimeSpan();
conn.Open();
cmd = new SqlCommand(ipSQL, conn);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
span = DateTime.Now.Subtract(DateTime.Parse(reader["time"].ToString()));
reader.Close();
if (span.TotalSeconds <= 2)
{
votelabel.Text = "您刚投过,稍后再投";
votelabel.Visible = true;
return;
}
}
reader.Close();
string insertSql = "insert into vote(test,ip,time) values(" +
RadioButtonList1.SelectedItem.Value + ",'" + Request.ServerVariables["REMOTE_ADDR"].ToString() +
"','" + DateTime.Now.ToString() + "')";
cmd.CommandText = insertSql;
try
{
cmd.ExecuteNonQuery();
MESSAGE.Text = "vote is done";
}
catch
{
MESSAGE.Text = "fail to vote";
}
conn.Close();
calVote();
}
//计算投票比率,先得出"非常好",和“不好”,然后用总数100减去两者之和,得出"无所谓"的比率
private void calVote()
{
string SQL1 = "SELECT COUNT(*) FROM vote WHERE test=1";//代表非常好
string SQL2 = "SELECT COUNT(*) FROM vote WHERE test=2";//代表不好
string SQL3 = "SELECT COUNT(*) FROM vote";//代表总数(指所投票的总数)
conn.Open();
cmd = new SqlCommand(SQL1, conn);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
int good = Convert.ToInt32(reader[0].ToString());
reader.Close();
cmd.CommandText = SQL2;
reader = cmd.ExecuteReader();
reader.Read();
int bad = Convert.ToInt32(reader[0].ToString());
reader.Close();
cmd.CommandText = SQL3;
reader = cmd.ExecuteReader();
reader.Read();
int total = Convert.ToInt32(reader[0].ToString());
reader.Close();
conn.Close();
if (total != 0)
{
int v1 = good * 100 / total;//非常好
int v2 = bad * 100 / total;//不好
int v3 = 100 - v1 - v2;//无所谓
vote1.Width = v1;
vote2.Width = v2;
vote3.Width = v3;
lblGood.Text = v1.ToString() + "%";
lblBad.Text = v2.ToString() + "%";
lblNo.Text = v3.ToString() + "%";
}
else
{
vote1.Attributes.Add("width", "100");
vote2.Attributes.Add("width", "100");
vote3.Attributes.Add("width", "100");
lblBad.Text = "0%";
lblGood.Text = "0%";
lblNo.Text = "0%";
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="votes.aspx.cs" Inherits="test.votes" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<img src="image/m001.jpg" />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="1" Selected="True">非常好</asp:ListItem>
<asp:ListItem Value="2">不好</asp:ListItem>
<asp:ListItem Value="3">无所谓</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="btnVote" runat="server" Height="28px" Text="投票" Width="80px" OnClick="btnVote_Click" />
<asp:Label ID="votelabel" runat="server" Text=""></asp:Label>
<td colspan=3>
目前投票</p>
<table>
<tr>
<td colspan=3 align=center style="height: 30px">目前投票</td>
</tr>
<tr><td align=right>非常好</td><td><asp:Image ID="vote1" runat="server"
Height="16px" ImageUrl="~/image/m001.jpg" Width="64px" /></td>
<td>
<asp:Label ID="lblGood" runat="server" ForeColor="Red"></asp:Label></td>
</tr>
<tr><td align=right>不好</td><td><asp:Image ID="vote2" runat="server"
Height="16px" ImageUrl="~/image/m001.jpg" Width="63px" /></td>
<td>
<asp:Label ID="lblBad" runat="server" ForeColor="Red"></asp:Label></td>
</tr>
<tr><td align=right>无所谓</td><td><asp:Image ID="vote3" runat="server"
Height="16px" ImageUrl="~/image/m001.jpg" Width="64px" /></td>
<td>
<asp:Label ID="lblNo" runat="server" ForeColor="Red"></asp:Label></td>
</tr> <td>
</table>
</tr>
<p>
<asp:Label ID="MESSAGE" runat="server" Text=""></asp:Label>
</p>
</form>
<p>
</p>
<p>
</p>
</body>
</html>
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace test
{
public partial class votes : System.Web.UI.Page
{
static string strconn = "Data Source=localhost;database=BOOKS;integrated security=SSPI";
SqlConnection conn = new SqlConnection(strconn);
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
calVote();
}
}
protected void button1_Click(object sender, EventArgs e)
{
}
protected void btnVote_Click(object sender, EventArgs e)
{
//注意要获取最近的IP,使用order by ip desc
string ipSQL = "SELECT ip,time from vote where ip='" +
Request.ServerVariables["REMOTE_ADDR"].ToString() + "' order by id desc";
TimeSpan span = new TimeSpan();
conn.Open();
cmd = new SqlCommand(ipSQL, conn);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
span = DateTime.Now.Subtract(DateTime.Parse(reader["time"].ToString()));
reader.Close();
if (span.TotalSeconds <= 2)
{
votelabel.Text = "您刚投过,稍后再投";
votelabel.Visible = true;
return;
}
}
reader.Close();
string insertSql = "insert into vote(test,ip,time) values(" +
RadioButtonList1.SelectedItem.Value + ",'" + Request.ServerVariables["REMOTE_ADDR"].ToString() +
"','" + DateTime.Now.ToString() + "')";
cmd.CommandText = insertSql;
try
{
cmd.ExecuteNonQuery();
MESSAGE.Text = "vote is done";
}
catch
{
MESSAGE.Text = "fail to vote";
}
conn.Close();
calVote();
}
//计算投票比率,先得出"非常好",和“不好”,然后用总数100减去两者之和,得出"无所谓"的比率
private void calVote()
{
string SQL1 = "SELECT COUNT(*) FROM vote WHERE test=1";//代表非常好
string SQL2 = "SELECT COUNT(*) FROM vote WHERE test=2";//代表不好
string SQL3 = "SELECT COUNT(*) FROM vote";//代表总数(指所投票的总数)
conn.Open();
cmd = new SqlCommand(SQL1, conn);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
int good = Convert.ToInt32(reader[0].ToString());
reader.Close();
cmd.CommandText = SQL2;
reader = cmd.ExecuteReader();
reader.Read();
int bad = Convert.ToInt32(reader[0].ToString());
reader.Close();
cmd.CommandText = SQL3;
reader = cmd.ExecuteReader();
reader.Read();
int total = Convert.ToInt32(reader[0].ToString());
reader.Close();
conn.Close();
if (total != 0)
{
int v1 = good * 100 / total;//非常好
int v2 = bad * 100 / total;//不好
int v3 = 100 - v1 - v2;//无所谓
vote1.Width = v1;
vote2.Width = v2;
vote3.Width = v3;
lblGood.Text = v1.ToString() + "%";
lblBad.Text = v2.ToString() + "%";
lblNo.Text = v3.ToString() + "%";
}
else
{
vote1.Attributes.Add("width", "100");
vote2.Attributes.Add("width", "100");
vote3.Attributes.Add("width", "100");
lblBad.Text = "0%";
lblGood.Text = "0%";
lblNo.Text = "0%";
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="votes.aspx.cs" Inherits="test.votes" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<img src="image/m001.jpg" />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="1" Selected="True">非常好</asp:ListItem>
<asp:ListItem Value="2">不好</asp:ListItem>
<asp:ListItem Value="3">无所谓</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="btnVote" runat="server" Height="28px" Text="投票" Width="80px" OnClick="btnVote_Click" />
<asp:Label ID="votelabel" runat="server" Text=""></asp:Label>
<td colspan=3>
目前投票</p>
<table>
<tr>
<td colspan=3 align=center style="height: 30px">目前投票</td>
</tr>
<tr><td align=right>非常好</td><td><asp:Image ID="vote1" runat="server"
Height="16px" ImageUrl="~/image/m001.jpg" Width="64px" /></td>
<td>
<asp:Label ID="lblGood" runat="server" ForeColor="Red"></asp:Label></td>
</tr>
<tr><td align=right>不好</td><td><asp:Image ID="vote2" runat="server"
Height="16px" ImageUrl="~/image/m001.jpg" Width="63px" /></td>
<td>
<asp:Label ID="lblBad" runat="server" ForeColor="Red"></asp:Label></td>
</tr>
<tr><td align=right>无所谓</td><td><asp:Image ID="vote3" runat="server"
Height="16px" ImageUrl="~/image/m001.jpg" Width="64px" /></td>
<td>
<asp:Label ID="lblNo" runat="server" ForeColor="Red"></asp:Label></td>
</tr> <td>
</table>
</tr>
<p>
<asp:Label ID="MESSAGE" runat="server" Text=""></asp:Label>
</p>
</form>
<p>
</p>
<p>
</p>
</body>
</html>