All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
用C#和ADO.NET建立一个数据绑定网格_[Asp.Net教程]
Visual,C#程序员面试基础问题和答案_[Asp.Net教程]
SQL,Server不能启动的常见故障_[SQL,Server教程]
一款DIV+CSS导航条效果_JavaScript技术_编程技术
ASP.Net留言本:用ASP.Net和Access编写留言本_[Asp.Net教程]
完全讲解PHP+MySQL的分页显示示例分析_[PHP教程]
关于ASP.NET,2.0的目录结构变化_[Asp.Net教程]
ASP.net实现无扩展名的URL重写。简单、方便、无需ISAPI_[Asp.Net教程]
Delphi以XPManifest组件显示界面
关于ASP.NET在IIS一些问题的经验总结_[Asp.Net教程]
Reading number is top 10 pictures
Breasts woman big set 1
Forced sex girl living abroad2
In 2013 hercules Arnold classic1
两个妞在等世界上最短的火车
Angie Chiu vijara myth1
The little girl with long hair1
A man's favorite things5
The girl of like self-time
mythology hero1
非常漂亮的泳装美女
Download software ranking
Tram sex maniac 2 (H) rar bag1
Tram sex maniac 2 (H) rar bag12
Boxer's Top ten classic battle8
VeryCD电驴(EasyMule) V1.1.9 Build09081
Boxer's Top ten classic battle9
Love the forty days
在线棋牌游戏3.05版
Call Of Duty2
Boxer vs Yellow5
asp.net技术内幕
qq published in(发表于) 2014/7/9 1:36:11 Edit(编辑)
asp.net2.0中Cookie对象的应用实例

asp.net2.0中Cookie对象的应用实例

asp.net2.0中Cookie对象的应用实例





Cookie对象的应用



本节通过一个简单的实例来介绍如何使用Cookie对象实现一个简单的网上投票系统。实例运行结果如图1和图2所示。







图1 在线投票页面







图2 查看投票结果页面



程序开发步骤如下。



(1)新建一个网站,命名为15_06,默认主页名为Default.aspx。



(2)在Default.aspx页面中添加一个Table表格,用来布局页面,然后,在该Table表格中添加一个RadioButtonList控件和两个Button控件,分别用来供用户选择投票、执行投票操作和查看投票结果功能。



(3)在该网站中添加一个Web页面,命名为Default2.aspx,该页面主要用来显示投票结果,然后在该网站的虚拟目录下新建4个记事本文件count1.txt、count2.txt、count3.txt和count4.txt,它们分别用来记录各投票选项的投票数量。



(4)程序主要代码。



为了提高代码的重用率,本系统在实现各功能之前,首先新建了一个公共类文件count.cs,该类主要用来对txt文本文件进行读取和写入操作。在count.cs类文件中,定义了两个方法readCount和addCount,其中,readCount方法用来从txt文件中读取投票数量,addCount方法用来向txt文本文件中写入投票数量。readCount方法实现代码如下:



#region 从txt文件中读取投票数量



///





/// 从txt文件中读取投票数量



///




/// 要读取的txt文件的路径及名称



/// 返回一个int类型的值,用来记录投票数量



public static int readCount(string P_str_path)



{



int P_int_count = 0;



StreamReader streamread;



streamread = File.OpenText(P_str_path);



while (streamread.Peek() != -1)



{



P_int_count = int.Parse(streamread.ReadLine());



}



streamread.Close();



return P_int_count;



}



#endregion



addCount方法实现代码如下:



#region 写入投票数量



///



/// 写入投票数量



///




/// 要操作的txt文件的路径及名称



public static void addCount(string P_str_path)



{



int P_int_count = readCount(P_str_path);



P_int_count += 1;



//将数据记录写入文件



StreamWriter streamwriter = new StreamWriter(P_str_path, False);



streamwriter.WriteLine(P_int_count);



streamwriter.Close();



}



#endregion



页面Default.aspx中,单击【投票】按钮,程序首先判断用户是否已投过票,如果用户已经投过票,则弹出信息提示框,否则,利用Cookie对象保存用户的IP地址,并弹出对话框提示用户投票成功。【投票】按钮的Click事件代码如下:



protected void Button1_Click(object sender, EventArgs e)



{



string P_str_IP = Request.UserHostAddress.ToString();



HttpCookie oldCookie = Request.Cookies["userIP"];



if (oldCookie == null)



{



if (RadioButtonList1.SelectedIndex == 0)



{



count.addCount(Server.MapPath("count1.txt"));



}



if (RadioButtonList1.SelectedIndex == 1)



{



count.addCount(Server.MapPath("count2.txt"));



}



if (RadioButtonList1.SelectedIndex == 2)



{



count.addCount(Server.MapPath("count3.txt"));



}



if (RadioButtonList1.SelectedIndex == 3)



{



count.addCount(Server.MapPath("count4.txt"));



}



Response.Write("");



//定义新的Cookie对象



HttpCookie newCookie = new HttpCookie("userIP");



newCookie.Expires = DateTime.MaxValue;



//添加新的Cookie变量IPaddress,值为P_str_IP



newCookie.Values.Add("IPaddress", P_str_IP);



//将变量写入Cookie文件中



Response.AppendCookie(newCookie);



}



else



{



string P_str_oldIP = oldCookie.Values["IPaddress"];



if (P_str_IP.Trim() == P_str_oldIP.Trim())



{



Response.Write("");



}



else



{



HttpCookie newCookie = new HttpCookie("userIP");



newCookie.Values.Add("IPaddress", P_str_IP);




















添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.