《ASP.NET1200例》实现投票的用户控件
用户控件ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="249VoteControl.ascx.cs" Inherits="FirstWeb._249VoteControl1" %> 您对公司餐饮服务是否满意? <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem Value="0">非常满意</asp:ListItem> <asp:ListItem Value="1">比较满意</asp:ListItem> <asp:ListItem Value="2">一般</asp:ListItem> </asp:RadioButtonList> <asp:Button ID="Button1" runat="server" Text="我要投票" onclick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="查看结果" onclick="Button2_Click" />
控件后台代码ascx.cs
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 将投票数量写入文件,然后读取出来显示投票结果 /// </summary> public partial class _249VoteControl1 : System.Web.UI.UserControl { public static int readCount(String fielPath) { int count = 0; StreamReader sr = File.OpenText(fielPath) ; while (sr.Peek() != -1) { count =int.Parse( sr.ReadLine().ToString()); } sr.Close(); return count; } public static void addCount(String filePath) { int count = readCount(filePath); StreamWriter sw = new StreamWriter(filePath,false); count = count + 1; sw.WriteLine(count); sw.Close(); } protected void Button1_Click(object sender, EventArgs e) { String userIp = Request.UserHostAddress.ToString(); HttpCookie oldCookie = Request.Cookies["ipCookie"]; if (oldCookie == null) { int flag = RadioButtonList1.SelectedIndex;//--- switch (flag) { case 0: addCount(Server.MapPath("Vote1.txt")); break; case 1: addCount(Server.MapPath("Vote2.txt")); break; case 2: addCount(Server.MapPath("Vote3.txt")); break; } Page.ClientScript.RegisterStartupScript(this.GetType(),"","alert('投票成功,感谢您的参与');",true); HttpCookie newCookie = new HttpCookie("ipCookie"); newCookie.Values.Add("Ip", userIp); newCookie.Expires = DateTime.Now.AddSeconds(5);//Cookie 的过期时间设置为当前时间之后5秒 Response.AppendCookie(newCookie); //--- } else { if (oldCookie.Values["Ip"].ToString().Trim() == userIp.Trim()) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('同一个IP只能投票一次');", true); } int flag = RadioButtonList1.SelectedIndex; //--- switch (flag) { case 0: addCount(Server.MapPath("Vote1.txt")); break; case 1: addCount(Server.MapPath("Vote2.txt")); break; case 2: addCount(Server.MapPath("Vote3.txt")); break; } Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('投票成功,感谢您的参与');", true); HttpCookie newCookie = new HttpCookie("ipCookie"); newCookie.Values.Add("Ip", userIp); newCookie.Expires = DateTime.Now.AddSeconds(5);//Cookie 的过期时间设置为当前时间之后5秒 Response.AppendCookie(newCookie); //--- } } protected void Button2_Click(object sender, EventArgs e) { Response.Write("<script>window.open('249VoteResult.aspx','','height=500,width=600');</script>"); }
引用控件的页面aspx
<%@ Register Src="~/249VoteControl.ascx" TagName="VoteControl" TagPrefix="uc3" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <uc3:VoteControl runat="server">//对投票控件的应用 </uc3:VoteControl> </div> </form> </body>
投票结果页面
VoteResult.aspx
<body> <form id="form1" runat="server"> <div> <table id="myTable" border="1" cellpadding="5" cellspacing="0" runat="server" style="" > <h2>查看投票结果</h2> <tr><td>序号</td> <td>投票意见</td> <td>票数</td> <td>百分比</td> </tr> <tr><td>1</td> <td>非常满意</td> <td><%=vote1%></td> <td><%=vote1percent%> % </td> </tr> <tr><td>2</td> <td>比较满意</td> <td><%=vote2%></td> <td><%=vote2percent%> % </td> </tr> <tr><td>3</td> <td>一般</td> <td><%=vote3%></td> <td><%=vote3percent%> % </td> </tr> </table> <h3>参与投票人数共<%=count%></h3> </div> </form> </body>
VoteResult.aspx.cs
public int vote1; public int vote2; public int vote3; public String vote1percent; public String vote2percent; public String vote3percent; public int count; public static int readCount(String fielPath) { int count = 0; StreamReader sr = File.OpenText(fielPath); while (sr.Peek() != -1) { count = int.Parse(sr.ReadLine().ToString()); } sr.Close(); return count; } protected void Page_Load(object sender, EventArgs e) { vote1 = readCount(Server.MapPath("Vote1.txt")); vote2 = readCount(Server.MapPath("Vote2.txt")); vote3 = readCount(Server.MapPath("Vote3.txt")); count=vote1+vote2+vote3; vote1percent = (Convert.ToDouble(vote1) * 100 / Convert.ToDouble(count)).ToString("0.00"); //将double型转换为string型。并保留2位小数点 vote2percent = (Convert.ToDouble(vote2) * 100 / Convert.ToDouble(count)).ToString("0.00"); //将double型转换为string型。并保留2位小数点 vote3percent = (Convert.ToDouble(vote3) * 100 / Convert.ToDouble(count)).ToString("0.00"); //将double型转换为string型。并保留2位小数点 }
总结:
【1】对控件的引用<%@ Register Src="~/249VoteControl.ascx" TagName="VoteControl" TagPrefix="uc3" %>
<uc3:VoteControl runat="server">
</uc3:VoteControl>
【2】关于文件的读写
StreamReader sr = File.OpenText(fielPath) ;
while (sr.Peek() != -1)
{
count =int.Parse( sr.ReadLine().ToString());
}
StreamWriter sw = new StreamWriter(filePath,false);
sw.WriteLine(count);
【3】疑惑: Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('同一个IP只能投票一次');", true);
【4】关于查看投票的结果百分比问题
将double型转换为string型。并保留2位小数点
vote1percent = (Convert.ToDouble(vote1) * 100 / Convert.ToDouble(count)).ToString("0.00");
【5】最后发点牢骚---困扰程序运行的都是一些小的细节,以前老师总说:现在偷得懒以后总要还的,
现在真的被一些基础的细节问题扰的崩溃。写代码需要沉下心,再沉下心,忌心浮气躁。
相关文章
- 无法下载apk等格式的文件的解决方案---ASP .NET Core 2.0 MVC 发布到IIS上以后无法下载apk等格式的文件的解决方案
- ASP.NET Core托管和部署Linux实操演练手册
- Asp.NET WebApi+Redis实现单用户登录实战演练
- ASP.NET WebApi 基于分布式Session方式实现Token签名认证
- ASP.NET 最佳DataGrid读取单元格的值
- ASP.NET 尖括号 百分号 井号 等号 的用法
- ASP服务器I I S出现authentication mode=Windows错误解决办法
- ASP.NET找不到类型或命名空间名称怎么办
- Asp.net管道模型(管线模型)之一发不可收拾
- ASP.Net Web中Repeater怎么删除指定行
- ASP.NET 窗体间传值实现方法详解
- [转]利用ASP.NET 2.0创建自定义Web控件(2)
- Asp.Net Core轻量级Aop解决方案:AspectCore
- 关于Asp.Net中的编程实现下载
- asp乱码问题
- [Log]ASP.NET之HttpModule 事件执行顺序
- ASP.NET Core 6框架揭秘实例演示[19]:数据加解密与哈希
- ASP.NET Core应用基本编程模式[4]:基于承载环境的编程
- ASP.NET Core应用基本编程模式[2]:依赖注入
- 通过极简模拟框架让你了解ASP.NET Core MVC框架的设计与实现[下篇]:参数绑定
- 用最简单的方式在ASP.NET Core应用中实现认证、登录和注销
- [ASP.NET Core 3框架揭秘] 依赖注入[9]:实现概述
- [ASP.NET Core 3框架揭秘] 跨平台开发体验: Windows [中篇]
- [ASP.NET MVC] 利用自定义的AuthenticationFilter实现Basic认证
- 【踩坑经历】一次Asp.NET小网站部署踩坑和解决经历
- asp.net拦截器
- Asp.Netserver控制发展Grid实现(一个)UI转让
- ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存
- ASP.NET MVC3默认提供了11种ActionResult的实现
- ASP.Net Core 发布到 Centos Docker
- ASP.NET Core教程:ASP.NET Core中使用Redis缓存