《ASP.NET1200例》实现投票的用户控件
用户控件ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="249VoteControl.ascx.cs" Inherits="FirstWeb._249VoteControl1" %>
您对公司餐饮服务是否满意?
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="">非常满意</asp:ListItem>
<asp:ListItem Value="">比较满意</asp:ListItem>
<asp:ListItem Value="">一般</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
/// <summary>
/// 将投票数量写入文件,然后读取出来显示投票结果
/// </summary>
public partial class _249VoteControl1 : System.Web.UI.UserControl
{
public static int readCount(String fielPath)
{
int count = ;
StreamReader sr = File.OpenText(fielPath) ;
while (sr.Peek() != -)
{
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 + ;
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 : addCount(Server.MapPath("Vote1.txt")); break;
case : addCount(Server.MapPath("Vote2.txt")); break;
case : 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();//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 : addCount(Server.MapPath("Vote1.txt")); break;
case : addCount(Server.MapPath("Vote2.txt")); break;
case : 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();//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="" cellpadding="" cellspacing="" runat="server" style="" >
<h2>查看投票结果</h2> <tr><td>序号</td>
<td>投票意见</td>
<td>票数</td>
<td>百分比</td>
</tr>
<tr><td></td>
<td>非常满意</td>
<td><%=vote1%></td>
<td><%=vote1percent%> % </td>
</tr>
<tr><td></td>
<td>比较满意</td>
<td><%=vote2%></td>
<td><%=vote2percent%> % </td>
</tr>
<tr><td></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 = ;
StreamReader sr = File.OpenText(fielPath);
while (sr.Peek() != -)
{
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) * / Convert.ToDouble(count)).ToString("0.00"); //将double型转换为string型。并保留2位小数点
vote2percent = (Convert.ToDouble(vote2) * / Convert.ToDouble(count)).ToString("0.00"); //将double型转换为string型。并保留2位小数点
vote3percent = (Convert.ToDouble(vote3) * / 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】最后发点牢骚---困扰程序运行的都是一些小的细节,以前老师总说:现在偷得懒以后总要还的,
现在真的被一些基础的细节问题扰的崩溃。写代码需要沉下心,再沉下心,忌心浮气躁。
《ASP.NET1200例》实现投票的用户控件的更多相关文章
- 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定
		
aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...
 - asp.net动态加载ascx用户控件
		
原文:asp.net动态加载ascx用户控件 在主aspx/ascx文件中,将目标ascx1,ascx2控件拖拉到其页面中,然后删除,目的是要生成:Register 代码,然后在主文件中定义DIV或T ...
 - ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用
		
在用户控件中,获取父页面的方法 1:方法没有参数(userInfor()) string userInfor = Convert.ToString(this.Page.GetType().GetMet ...
 - asp.net 动态添加多个用户控件
		
动态添加多个相同用户控件,并使每个用户控件获取不同的内容. 用户控件代码: 代码WebControls using System; using System.Collections.Generic; ...
 - (九)ASP.NET自定义用户控件(2)
		
http://www.cnblogs.com/SkySoot/archive/2012/09/04/2670678.html 用户控件 在 .NET 里,可以通过两种方式把自己的控件插入到 Web 窗 ...
 - ASP.NET MVC中加载WebForms用户控件(.ascx)
		
原文:ASP.NET MVC中加载WebForms用户控件(.ascx) 问题背景 博客园博客中的日历用的是ASP.NET WebForms的日历控件(System.Web.UI.WebControl ...
 - Asp.net 恢复页面内用户控件内的控件ClientID
		
众所周知在Asp.net中如果一个页面添加了一个用户控件(或母版页),那么用户控件内的控件的 ClientID号会被自动添加页面中用户控件的ClientID 即页面中的控件内的控件ClientID ...
 - 035. asp.netWeb用户控件之四通过用户控件实现投票和结果分析
		
用户控件Vote.ascx代码 <%@ Control Language="C#" AutoEventWireup="true" CodeFile=&qu ...
 - 039. asp.netWeb用户控件之七实现具有虚拟键盘的功能的用户控件
		
用户控件ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="K ...
 
随机推荐
- java.lang.IllegalStateException: getWriter() has already been called for this response问题解决
			
java.lang.IllegalStateException: getWriter() has already been called for this response问题解决 java.lang ...
 - Xcode7企业版打包
			
今天才发现Xcode7企业账号打包竟然和以前稍微不一样了,一时手残,先把公司服务器以前的ipa包删了,吓得我的小心脏呢 首先选中然后选archive然后点export然后然后选中include man ...
 - Java基础-String 存储机制管理
			
JVM运行的时候,将内存分为两个部分,一部分是堆,一部分是栈.堆中存放的是创建对象,而栈中存放的则是方法调用过程中的局部变量或引用.在设计JAVA字符串对象内存实现的时候,在堆中又开辟了一块很小的内存 ...
 - Spring 作用域 scope
			
spring的作用域将对Bean的生命周期和创建方式产生影响. 主要分为五种类型的作用域 singleton (默认)在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. ...
 - 图解Android - Android GUI 系统 (5) - Android的Event Input System
			
Android的用户输入处理 Android的用户输入系统获取用户按键(或模拟按键)输入,分发给特定的模块(Framework或应用程序)进行处理,它涉及到以下一些模块: Input Reader: ...
 - 【UVA 11401】Triangle Counting
			
题 题意 求1到n长度的n根棍子(3≤n≤1000000)能组成多少不同三角形. 分析 我看大家的递推公式都是 a[i]=a[i-1]+ ((i-1)*(i-2)/2-(i-1)/2)/2; 以i 为 ...
 - 什么是谷歌loon计划
			
互联网服务已经与人类的生活密不可分,但受地理环境限制,目前全球只有三分之一的幸运儿能够体验到这种服务.为了让更多的人感受互联网,Google推出了一项名为“Project Loon”的计划,利用氢气球 ...
 - 使用U盘安装mint
			
系统坏了,重新装的时候,硬盘甚至都没法格式化...所以,狠狠心买了块固态硬盘,123G,威刚. 想自己装Linux系统,这样用起来更方便一点,不用装虚拟机,然后再跑linux什么的.最后选了mint. ...
 - java导出txt文本
			
页面 项目结构 html代码 <html> </head> <body> <form action="down/downLoad" met ...
 - 采用get的方式提交数据到服务器
			
1 效果演示: