ASP投票页面

利用xml存储数据

 

VoteXml.xml,初始人名票数

<?xml version="1.0" standalone="yes"?>
<myXml>
  <Vote id="1">
    <Name>那英</Name>
    <Count>325</Count>
  </Vote>
  <Vote id="2">
    <Name>汪峰</Name>
    <Count>219</Count>
  </Vote>
  <Vote id="3">
    <Name>庾澄庆</Name>
    <Count>340</Count>
  </Vote>
  <Vote id="4">
    <Name>周杰伦</Name>
    <Count>463</Count>
  </Vote>
</myXml>

Vote.aspx,投票页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Vote.aspx.cs" Inherits="Vote" %>

<!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">
    <div>
    
        <table class="auto-style1">
            <tr>
                <td class="auto-style2" colspan="4">在线投票系统</td>
            </tr>
            <tr>
                <td class="auto-style4" colspan="4" style="font-size: large; text-align: center"><strong>《中国好声音》的导师你最喜欢哪一个?</strong></td>
            </tr>
            <tr>
                <td class="auto-style3">
                    <asp:Image ID="Image1" runat="server" Height="100px" Width="100px" ImageUrl="~/images/那英.jpg"  />
                    <br />
                    <asp:RadioButton ID="rbtnNY" runat="server" Text="那英" Checked="True" GroupName="v" />
                </td>
                <td class="auto-style3">
                    <asp:Image ID="Image2" runat="server" Height="100px" Width="100px" ImageUrl="~/images/汪峰.jpg" />
                    <br />
                    <asp:RadioButton ID="rbtnWF" runat="server" Text="汪峰" GroupName="v" />
                </td>
                <td class="auto-style3">
                    <asp:Image ID="Image3" runat="server" Height="100px" Width="100px" ImageUrl="~/images/庾澄庆.jpg" />
                    <br />
                    <asp:RadioButton ID="rbtnYCQ" runat="server" Text="庾澄庆" GroupName="v" />
                </td>
                <td class="auto-style3">
                    <asp:Image ID="Image4" runat="server" Height="100px" Width="100px" ImageUrl="~/images/周杰伦.jpg" />
                    <br />
                    <asp:RadioButton ID="rbtnZJL" runat="server" Text="周杰伦" GroupName="v" />
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="投票" />
                </td>
                <td>
                    <asp:Button ID="Button2" runat="server" Text="查看投票结果" OnClick="Button2_Click" />
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

设计:

 

 

内部功能代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Vote : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void VoteAdd()
    {
        try
        {
            //利用xml存储数据
            DataSet voteds = new DataSet();
            voteds.ReadXml(Server.MapPath("VoteXml.xml"));
            if (rbtnNY.Checked)
                voteds.Tables[0].Rows[0][1] = Convert.ToInt32(voteds.Tables[0].Rows[0][1]) + 1;
            if (rbtnWF.Checked)
                voteds.Tables[0].Rows[1][1] = Convert.ToInt32(voteds.Tables[0].Rows[1][1]) + 1;
            if (rbtnYCQ.Checked)
                voteds.Tables[0].Rows[2][1] = Convert.ToInt32(voteds.Tables[0].Rows[2][1]) + 1;
            if (rbtnZJL.Checked)
                voteds.Tables[0].Rows[3][1] = Convert.ToInt32(voteds.Tables[0].Rows[3][1]) + 1;
            voteds.WriteXml(Server.MapPath("VoteXml.xml"));
            Response.Write("<script>alert('投票成功!');</script>");
        }
        catch
        {
            Response.Write("<script>alert('投票失败!')</script>");
        }
    }
    private void VoteDb()
    {
        try
        {
            //利用数据库存储数据
            string strCon = @"Data Source = REDWENDY\SQLEXPRESS; ; database=VoteDB;Trusted_Connection=true;";
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            string sqlstr = "update votetable set VoteNum=VoteNum+1 where voteno='";
            if (rbtnNY.Checked)
                sqlstr = sqlstr + rbtnNY.Text.Trim() + "'";
            if (rbtnWF.Checked)
                sqlstr = sqlstr + rbtnWF.Text.Trim() + "'";
            if (rbtnYCQ.Checked)
                sqlstr = sqlstr + rbtnYCQ.Text.Trim() + "'";
            if (rbtnZJL.Checked)
                sqlstr = sqlstr + rbtnZJL.Text.Trim() + "'";
            cmd.CommandText = sqlstr;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('投票成功!')</script>");
        }
        catch
        {
            Response.Write("<script>alert('投票失败!')</script>");
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        this.VoteAdd();
        // VoteDb();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("VoteResult.aspx");
    }
}

 

结果显示页面,VoteResult.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="VoteResult.aspx.cs" Inherits="VoteResult" %>

<!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">
    <div>
    <table border="1" style="width: 768px; height: 184px">
            <tr>
                <td colspan="4" style="height: 53px; text-align: center">
                    <strong><span style="font-size: 14pt; color: #0000ff">《中国好声音》调查(《中国好声音》第五季导师你最喜欢哪一个?)</span></strong></td>
            </tr>
            <tr>
                <td bgcolor="#3399ff" style="width: 100px">
                </td>
                <td bgcolor="#3399ff" style="width: 100px">
                </td>
                <td bgcolor="#3399ff" style="width: 100px; text-align: right">
                    <span style="font-size: 10pt; color: #ffff33"><strong>总票数</strong></span></td>
                <td bgcolor="#3399ff" style="width: 460px">
                    <asp:Label ID="lbCount" runat="server" Font-Size="10pt"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 100px; text-align: center">
                    <span style="font-size: 10pt"><strong>导师姓名</strong></span></td>
                <td style="width: 100px; text-align: center">
                    <span style="font-size: 10pt"><strong>投票数</strong></span></td>
                <td style="width: 100px; text-align: center">
                    <span style="font-size: 10pt"><strong>百分比</strong></span></td>
                <td style="width: 460px; text-align: center">
                    <span style="font-size: 10pt"><strong>比例</strong></span></td>
            </tr>
            <tr>
                <td bgcolor="#ff9966" style="width: 100px; text-align: center; height: 30px;">
                    <span style="font-size: 10pt">那英</span></td>
                <td bgcolor="#ff9966" style="width: 100px; text-align: center; height: 30px;">
                    <asp:Label ID="lbVote1" runat="server" Font-Size="10pt" Width="43px"></asp:Label></td>
                <td bgcolor="#ff9966" style="width: 100px; text-align: center; height: 30px;">
                    <asp:Label ID="lbBFB1" runat="server" Font-Size="10pt"></asp:Label><span style="font-size: 10pt">%</span></td>
                <td bgcolor="#ff9966" style="width: 460px; height: 30px;">
                    <asp:Label ID="lbBL1" runat="server" Font-Size="10pt" Height ="16px" BackColor="Yellow" BorderColor="Black" BorderWidth="1px"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 100px; height: 24px; text-align: center">
                    <span style="font-size: 10pt">汪峰</span></td>
                <td style="width: 100px; height: 24px; text-align: center">
                    <asp:Label ID="lbVote2" runat="server" Font-Size="10pt"></asp:Label></td>
                <td style="width: 100px; height: 24px; text-align: center">
                    <asp:Label ID="lbBFB2" runat="server" Font-Size="10pt"></asp:Label><span style="font-size: 10pt">%</span></td>
                <td style="width: 460px; height: 24px">
                    <asp:Label ID="lbBL2" runat="server" Font-Size="10pt"  Height ="16px" BackColor="Red" BorderColor="Black" BorderWidth="1px"></asp:Label></td>
            </tr>
            <tr>
                <td bgcolor="#ff9966" style="width: 100px; height: 25px; text-align: center">
                    <span style="font-size: 10pt">庾澄庆</span></td>
                <td bgcolor="#ff9966" style="width: 100px; height: 25px; text-align: center">
                    <asp:Label ID="lbVote3" runat="server" Font-Size="10pt"></asp:Label></td>
                <td bgcolor="#ff9966" style="width: 100px; height: 25px; text-align: center">
                    <asp:Label ID="lbBFB3" runat="server" Font-Size="10pt"></asp:Label><span style="font-size: 10pt">%</span></td>
                <td bgcolor="#ff9966" style="width: 460px; height: 25px">
                    <asp:Label ID="lbBL3" runat="server" Font-Size="10pt" Height ="16px" BackColor="#0000C0" BorderColor="Black" BorderWidth="1px"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 100px; height: 30px; text-align: center">
                    <span style="font-size: 10pt">周杰伦</span></td>
                <td style="width: 100px; height: 30px; text-align: center">
                    <asp:Label ID="lbVote4" runat="server" Font-Size="10pt"></asp:Label></td>
                <td style="width: 100px; height: 30px; text-align: center">
                    <asp:Label ID="lbBFB4" runat="server" Font-Size="10pt"></asp:Label><span style="font-size: 10pt">%</span></td>
                <td style="width: 460px; height: 30px">
                    <asp:Label ID="lbBL4" runat="server" Font-Size="10pt" Height ="16px" BackColor="Fuchsia" BorderColor="Black" BorderWidth="1px"></asp:Label></td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

设计:

内部功能代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class VoteResult : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         this.Vote();
       // this.ShowDb();

    }
    void Vote()
    {
        DataSet ds = new DataSet();
        //读取VoteXml文档
        ds.ReadXml(Server.MapPath("VoteXml.xml"));
        int count, countGzj, countHx, countKh, countLxy;
        //从VoteXml文档中读取各主播的投票总数
        countGzj = Convert.ToInt32(ds.Tables[0].Rows[0][1]);
        countHx = Convert.ToInt32(ds.Tables[0].Rows[1][1]);
        countKh = Convert.ToInt32(ds.Tables[0].Rows[2][1]);
        countLxy = Convert.ToInt32(ds.Tables[0].Rows[3][1]);
        count = countGzj + countHx + countKh + countLxy;
        //显示各主播的总票数
        lbVote1.Text = countGzj.ToString();
        lbVote2.Text = countHx.ToString();
        lbVote3.Text = countKh.ToString();
        lbVote4.Text = countLxy.ToString();
        lbCount.Text = count.ToString();
        //显示各主播的百分比
        float fLxy = (countLxy * 100) / count;
        float fHx = (countHx * 100) / count;
        float fKh = (countKh * 100) / count;
        float fGzj = (countGzj * 100) / count;
        lbBFB1.Text = fGzj.ToString();
        lbBFB2.Text = fHx.ToString();
        lbBFB3.Text = fKh.ToString();
        lbBFB4.Text = fLxy.ToString();
        //显示各主播的百分比图形
        lbBL1.Width = Convert.ToInt32(fGzj); ;
        lbBL2.Width = Convert.ToInt32(fHx); ;
        lbBL3.Width = Convert.ToInt32(fKh); ;
        lbBL4.Width = Convert.ToInt32(fLxy); ;
    }

    private void ShowDb()
    {
        string strCon = @"Data Source = REDWENDY\SQLEXPRESS; ; database=VoteDB;Trusted_Connection=true;";
        SqlConnection con = new SqlConnection(strCon);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Votetable", con);
        SqlDataAdapter dapt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dapt.Fill(ds);
        int countLxy = Convert.ToInt32(ds.Tables[0].Rows[0][1]);
        int countHx = Convert.ToInt32(ds.Tables[0].Rows[1][1]);
        int countKh = Convert.ToInt32(ds.Tables[0].Rows[2][1]);
        int countGzj = Convert.ToInt32(ds.Tables[0].Rows[3][1]);
        int count = countGzj + countHx + countKh + countLxy;
        //…与前面访问Xml文档的代码类似
        //显示各主播的总票数
        lbVote1.Text = countGzj.ToString();
        lbVote2.Text = countHx.ToString();
        lbVote3.Text = countKh.ToString();
        lbVote4.Text = countLxy.ToString();
        lbCount.Text = count.ToString();
        //显示各主播的百分比
        float fLxy = (countLxy * 100) / count;
        float fHx = (countHx * 100) / count;
        float fKh = (countKh * 100) / count;
        float fGzj = (countGzj * 100) / count;
        lbBFB1.Text = fGzj.ToString();
        lbBFB2.Text = fHx.ToString();
        lbBFB3.Text = fKh.ToString();
        lbBFB4.Text = fLxy.ToString();
        //显示各主播的百分比图形
        lbBL1.Width = Convert.ToInt32(fGzj); ;
        lbBL2.Width = Convert.ToInt32(fHx); ;
        lbBL3.Width = Convert.ToInt32(fKh); ;
        lbBL4.Width = Convert.ToInt32(fLxy); ;
    }


}

 结果:

点击投票:

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值