投票系统设计

投票系统设计(ASP.NET内置对象综合应用)

实训目标

1、  了解Page对象的事件

2、  了解Request对象常用方法和属性

3、  了解Response对象常用方法和属性

4、  能够创建网页模板

   5、了解Application对象常用方法和属性

   6、了解Session对象常用方法和属性

   7、了解Cookie对象常用方法和属性

   8、理解Application对象的生命周期、Session对象的生存期、Cookie对象的生命周期

实施步骤

1.在线投票系统登陆界面(default.aspx),这里是用匿名用户登录,不需要密码,但用户名不能为空,登录到投票页面。

图1 在线投票系统登录界面

//default.aspx.cs后台代码参考

protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    //点击按钮事件代码

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (TextBox1.Text != "")//如何文本框不为空才执行下面的代码

        {

            Session["username"] = TextBox1.Text.Trim();//保存Session,记住会话状态

            Response.Redirect("vote.aspx");//跳转到投票页面

        }

        else

        {

            Response.Write("<script>alert('用户名不能空!')</script>");//用户名文本框为空时,弹出对话框提示

        }

}

 

 

2.在线投票系统主界面(vote.aspx),用于显示登录用户的用户名,登录的IP地址,并显示投票选项单选按钮列表、投票按钮、查看结果按钮,退出登录按钮等。

用户选择具体选项进行投票,每个用户只允许投票一次!使用Cookie对象实现一个客户

只能投票一次,点击投票按钮会提示“投票成功”或者“已经投过票”。

 

图2在线投票系统主界面

//vote.aspx.cs代码参考

    //页面加载时执行的代码

    protected void Page_Load(object sender, EventArgs e)

    {

        //RadioButtonList1.Items[0].Selected = true;//设置第一项为默认选中项

        if (Session["username"] != null)//判断是否有登录?

            Label1.Text = Session["username"] + "欢迎您!IP地址:"+Request.UserHostAddress;

        else

            Response.Redirect("default.aspx");//没有登录则跳转到登录页面

    }

    //点击按钮事件代代码

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (RadioButtonList1.SelectedValue != "")//判断有没有选择?

        {

            //if(Request.Cookies["ip"]!=null){//判断cookie是否存在

           // string newip=Request.Cookies["ip"].Value;

            //if(newip==Request.UserHostAddress){//判断cookie中的IP是否相同

            //    Response.Write("<script>alert('你已经投票啦!')</script>");

             //   return;//跳出方法,结束!

            //   }

             // }

            int selectvalue = Convert.ToInt32(RadioButtonList1.SelectedValue);//获取单选按钮列表的选中项的值

            switch (selectvalue)//根据不同的选择,执行不同的代码,采用switch分支结构(打开对应数据文件,改变投票数)

            {

                case 1://选择:1广州,2深圳,3东莞,4珠海

                    read2write(1);

                    break;

                case 2:

                    read2write(2);

                    break;

                case 3:

                    read2write(3);

                    break;

                case 4:

                    read2write(4);

                    break;

            }

            //保存cookie

            string ip = Request.UserHostAddress;//获取IP地址

            Response.Cookies["ip"].Value = ip;//定义cookie

            Response.Cookies["ip"].Expires = DateTime.MaxValue;//设置cookie有效期,必须设置有效期才能生效

            Response.Write("<script>alert('投票成功!')</script>");

        }

        else

        {

            Response.Write("<script>alert('必须选择!')</script>");

        }

    }

    //自定义一个方法,根据选择不同的项目带入参数,读取不同的文本数据文件并完成写入!

    public void read2write(int i)

    {

        System.IO.StreamReader sr = System.IO.File.OpenText(Server.MapPath("." + "\\data\\"+i+".txt"));//打开文件

        //System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath("." + "\\data\\" + i + ".txt"));//使用这个方法也可以

        Application.Lock();

        Application["count"] = sr.ReadLine();// 读取内容

        sr.Close();

        int all = Convert.ToInt32(Application["count"]);

        all += 1;

        System.IO.StreamWriter rw = System.IO.File.CreateText(Server.MapPath("." + "\\data\\" + i + ".txt"));//创建文件

        //System.IO.StreamWriter rw = new System.IO.StreamWriter(Server.MapPath("." + "\\data\\" + i + ".txt"));//使用这个方法也可以

        rw.WriteLine(all);//写入内容

        rw.Close();

        Application.UnLock();

    }

    //查看结果按钮事件代码

    protected void Button2_Click(object sender, EventArgs e)

    {

        Server.Transfer("result.aspx");

    }

    //退出登录按钮事件代码

    protected void Button3_Click(object sender, EventArgs e)

    {

        Session["username"] = null;//清除session,关闭会话状态

        Session.Clear();

        Response.Redirect("default.aspx");

}

 

3.在线投票系统的投票结果查看界面(result.aspx),把结果用数字和图形显示出来。

图3在线投票系统结果查看页面

//result.aspx.cs代码参考

//页面加载事件代码

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            //if (Session["username"] == null)//判断是否登录

            //    Response.Redirect("default.aspx");

            int c1, c2, c3, c4, total;//定义变量,分别存储每一个投票选项的票数

            System.IO.StreamReader sr;

            sr = System.IO.File.OpenText(Server.MapPath("." + "\\data\\1.txt"));//分别读取每一个数据文件的内容

            c1 = Convert.ToInt32(sr.ReadLine());

            sr.Close();

            sr = System.IO.File.OpenText(Server.MapPath("." + "\\data\\2.txt"));

            c2 = Convert.ToInt32(sr.ReadLine());

            sr.Close();

            sr = System.IO.File.OpenText(Server.MapPath("." + "\\data\\3.txt"));

            c3 = Convert.ToInt32(sr.ReadLine());

            sr.Close();

            sr = System.IO.File.OpenText(Server.MapPath("." + "\\data\\4.txt"));

            c4 = Convert.ToInt32(sr.ReadLine());

            sr.Close();

            total = c1 + c2 + c3 + c4;//计数总票数

            Label1.Text = c1.ToString();//在label控件中显示对应的票数(赋值)

            Label2.Text = c2.ToString();

            Label3.Text = c3.ToString();

            Label4.Text = c4.ToString();

            Label5.Text = total.ToString();

 

            Label1.Width = 300 * c1 / total;//根据比例设置对应label控件的宽度

            Label2.Width = 300 * c2 / total;

            Label3.Width = 300 * c3 / total;

            Label4.Width = 300 * c4 / total;

            Label5.Width = 300 * 1;

        }

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值