投票系统 查看投票功能(ASP.NET C#)
在做查看投票功能时,显示所占总票百分比这个功能无法成功实现。请各位高手们帮帮我。附我运行后的图。显示百分比这个图没有实现成功。前台界面部分代码:<Columns><asp...
在做查看投票功能时,显示所占总票百分比这个功能无法成功实现。请各位高手们帮帮我。附我运行后的图。显示百分比这个图没有实现成功。
前台界面部分代码:
<Columns>
<asp:BoundField DataField="Item" HeaderText="投票项目" SortExpression="Item" />
<asp:BoundField DataField="VoteCount" HeaderText="票数" SortExpression="VoteCount" />
<asp:BoundField HeaderText="所占总票百分比" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="VoteImage" runat="server" Height="20" Width='<%#FormatVoteImage(FormatVoteCount
(DataBinder.Eval(Container.DataItem,"VoteCount").ToString())) %>' ImageUrl="~/image/VOTE.gif" />
<%#FormatVoteCount(DataBinder.Eval(Container.DataItem,"VoteCount").ToString()) %>%
</ItemTemplate>
</asp:TemplateField>
</Columns>后台代码:
public partial class ShowVote : System.Web.UI.Page
{
int VoteTotal=0;
protected void Page_Load(object sender, EventArgs e)
{
}
private void SetVoteTotal()
{
int VoteTotal;
//获取所有数据
ShowVote vote = new ShowVote();
SqlDataReader a = vote.GetVotes();
VoteTotal = 0;
//读取每一个选项,并计算票数总和
while (a.Read())
{
//计算它们的总和
VoteTotal += Int32.Parse(a["VoteCount"].ToString());
}
a.Close();
}
public int FormatVoteCount(String VoteCount)
{
//如果投票没有被投票
if (VoteCount.Length <= 0)
{
//返回0个百分比
return (0);
}
if (VoteTotal > 0)
{
return ((Int32.Parse(VoteCount) * 100/VoteTotal));
}
return (0);
}
public int FormatVoteImage(int VoteCount)
{
//返回百分比的图像的长度
return (VoteCount * 3);
}
public SqlDataReader GetVotes()
{
//进行数据库字符连接
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
SqlConnection myconn = new SqlConnection(settings);
//打开数据库
myconn.Open();
SqlCommand mycmd = new SqlCommand("GetVotes", myconn);
//设置命令类型
mycmd.CommandType = CommandType.StoredProcedure;
//定义保存从数据库获取的结果的DataReader
SqlDataReader dr = null;
try
{
//执行存储过程
dr = mycmd.ExecuteReader();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return (dr);
}
} 展开
前台界面部分代码:
<Columns>
<asp:BoundField DataField="Item" HeaderText="投票项目" SortExpression="Item" />
<asp:BoundField DataField="VoteCount" HeaderText="票数" SortExpression="VoteCount" />
<asp:BoundField HeaderText="所占总票百分比" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="VoteImage" runat="server" Height="20" Width='<%#FormatVoteImage(FormatVoteCount
(DataBinder.Eval(Container.DataItem,"VoteCount").ToString())) %>' ImageUrl="~/image/VOTE.gif" />
<%#FormatVoteCount(DataBinder.Eval(Container.DataItem,"VoteCount").ToString()) %>%
</ItemTemplate>
</asp:TemplateField>
</Columns>后台代码:
public partial class ShowVote : System.Web.UI.Page
{
int VoteTotal=0;
protected void Page_Load(object sender, EventArgs e)
{
}
private void SetVoteTotal()
{
int VoteTotal;
//获取所有数据
ShowVote vote = new ShowVote();
SqlDataReader a = vote.GetVotes();
VoteTotal = 0;
//读取每一个选项,并计算票数总和
while (a.Read())
{
//计算它们的总和
VoteTotal += Int32.Parse(a["VoteCount"].ToString());
}
a.Close();
}
public int FormatVoteCount(String VoteCount)
{
//如果投票没有被投票
if (VoteCount.Length <= 0)
{
//返回0个百分比
return (0);
}
if (VoteTotal > 0)
{
return ((Int32.Parse(VoteCount) * 100/VoteTotal));
}
return (0);
}
public int FormatVoteImage(int VoteCount)
{
//返回百分比的图像的长度
return (VoteCount * 3);
}
public SqlDataReader GetVotes()
{
//进行数据库字符连接
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
SqlConnection myconn = new SqlConnection(settings);
//打开数据库
myconn.Open();
SqlCommand mycmd = new SqlCommand("GetVotes", myconn);
//设置命令类型
mycmd.CommandType = CommandType.StoredProcedure;
//定义保存从数据库获取的结果的DataReader
SqlDataReader dr = null;
try
{
//执行存储过程
dr = mycmd.ExecuteReader();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return (dr);
}
} 展开
2个回答
展开全部
<Columns>
<asp:BoundField DataField="Item" HeaderText="投票项目" SortExpression="Item" />
<asp:BoundField DataField="VoteCount" HeaderText="票数" SortExpression="VoteCount" />
<asp:BoundField HeaderText="所占总票百分比" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="VoteImage" runat="server" Height="20" Width='<%#FormatVoteImage(FormatVoteCount
(DataBinder.Eval(Container.DataItem,"VoteCount").ToString())) %>' ImageUrl="~/image/VOTE.gif" />
<%#FormatVoteCount(DataBinder.Eval(Container.DataItem,"VoteCount").ToString()) %>%
</ItemTemplate>
</asp:TemplateField>
</Columns>后台代码:
public partial class ShowVote : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private int SetVoteTotal()
{
int VoteTotal;
//获取所有数据
SqlDataReader a = this.GetVotes();
VoteTotal = 0;
//读取每一个选项,并计算票数总和
while (a.Read())
{
//计算它们的总和
VoteTotal += Int32.Parse(a["VoteCount"].ToString());
}
a.Close();
return(VoteTotal);
}
public int FormatVoteCount(String VoteCount)
{
int VoteTotal=this.SetVoteTotal();
//如果投票没有被投票
if (VoteCount.Length <= 0)
{
//返回0个百分比
return (0);
}
if (VoteTotal > 0)
{
return ((Int32.Parse(VoteCount) * 100/VoteTotal));
}
return (0);
}
public int FormatVoteImage(int VoteCount)
{
//返回百分比的图像的长度
return (VoteCount * 3);
}
public SqlDataReader GetVotes()
{
//进行数据库字符连接
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
SqlConnection myconn = new SqlConnection(settings);
//打开数据库
myconn.Open();
SqlCommand mycmd = new SqlCommand("GetVotes", myconn);
//设置命令类型
mycmd.CommandType = CommandType.StoredProcedure;
//定义保存从数据库获取的结果的DataReader
SqlDataReader dr = null;
try
{
//执行存储过程
dr = mycmd.ExecuteReader();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return (dr);
}
}
<asp:BoundField DataField="Item" HeaderText="投票项目" SortExpression="Item" />
<asp:BoundField DataField="VoteCount" HeaderText="票数" SortExpression="VoteCount" />
<asp:BoundField HeaderText="所占总票百分比" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="VoteImage" runat="server" Height="20" Width='<%#FormatVoteImage(FormatVoteCount
(DataBinder.Eval(Container.DataItem,"VoteCount").ToString())) %>' ImageUrl="~/image/VOTE.gif" />
<%#FormatVoteCount(DataBinder.Eval(Container.DataItem,"VoteCount").ToString()) %>%
</ItemTemplate>
</asp:TemplateField>
</Columns>后台代码:
public partial class ShowVote : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private int SetVoteTotal()
{
int VoteTotal;
//获取所有数据
SqlDataReader a = this.GetVotes();
VoteTotal = 0;
//读取每一个选项,并计算票数总和
while (a.Read())
{
//计算它们的总和
VoteTotal += Int32.Parse(a["VoteCount"].ToString());
}
a.Close();
return(VoteTotal);
}
public int FormatVoteCount(String VoteCount)
{
int VoteTotal=this.SetVoteTotal();
//如果投票没有被投票
if (VoteCount.Length <= 0)
{
//返回0个百分比
return (0);
}
if (VoteTotal > 0)
{
return ((Int32.Parse(VoteCount) * 100/VoteTotal));
}
return (0);
}
public int FormatVoteImage(int VoteCount)
{
//返回百分比的图像的长度
return (VoteCount * 3);
}
public SqlDataReader GetVotes()
{
//进行数据库字符连接
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
SqlConnection myconn = new SqlConnection(settings);
//打开数据库
myconn.Open();
SqlCommand mycmd = new SqlCommand("GetVotes", myconn);
//设置命令类型
mycmd.CommandType = CommandType.StoredProcedure;
//定义保存从数据库获取的结果的DataReader
SqlDataReader dr = null;
try
{
//执行存储过程
dr = mycmd.ExecuteReader();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return (dr);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询