Webform之(简单投票)练习

Webform之(简单投票)练习

Webform之(简单投票)练习_第1张图片Webform之(简单投票)练习_第2张图片

 

 

创建数据库:

 

复制代码
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" %>

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

"http://www.w3.org/1999/xhtml">
"server">
"Content-Type" content="text/html; charset=utf-8"/>
    
    


    
"form1" runat="server">
"Label1" runat="server">
<%-- 显示题目内容 --%>
"vote" style="width:500px;height:300px"> "CheckBoxList1" runat="server">
"Button1" runat="server" Text="提交" OnClick="Button1_Click" /> &nbsp; "button" value="查看结果" οnclick="xianshi()" />
<%-- 显示结果 --%>
"result" style="display:none;">
    "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; %>
  • "width:600px;height:20px; margin-top:10px">
    "width:150px;height:20px; float:left"><%=data.Options%>
    "width:200px;height:20px; background-color:#f6f1f1; float:left;">
    "width:<%=width%>px;height:20px; background-color:#00ff21">
    "width:20px;height:20px; float:left;"><%=data.Numebers%>
    "width:80px;height:20px; float:left;">(<%=bf%>%)
  • <% } %>
"button" value="返回" οnclick="fanhui()" />
复制代码

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();
            }
        }




    }
}
复制代码

 

你可能感兴趣的:(Webform之(简单投票)练习)