用户控件Vote.ascx代码

  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="vote.ascx.cs" Inherits="vote" %>
  2. <link href="style.css" rel="stylesheet" type="text/css" />
  3. <style type="text/css">
  4. .style1
  5. {
  6. font-size: small;
  7. color: #0066CC;
  8. }
  9. </style>
  10. <div id="box">
  11. <table width="370" border="0">
  12. <tr>
  13. <td width="364" height="37" class="style1">您对本公司的售后服务是否满意?</td>
  14. </tr>
  15. <tr>
  16. <td height="106">
  17. <asp:RadioButtonList ID="RadioButtonList1" runat="server" Font-Size="9pt">
  18. <asp:ListItem Selected="True">非常满意</asp:ListItem>
  19. <asp:ListItem>基本满意</asp:ListItem>
  20. <asp:ListItem>不满意</asp:ListItem>
  21. <asp:ListItem>不发表意见</asp:ListItem>
  22. </asp:RadioButtonList>
  23. </td>
  24. </tr>
  25. <tr>
  26. <td height="30" align="center">
  27. <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/dc_03.gif"
  28. onclick="ImageButton1_Click" />
  29.       <img src="data:images/dc_05.gif" width="82" height="33" onclick="window.location.replace('result.aspx')" style="cursor:pointer"/></td>
  30. </tr>
  31. </table>
  32. </div>

用户控件vote.ascx.cs代码:

  1. public partial class vote : System.Web.UI.UserControl
  2. {
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5.  
  6. }
  7. /// <summary>
  8. /// 从txt文件中读取投票数量
  9. /// </summary>
  10. /// <param name="P_str_path">要读取的txt文件的路径及名称</param>
  11. /// <returns>返回一个int类型的值,用来记录投票数量</returns>
  12. public static int readCount(string P_str_path)
  13. {
  14. int P_int_count = ;
  15. StreamReader streamread;
  16. streamread = File.OpenText(P_str_path);
  17. while (streamread.Peek() != -)
  18. {
  19. P_int_count = int.Parse(streamread.ReadLine());
  20. }
  21. streamread.Close();
  22. return P_int_count;
  23. }
  24. /// <summary>
  25. /// 写入投票数量
  26. /// </summary>
  27. /// <param name="P_str_path">要操作的txt文件的路径及名称</param>
  28. public static void addCount(string P_str_path)
  29. {
  30. int P_int_count = readCount(P_str_path);
  31. P_int_count += ;
  32. //将数据记录写入文件
  33. StreamWriter streamwriter = new StreamWriter(P_str_path, false);
  34. streamwriter.WriteLine(P_int_count);
  35. streamwriter.Close();
  36. }
  37. protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  38. {
  39. string P_str_IP = Request.UserHostAddress.ToString();
  40. HttpCookie oldCookie = Request.Cookies["userIP"];
  41. if (oldCookie == null)
  42. {
  43. int flag = RadioButtonList1.SelectedIndex;
  44. switch (flag)
  45. {
  46. case : addCount(Server.MapPath("result1.txt")); break;
  47. case : addCount(Server.MapPath("result2.txt")); break;
  48. case : addCount(Server.MapPath("result3.txt")); break;
  49. case : addCount(Server.MapPath("result4.txt")); break;
  50. }
  51. Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('投票成功,谢谢您的参与!');", true);
  52. HttpCookie newCookie = new HttpCookie("userIP"); //定义新的Cookie对象
  53. newCookie.Expires = DateTime.MaxValue;
  54. //添加新的Cookie变量IPaddress,值为P_str_IP
  55. newCookie.Values.Add("IPaddress", P_str_IP);
  56. Response.AppendCookie(newCookie); //将变量写入Cookie文件中
  57. }
  58. else
  59. {
  60. string P_str_oldIP = oldCookie.Values["IPaddress"];
  61. if (P_str_IP.Trim() == P_str_oldIP.Trim())
  62. {
  63. Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('一个IP地址只能投一次票,谢谢您的参与!');", true);
  64. }
  65. else
  66. {
  67. HttpCookie newCookie = new HttpCookie("userIP");
  68. newCookie.Values.Add("IPaddress", P_str_IP);
  69. newCookie.Expires = DateTime.MaxValue;
  70. Response.AppendCookie(newCookie);
  71. int rflag = RadioButtonList1.SelectedIndex;
  72. switch (rflag)
  73. {
  74. case : addCount("result1.txt"); break;
  75. case : addCount("result2.txt"); break;
  76. case : addCount("result3.txt"); break;
  77. case : addCount("result4.txt"); break;
  78. }
  79. Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('投票成功,谢谢您的参与!');", true);
  80. }
  81. }
  82. }
  83. }

结果页面result.aspx代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <%@ Register src="vote.ascx" tagname="vote" tagprefix="uc1" %>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head runat="server">
  9. <title>在线投票系统</title>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <uc1:vote ID="vote1" runat="server" />
  14. </form>
  15. </body>
  16. </html>

结果页面result.aspx.cs代码:

  1. public partial class Default2 : System.Web.UI.Page
  2. {
  3. protected string M_str_rate1;
  4. protected string M_str_rate2;
  5. protected string M_str_rate3;
  6. protected string M_str_rate4;
  7. protected int P_int_count1;
  8. protected int P_int_count2;
  9. protected int P_int_count3;
  10. protected int P_int_count4;
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. P_int_count1 = readCount(Server.MapPath("result1.txt"));
  14. P_int_count2 = readCount(Server.MapPath("result2.txt"));
  15. P_int_count3 = readCount(Server.MapPath("result3.txt"));
  16. P_int_count4 = readCount(Server.MapPath("result4.txt"));
  17. int P_int_count = P_int_count1 + P_int_count2 + P_int_count3+P_int_count4;
  18. if (P_int_count == )
  19. {
  20. Response.Write("<script>alert('还没有人投过票!')</script>");
  21. lblresult.Text = "共有0人参与投票";
  22. }
  23. else
  24. {
  25. M_str_rate1 = (Convert.ToDouble(P_int_count1) * / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
  26. M_str_rate2 = (Convert.ToDouble(P_int_count2) * / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
  27. M_str_rate3 = (Convert.ToDouble(P_int_count3) * / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
  28. M_str_rate4 = (Convert.ToDouble(P_int_count4) * / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
  29. lblresult.Text = "共有" + P_int_count.ToString() + "人参与投票";
  30. }
  31. }
  32. /// <summary>
  33. /// 从txt文件中读取投票数量
  34. /// </summary>
  35. /// <param name="P_str_path">要读取的txt文件的路径及名称</param>
  36. /// <returns>返回一个int类型的值,用来记录投票数量</returns>
  37. public static int readCount(string P_str_path)
  38. {
  39. int P_int_count = ;
  40. StreamReader streamread;
  41. streamread = File.OpenText(P_str_path);
  42. while (streamread.Peek() != -)
  43. {
  44. P_int_count = int.Parse(streamread.ReadLine());
  45. }
  46. streamread.Close();
  47. return P_int_count;
  48. }
  49. }

默认页面Default.aspx页面代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <%@ Register src="vote.ascx" tagname="vote" tagprefix="uc1" %>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head runat="server">
  9. <title>在线投票系统</title>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <uc1:vote ID="vote1" runat="server" />
  14. </form>
  15. </body>
  16. </html>

style.css代码:

  1. #box {
  2. height: 195px;
  3. width: 370px;
  4. background-image: url(images/1.jpg);
  5. background-repeat: no-repeat;
  6. padding-top: 65px;
  7. padding-right: 45px;
  8. padding-left: 45px;
  9. }
  10. #box2 {
  11. height: 195px;
  12. width: 370px;
  13. background-image: url(images/2.jpg);
  14. background-repeat: no-repeat;
  15. padding-top: 65px;
  16. padding-right: 45px;
  17. padding-left: 45px;
  18. }

用到的四个文件:

用到的几张图片:

最终效果展示:

035. asp.netWeb用户控件之四通过用户控件实现投票和结果分析的更多相关文章

  1. 033. asp.netWeb用户控件之二将页面转换成web控件和使用Web控件显示热点新闻

    访问Web用户控件的属性 ASP.NET提供的各种服务器控件都有其自身的属性和方法,程序开发人员可以灵活地使用服务器控件中的属性和方法开发程序.在用户控件中,程序开发人员也可以自行定义各种属性和方法, ...

  2. ASP.NET MVC中加载WebForms用户控件(.ascx)

    原文:ASP.NET MVC中加载WebForms用户控件(.ascx) 问题背景 博客园博客中的日历用的是ASP.NET WebForms的日历控件(System.Web.UI.WebControl ...

  3. 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  4. JS获取用户控件中的子控件Id

    用户控件 <asp:HiddenField ID="hfGradeId" runat="server" /> <asp:HiddenField ...

  5. asp.net web 开发登录相关操作的控件LoginName、LoginStatus和LoginView控件使用详解

    http://book.51cto.com/art/200909/154039.htm http://book.51cto.com/art/200909/154041.htm ASP.NET提供了一套 ...

  6. ASP.NET MVC显示WebForm网页或UserControl控件

    ASP.NET MVC显示WebForm网页或UserControl控件 学习与使用ASP.NET MVC这样久,还是对asp.net念念不忘.能否在asp.net mvc去显示aspx或是user ...

  7. 念念不忘,ASP.NET MVC显示WebForm网页或UserControl控件

    学习与使用ASP.NET MVC这样久,还是对asp.net念念不忘.能否在asp.net mvc去显示aspx或是user control呢?这个灵感(算不上灵感,只能算是想法)是来自前些天有写过一 ...

  8. 【Asp.net之旅】--因自己定义控件注冊而引发的思考

    前言 近期在开发远洋的SOA系统平台,开发使用的是.NET平台.对于Asp.net并不困难,但该系统的开发并非全然依靠Asp.net.而是自身封装好的框架.这套框架是远洋地产购买的微软的开发平台,项目 ...

  9. asp.net 弹出式日历控件 选择日期 Calendar控件

    原文地址:asp.net 弹出式日历控件 选择日期 Calendar控件 作者:逸苡 html代码: <%@ Page Language="C#" CodeFile=&quo ...

随机推荐

  1. Pycharm 介绍

    Pycharm官方站点: http://www.jetbrains.com/pycharm/ Pycharm百科: Pycharm百度百科 Pycharm是由jetbrains开发的优秀的python ...

  2. transition&transform,CSS中过度和变形的设置

    设置样式的过度效果transition-property: none/all; transition-duration:2s;运动时间,默认是0秒 transition-delay:0s; 延迟时间默 ...

  3. postgresql - 服务配置

    1.查看配置的修改是否需要重启数据库 select name,context from pg_settings where name like 'wal_buffers'; 如果context的值是p ...

  4. /proc/stat文件详解(翻译)

    原文地址:http://www.linuxhowtos.org/System/procstat.htm 各种关于系统内核的活动信息都可以在/proc/stat文件中找到,该文件记录了自系统第一次启动以 ...

  5. javascript画直线和画圆的方法(非HTML5的方法)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. html基本标签结构

    简单的html5的基本架构 HTML全称为HyperText Markup Language,一款写给浏览器的语言. HyperText:超文本(文本,图片,视频,音频,链接) Markup Lanu ...

  7. 对比Linux系统和Windows系统哪个更好

    最近半年来,我一直在读一本叫做<Linux就该这么学>的Linux教材,确实让我进步的很快,也慢慢的让Linux系统走入了我的知识认知中,那么学习前我们来对比下Windows和Linux的 ...

  8. 省常中模拟 day1

    第一题: 题目大意: 给出N个数的数列,如果相邻的两个数加起来是偶数,那么就可以把这两个数消掉,求最多能消掉多少数. 解题过程: 1.先自己手工模拟了几组数据,发现不管消除的顺序如何,最终剩下的是一定 ...

  9. php max_input_vars限制数组大小

    今天做一个项目需要post2000个数组过去,发现一直只能接到一半,后来发现是max_input_vars显示问题. 修改php.ini里面max_input_vars的大小就可以了

  10. Spring 整合 Struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...