---
title: "C#实现仿msn提示消息在登录后只弹出一次的效果(downmoon)"
author: "netxdiy"
date: "2007-06-13T09:13:00Z"
tags: []
slug: "c-msn-downmoon"
draft: false
lastmod: "2012-09-27T11:33:14Z"
id: 1687125
url: "https://netxdiy.iteye.com/blog/1687125"
---

在 [**codeproject**](http://www.codeproject.com/KB/custom-controls/asppopup.aspx) 上看到一个很不错的控件，完全能够自定义弹出窗口的样式、内容、链接，该控件支持Mozilla , Internet Explorer 和 Opera ，我试用了一下，效果非常不错，推荐大家使用。

- [Download demo project (C# and VB.NET) - 38.1 Kb](http://www.codeproject.com/KB/custom-controls/asppopup/demo.zip)
- [Download control with source - 37.7 Kb](http://www.codeproject.com/KB/custom-controls/asppopup/source.zip)
- [Download control documentation - 71.1 Kb](http://www.codeproject.com/KB/custom-controls/asppopup/doc.zip)

<!--THE END-->

- [Open online sample](http://www.eeeksoft.net/files/popuptest) \[[^](http://www.eeeksoft.net/files/popuptest) ]. &lt;!-- {ps..0}--&gt;

下面主要介绍该控件结合cookie实现登录后只提醒一次的功能！  
一、定义变量}

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)members #region members  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) protected AgronetControl.PopupWinpwVegnet;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private string downmoontest = " downmoontest " ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) #endregion

二、按钮事件

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)Events #region Events  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private void Page\_Load( object sender,System.EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.LinkClicked += new EventHandler(pwVegnet\_LinkClicked);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) this .pwVegnet.Visible = false ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if ( ! IsPostBack)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) bool bl = IsShowMessage();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if (bl)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) SetMessage();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) setCookies(downmoontest, " yes " , 1 );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) this .pwVegnet.Visible = false ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) /\*\*/ /// &lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) /// 注销按钮  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) /// &lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) /// &lt;paramname="sender"&gt;&lt;/param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) /// &lt;paramname="e"&gt;&lt;/param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private void btnLogOff\_Click( object sender,System.EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) RemoveAll();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private void pwVegnet\_LinkClicked( object sender,EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 此处可以加上"下次是否显示之类的脚本"---downmoon  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Page.Response.Redirect( @" http://www.vegnet.com.cn/user/ " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }

三、主要方法：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)Methods #region Methods  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private void SetMessage()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.HideAfter = 5000 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.Visible = true ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.Title = textTitle.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.Message = textMsg.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.Text = textFull.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.DragDrop = true ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.OffsetX = 30 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.OffsetY = 15 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.DockMode = AgronetControl.PopupDocking.BottomRight;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 随机弹出窗口样式  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) int t = GetRandomID( 1 , 4 );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) switch (t)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) case 1 :pwVegnet.ColorStyle = AgronetControl.PopupColorStyle.Red; break ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) case 2 :pwVegnet.ColorStyle = AgronetControl.PopupColorStyle.Green; break ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) case 3 :pwVegnet.ColorStyle = AgronetControl.PopupColorStyle.Blue; break ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) case 4 :pwVegnet.ColorStyle = AgronetControl.PopupColorStyle.Violet; break ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) default :pwVegnet.ColorStyle = AgronetControl.PopupColorStyle.Green; break ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) pwVegnet.Link = @" http://www.vegnet.com.cn " ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private bool IsShowMessage()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) string str = getCookies(downmoontest);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if (str == string .Empty || str.ToLower() == " no " )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return true ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return false ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public void MsgBox( string strDebug)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Page.Response.Write( " &lt;scriptlanguage=javascript&gt;alert(' " + strDebug.Replace( " ' " , "" ).Replace( " " , "" ) + " ');&lt;/script&gt; " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // Page.Response.End();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public static string RandomKey( int b, int e)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return DateTime.Now.ToString( " yyyyMMdd-HHmmss-fff- " ) + GetRandomID(b,e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public static int GetRandomID( int minValue, int maxValue)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Randomri = new Random( unchecked (( int )DateTime.Now.Ticks));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) int k = ri.Next(minValue,maxValue);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return k;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) 设置cookie #region 读cookie  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private void setCookies( string cook, string valuee, int num)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // Response.Cookies.Clear();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) HttpCookiecookie;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) cookie = Request.Cookies\[cook];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if (cookie == null )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) cookie = new HttpCookie(cook);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) cookie.Value = valuee;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) cookie.Expires = DateTime.Now.AddDays(num);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) HttpContext.Current.Response.Cookies.Add(cookie);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) cookie.Value = valuee;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) #endregion  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) 读cookie #region 写cookie  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private string getCookies( string cook)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) HttpCookiereadcookie = Request.Cookies\[cook];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if (readcookie != null && readcookie.Value != null )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return readcookie.Value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) return string .Empty;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) #endregion  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) 去除SESSION/注销 #region 去除SESSION/注销  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) /\*\*/ /// &lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) /// 去除SESSION/注销  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) /// &lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public void RemoveAll()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) HttpCookieCollectioncooks = HttpContext.Current.Response.Cookies;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) cooks\[downmoontest].Expires = new DateTime( 1999 , 10 , 12 );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) HttpContext.Current.Response.Cookies.Add(cooks\[downmoontest]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) #endregion  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) #endregion

实现效果：

![邀月工作室](http://kdpwlq.blu.livefilestore.com/y1p3CiJ3xiGo4O6KESwoIt-e1h-D3rMyZNQJtXwDtgo7eTrACMdmpdIjfYjZ3Hd5MmiBY7Q7y3MGIkSE3NYN3Sj5A/%E5%95%86%E5%8A%A1%E5%AE%A4%E6%B6%88%E6%81%AF.jpg)

**邀月注：本文版权由邀月 和CSDN共同所有，转载请注明出处。**  
**助人等于自助! 3w@live.cn**