Webform之(简单投票)练习

Webform之(简单投票)练习

 

 

创建数据库:

 

复制代码
CREATE table DiaoYanTiMu
 (
    Ids int primary key ,--题目代号
   Title varchar(50) not null ,--要调查的题目
   SelectionType int,--单选多选代号,0-单选,1-多选
    IsOver  bit,--是否结束,true-结束,flase-未结束
 )
GO
 
 CREATE table DiaoYanXuanXiang
 (
 Ids varchar(50)primary key,
 Options varchar(50),--调研每个题目的选项内容
Numebers int,--选择此调研选项的人数
 TiMuDaiHao int references DiaoYanTiMu(Ids),--所属调研题目的代号
 )
 INSERT into DiaoYanTiMu VALUES('1','晚上在家干啥?','0','0')
 INSERT INTO DiaoYanTiMu VALUES('2','自己那个地方有欠缺','1','1')
INSERT INTO diaoyanxuanxiang VALUES('1','玩游戏','0','1')
 INSERT INTO diaoyanxuanxiang VALUES('2','睡觉','0','1')
 INSERT INTO diaoyanxuanxiang VALUES('3','吃饭打豆豆','0','1')
 INSERT INTO diaoyanxuanxiang VALUES('4','做练习','0','1')
 INSERT INTO diaoyanxuanxiang VALUES('5','知识点不会','0','2')
 INSERT INTO diaoyanxuanxiang VALUES('6','听不懂','0','2')
 INSERT INTO diaoyanxuanxiang VALUES('7','比较懒欠练习','0','2')
 INSERT INTO diaoyanxuanxiang VALUES('8','解决能力弱','0','2')
 INSERT INTO diaoyanxuanxiang VALUES('9','缺乏资源','0','2')
 SELECT * from DiaoYanTiMu
SELECT * FROM diaoyanxuanxiang
复制代码

aspx代码:

复制代码
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Linq" %>

<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        * {
            margin:0px;
            padding:0px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
             <%-- 显示题目内容 --%>
          <div id="vote" style="width:500px;height:300px"> 
           <asp:CheckBoxList ID="CheckBoxList1" runat="server">
           </asp:CheckBoxList>
           <br />
           <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
           &nbsp; 
              <input type="button" value="查看结果" οnclick="xianshi()" />
           
       </div>   
            <%-- 显示结果 --%>
       <div id ="result" style="display:none;">
           <ul style ="list-style:none">
               <% 
                   contestDataContext _context = new contestDataContext();
                   var query =   _context.DiaoYanXuanXiang.Where(p=>p.TiMuDaiHao==1).ToList();
                   int sum = query.Sum(p => p.Numebers).Value;//总票数
                    //分别去取值,增加宽度
                   foreach(DiaoYanXuanXiang data in query)
                   {
                       int ps = data.Numebers.Value;//投的票数,获取出来
                       double bf = Math.Round(((ps * 1.0) / sum) * 100, 2);//math.round(数值,取小数点后几位),取余数;数值*1.0后操作可以变小数;整数除以整数是整数
                       double width =  bf * 2;
                    %>
                    <li style ="width:600px;height:20px; margin-top:10px">
                        <div style="width:150px;height:20px; float:left"><%=data.Options%></div>
                        <div style ="width:200px;height:20px; background-color:#f6f1f1; float:left;">
                            <div style="width:<%=width%>px;height:20px; background-color:#00ff21"></div>
                        </div>
                        <div style="width:20px;height:20px; float:left;"><%=data.Numebers%></div>
                        <div style="width:80px;height:20px; float:left;">(<%=bf%>%)</div>
                    </li>
               <%
                   }
                    %>
               

           </ul>
           <input type="button" value="返回" οnclick="fanhui()" />
       </div>
   </div>
    </form>
    <script type="text/javascript">
        function fanhui()
        {
            document.getElementById("vote").style.display = "block";
            document.getElementById("result").style.display = "none";
        }
        function xianshi() {
            document.getElementById("result").style.display = "block";
            document.getElementById("vote").style.display = "none";
        }
    </script>
</body>
</html>
复制代码

aspx.cs代码:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {    
            //查询题目
            contestDataContext _contest = new contestDataContext();
            var query = _contest.DiaoYanTiMu.Where(p=>p.Ids ==1);
             Label1.Text = query.First().Title;

             //显示题目名称
             CheckBoxList1.DataSource = _contest.DiaoYanXuanXiang.Where(p=>p.TiMuDaiHao==query.First().Ids);
             CheckBoxList1.DataTextField = "Options";
             CheckBoxList1.DataValueField = "Ids";
             CheckBoxList1.DataBind();
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        contestDataContext _contest = new contestDataContext();
        //取投票的项,将票项更改
        foreach(ListItem list in  CheckBoxList1.Items)
        {   
            if(list.Selected)
            {
                var query = _contest.DiaoYanXuanXiang.Where(p=>p.Ids == list.Value);//每一项只有一个value
                query.First().Numebers += 1;
                _contest.SubmitChanges();
            }
        }




    }
}
复制代码

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值