zoukankan      html  css  js  c++  java
  • 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
    View Code

    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="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="查看结果" onclick="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 ="600px;height:20px; margin-top:10px">
                            <div style="150px;height:20px; float:left"><%=data.Options%></div>
                            <div style ="200px;height:20px; background-color:#f6f1f1; float:left;">
                                <div style="<%=width%>px;height:20px; background-color:#00ff21"></div>
                            </div>
                            <div style="20px;height:20px; float:left;"><%=data.Numebers%></div>
                            <div style="80px;height:20px; float:left;">(<%=bf%>%)</div>
                        </li>
                   <%
                       }
                        %>
                   
    
               </ul>
               <input type="button" value="返回" onclick="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();
                }
            }
    
    
    
    
        }
    }
  • 相关阅读:
    Allegro转换PADS终极篇.....
    PNP与NPN三极管
    windows无法设置防火墙
    SourceInsight 不断无响应的有关问题
    看片/复制/下载技巧
    WinRAR 5.01 正式版 (简体中文)附注册机及注册码
    JVM 学习笔记 1. JVM 运行模型
    Core Java笔记 9.集合(I)
    Core Java笔记 8.泛型(II)
    Core Java笔记 7.泛型(I)
  • 原文地址:https://www.cnblogs.com/franky2015/p/4876988.html
Copyright © 2011-2022 走看看