035. asp.netWeb用户控件之四通过用户控件实现投票和结果分析
用户控件Vote.ascx代码
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="vote.ascx.cs" Inherits="vote" %>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style1
{
font-size: small;
color: #0066CC;
}
</style>
<div id="box">
<table width="370" border="0">
<tr>
<td width="364" height="37" class="style1">您对本公司的售后服务是否满意?</td>
</tr>
<tr>
<td height="106">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" Font-Size="9pt">
<asp:ListItem Selected="True">非常满意</asp:ListItem>
<asp:ListItem>基本满意</asp:ListItem>
<asp:ListItem>不满意</asp:ListItem>
<asp:ListItem>不发表意见</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td height="30" align="center">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/dc_03.gif"
onclick="ImageButton1_Click" />
<img src="data:images/dc_05.gif" width="82" height="33" onclick="window.location.replace('result.aspx')" style="cursor:pointer"/></td>
</tr>
</table>
</div>
用户控件vote.ascx.cs代码:
public partial class vote : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{ }
/// <summary>
/// 从txt文件中读取投票数量
/// </summary>
/// <param name="P_str_path">要读取的txt文件的路径及名称</param>
/// <returns>返回一个int类型的值,用来记录投票数量</returns>
public static int readCount(string P_str_path)
{
int P_int_count = ;
StreamReader streamread;
streamread = File.OpenText(P_str_path);
while (streamread.Peek() != -)
{
P_int_count = int.Parse(streamread.ReadLine());
}
streamread.Close();
return P_int_count;
}
/// <summary>
/// 写入投票数量
/// </summary>
/// <param name="P_str_path">要操作的txt文件的路径及名称</param>
public static void addCount(string P_str_path)
{
int P_int_count = readCount(P_str_path);
P_int_count += ;
//将数据记录写入文件
StreamWriter streamwriter = new StreamWriter(P_str_path, false);
streamwriter.WriteLine(P_int_count);
streamwriter.Close();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string P_str_IP = Request.UserHostAddress.ToString();
HttpCookie oldCookie = Request.Cookies["userIP"];
if (oldCookie == null)
{
int flag = RadioButtonList1.SelectedIndex;
switch (flag)
{
case : addCount(Server.MapPath("result1.txt")); break;
case : addCount(Server.MapPath("result2.txt")); break;
case : addCount(Server.MapPath("result3.txt")); break;
case : addCount(Server.MapPath("result4.txt")); break;
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('投票成功,谢谢您的参与!');", true);
HttpCookie newCookie = new HttpCookie("userIP"); //定义新的Cookie对象
newCookie.Expires = DateTime.MaxValue;
//添加新的Cookie变量IPaddress,值为P_str_IP
newCookie.Values.Add("IPaddress", P_str_IP);
Response.AppendCookie(newCookie); //将变量写入Cookie文件中
}
else
{
string P_str_oldIP = oldCookie.Values["IPaddress"];
if (P_str_IP.Trim() == P_str_oldIP.Trim())
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('一个IP地址只能投一次票,谢谢您的参与!');", true);
}
else
{
HttpCookie newCookie = new HttpCookie("userIP");
newCookie.Values.Add("IPaddress", P_str_IP);
newCookie.Expires = DateTime.MaxValue;
Response.AppendCookie(newCookie);
int rflag = RadioButtonList1.SelectedIndex;
switch (rflag)
{
case : addCount("result1.txt"); break;
case : addCount("result2.txt"); break;
case : addCount("result3.txt"); break;
case : addCount("result4.txt"); break;
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('投票成功,谢谢您的参与!');", true);
}
}
}
}
结果页面result.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register src="vote.ascx" tagname="vote" tagprefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>在线投票系统</title>
</head>
<body>
<form id="form1" runat="server">
<uc1:vote ID="vote1" runat="server" />
</form>
</body>
</html>
结果页面result.aspx.cs代码:
public partial class Default2 : System.Web.UI.Page
{
protected string M_str_rate1;
protected string M_str_rate2;
protected string M_str_rate3;
protected string M_str_rate4;
protected int P_int_count1;
protected int P_int_count2;
protected int P_int_count3;
protected int P_int_count4;
protected void Page_Load(object sender, EventArgs e)
{
P_int_count1 = readCount(Server.MapPath("result1.txt"));
P_int_count2 = readCount(Server.MapPath("result2.txt"));
P_int_count3 = readCount(Server.MapPath("result3.txt"));
P_int_count4 = readCount(Server.MapPath("result4.txt"));
int P_int_count = P_int_count1 + P_int_count2 + P_int_count3+P_int_count4;
if (P_int_count == )
{
Response.Write("<script>alert('还没有人投过票!')</script>");
lblresult.Text = "共有0人参与投票";
}
else
{
M_str_rate1 = (Convert.ToDouble(P_int_count1) * / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
M_str_rate2 = (Convert.ToDouble(P_int_count2) * / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
M_str_rate3 = (Convert.ToDouble(P_int_count3) * / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
M_str_rate4 = (Convert.ToDouble(P_int_count4) * / Convert.ToDouble(P_int_count)).ToString("0.00") + "%";
lblresult.Text = "共有" + P_int_count.ToString() + "人参与投票";
}
}
/// <summary>
/// 从txt文件中读取投票数量
/// </summary>
/// <param name="P_str_path">要读取的txt文件的路径及名称</param>
/// <returns>返回一个int类型的值,用来记录投票数量</returns>
public static int readCount(string P_str_path)
{
int P_int_count = ;
StreamReader streamread;
streamread = File.OpenText(P_str_path);
while (streamread.Peek() != -)
{
P_int_count = int.Parse(streamread.ReadLine());
}
streamread.Close();
return P_int_count;
}
}
默认页面Default.aspx页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register src="vote.ascx" tagname="vote" tagprefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>在线投票系统</title>
</head>
<body>
<form id="form1" runat="server">
<uc1:vote ID="vote1" runat="server" />
</form>
</body>
</html>
style.css代码:
#box {
height: 195px;
width: 370px;
background-image: url(images/1.jpg);
background-repeat: no-repeat;
padding-top: 65px;
padding-right: 45px;
padding-left: 45px;
}
#box2 {
height: 195px;
width: 370px;
background-image: url(images/2.jpg);
background-repeat: no-repeat;
padding-top: 65px;
padding-right: 45px;
padding-left: 45px;
}
用到的四个文件:

用到的几张图片:





最终效果展示:


035. asp.netWeb用户控件之四通过用户控件实现投票和结果分析的更多相关文章
- 033. asp.netWeb用户控件之二将页面转换成web控件和使用Web控件显示热点新闻
访问Web用户控件的属性 ASP.NET提供的各种服务器控件都有其自身的属性和方法,程序开发人员可以灵活地使用服务器控件中的属性和方法开发程序.在用户控件中,程序开发人员也可以自行定义各种属性和方法, ...
- ASP.NET MVC中加载WebForms用户控件(.ascx)
原文:ASP.NET MVC中加载WebForms用户控件(.ascx) 问题背景 博客园博客中的日历用的是ASP.NET WebForms的日历控件(System.Web.UI.WebControl ...
- 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- JS获取用户控件中的子控件Id
用户控件 <asp:HiddenField ID="hfGradeId" runat="server" /> <asp:HiddenField ...
- asp.net web 开发登录相关操作的控件LoginName、LoginStatus和LoginView控件使用详解
http://book.51cto.com/art/200909/154039.htm http://book.51cto.com/art/200909/154041.htm ASP.NET提供了一套 ...
- ASP.NET MVC显示WebForm网页或UserControl控件
ASP.NET MVC显示WebForm网页或UserControl控件 学习与使用ASP.NET MVC这样久,还是对asp.net念念不忘.能否在asp.net mvc去显示aspx或是user ...
- 念念不忘,ASP.NET MVC显示WebForm网页或UserControl控件
学习与使用ASP.NET MVC这样久,还是对asp.net念念不忘.能否在asp.net mvc去显示aspx或是user control呢?这个灵感(算不上灵感,只能算是想法)是来自前些天有写过一 ...
- 【Asp.net之旅】--因自己定义控件注冊而引发的思考
前言 近期在开发远洋的SOA系统平台,开发使用的是.NET平台.对于Asp.net并不困难,但该系统的开发并非全然依靠Asp.net.而是自身封装好的框架.这套框架是远洋地产购买的微软的开发平台,项目 ...
- asp.net 弹出式日历控件 选择日期 Calendar控件
原文地址:asp.net 弹出式日历控件 选择日期 Calendar控件 作者:逸苡 html代码: <%@ Page Language="C#" CodeFile=&quo ...
随机推荐
- Linux 远程桌面 访问 WIndows
1. Debain 系列 linux sudo aptitude install rdesktop 2. Connect rdesktop <hostname> -r sound:off ...
- 用 python 实现各种排序算法(转)
常见几种排序的算法: 归并排序 归并排序也称合并排序,是分治法的典型应用.分治思想是将每个问题分解成个个小问题,将每个小问题解决,然后合并. 具体的归并排序就是,将一组无序数按n/2递归分解成只有一个 ...
- MYSQL常见错误及其解决方式
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- 作用域链–JS基础核心之一
JS中的作用域,大家都知道的,分为全局作用域和局部作用域,没有块级作用域,听起来其实很简单的,可是作用域是否能够有深入的了解,对于JS代码逻辑的编写成功率,BUG的解决能力,以及是否能写出更优秀的代码 ...
- ABAP之声母韵母
我们一开始上学的时候,老师最先教的是什么? 拼音,声母,韵母,声调等等. 那么ABAP里什么是这些东西呢? 基础的数据类型,已经数据字典里的东西:域,数据元素,结构,视图,表,搜索帮助,锁... 数据 ...
- AngularJS: 'Template for directive must have exactly one root element' when using 'th' tag in directive template
.controller('HomeController', function($scope,$location) { $scope.userName='天下大势,为我所控!'; $scope.clkU ...
- ADO.NET 增、删、改、查
ADO.NET:数据访问技术 就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中也可以将数据库中的数据提取到内存中供程序调用 所有数据访问技术的基础 连接 ...
- java写hadoop全局排序
前言: 一直不会用java,都是streaming的方式用C或者python写mapper或者reducer的可执行程序.但是有些情况,如全排序等等用streaming的方式往往不好处理,于是乎用原生 ...
- js检测浏览器屏幕宽度
使用javascript脚本编写的一个能检测浏览器屏幕的宽度,当改变浏览器屏幕大小时,输出的数值也会随之改变.
- iOS开发网络篇—网络编程基础
iOS开发网络篇—网络编程基础 一.为什么要学习网络编程 1.简单说明 在移动互联网时代,移动应用的特征有: (1)几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图 (2)只有通过 ...