最近因为工作需要我研究了下类似MSN的消息提示框方面的程序,发现BEN的PopupBox还不错
但在asp.net 2.0下运行时弹出的消息框是却是一片白色...没任何内容,
因为没源代码,所以我只好用reflector获取了源代码
并修改了代码里的小BUG,以适应asp.net 2.0
关键代码我贴出来,有需要研究的朋友可去看看,并可点击下载修改后的源码,内附简单调用方法 (下载地址)
using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Reflection; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls;
namespace benSoft.web.WebControls.PopupBox { [Designer(typeof(PopupBoxDesigner)), PersistChildren(false), ToolboxData("<{0}:PopupBox runat=server></{0}:PopupBox>"), DefaultProperty("Text"), ParseChildren(false)] public class PopupBox : Panel { private bool _Focus = false; private Unit _height = Unit.Pixel(120); private string _sound; private string _text = ""; private int _timer = 3; private Unit _width = Unit.Pixel(200); private string ScriptKey = "benSoft.web.WebControls.PopupBox"; private string ScriptParams = "/r/n/t/t<script language=javascript>/r/n/t/t/t/**//t/t /r/n/t/t/tvar tmpHtmlTemplate;/r/n/t/t/tvar w = parseInt('{1}');/t//box's width/r/n/t/t/tvar h = parseInt('{2}');/t//box's height/r/n/t/t/tvar sound = /"{3}/";/r/n/t/t/tvar wFocus = {4};/r/n/t/t/tvar dtime = {5};/r/n /t /r/n/t/t/ttmpHtmlTemplate = document.getElementById(/"{0}tmpHtmlTemplate/"); /r/n/t/t</script>/r/n/t/t";
public void CreatePopup() { Label label; HtmlInputHidden hiddens; Assembly executingAssembly = Assembly.GetExecutingAssembly(); string str = ""; string str2 = this.Context.Request.Url.ToString(); str2 = str2 + ((str2.IndexOf("?") >= 0) ? "&" : "?") + this.UniqueMark; Stream manifestResourceStream = executingAssembly.GetManifestResourceStream("benSoft.web.WebControls.PopupBox.dialog.htm"); manifestResourceStream.Position = 0L; str = new StreamReader(manifestResourceStream).ReadToEnd().Replace("[bgimagepath]", str2 + "bgImage=true").Replace("[imagepath]", str2 + "Image=true"); Panel panel = new Panel(); panel.Attributes["Style"] = string.Format("font-size:{0};color:{1};font-family:{2}", this.Font.Size, this.ForeColor, this.Font.Name); panel.Controls.Add(new LiteralControl(this.Text)); if (this.HorizontalAlign != HorizontalAlign.NotSet) { panel.Attributes["align"] = this.HorizontalAlign.ToString(); } else { panel.Attributes["align"] = "center"; } StringWriter writer = new StringWriter(); HtmlTextWriter writer2 = new HtmlTextWriter(writer); panel.RenderControl(writer2); if (base.FindControl(this.preMark + this.UniqueMark + "tmpLabel") == null) { label = new Label(); } else { label = (Label)base.FindControl(this.preMark + this.UniqueMark + "tmpLabel"); } label.ID = this.preMark + this.UniqueMark + "tmpLabel"; label.Attributes["Style"] = "display:none"; label.Text = writer.ToString(); base.Controls.Add(label); str = str.Replace("[MessageParam]", this.preMark + this.UniqueMark + "tmpLabel"); if (base.FindControl(this.preMark + this.UniqueMark + "tmpHtmlTemplate") == null) { hiddens = new HtmlInputHidden(); } else { hiddens = (HtmlInputHidden)base.FindControl(this.preMark + this.UniqueMark + "tmpHtmlTemplate"); } hiddens.ID = this.preMark + this.UniqueMark + "tmpHtmlTemplate"; hiddens.Value = HttpUtility.UrlEncode(str).Replace("+", "%20"); base.Controls.Add(hiddens); Stream stream = executingAssembly.GetManifestResourceStream("benSoft.web.WebControls.PopupBox.script.htm"); stream.Position = 0L; string script = new StreamReader(stream).ReadToEnd(); base.Page.ClientScript.RegisterStartupScript(this.GetType(),this.ScriptKey + "ScriptParams", string.Format(this.ScriptParams, new object[] { this.preMark + this.UniqueMark, this.Width, this.Height, this.Sound, this.Focus.ToString().ToLower(), this.Timer })); base.Page.ClientScript.RegisterStartupScript(this.GetType(),this.ScriptKey + "Scripts", script); base.Page.ClientScript.RegisterStartupScript(this.GetType(),this.ScriptKey + "Popup", "<script language=javascript>Popup();</script>"); }
protected override void OnLoad(EventArgs e) { Assembly executingAssembly = Assembly.GetExecutingAssembly(); if ((this.Context.Request[this.UniqueMark + "Image"] != null) || (this.Context.Request["$Ben$DesignImage"] != null)) { Bitmap bitmap = new Bitmap(executingAssembly.GetManifestResourceStream("benSoft.web.WebControls.PopupBox.branding_Full2.gif")); this.Context.Response.Clear(); bitmap.Save(this.Context.Response.OutputStream, ImageFormat.Gif); bitmap.Dispose(); this.Context.Response.End(); } else if ((this.Context.Request[this.UniqueMark + "bgImage"] != null) || (this.Context.Request["$Ben$DesignbgImage"] != null)) { Bitmap bitmap2 = new Bitmap(executingAssembly.GetManifestResourceStream("benSoft.web.WebControls.PopupBox.headerGRADIENT_Tall.gif")); this.Context.Response.Clear(); bitmap2.Save(this.Context.Response.OutputStream, ImageFormat.Gif); bitmap2.Dispose(); this.Context.Response.End(); } }
[DefaultValue(false), Category("Popup Box"), Bindable(true), Description("PopupBox 弹出时, 窗口是否获取焦点")] public new bool Focus { get { return this._Focus; } set { this._Focus = value; } }
[Description("Popup Box's Height")] public override Unit Height { get { return this._height; } set { this._height = value; } }
[Browsable(false), Bindable(false), Category("Popup Box"), ReadOnly(true)] public string PageName { get { string absolutePath = this.Context.Request.Url.AbsolutePath; return absolutePath.Substring(absolutePath.LastIndexOf("/") + 1, (absolutePath.Length - absolutePath.LastIndexOf("/")) - 1); } }
private string preMark { get { string clientID = base.NamingContainer.ClientID; clientID = (clientID == null) ? "" : clientID; return (clientID + ((clientID == "") ? "" : "_")); } }
[Description("Popup 声音文件路径"), Category("Popup Box"), DefaultValue(""), Bindable(true)] public string Sound { get { return this._sound; } set { this._sound = value; } }
[Description("Popup Box 的文本"), Bindable(true), Category("Popup Box"), DefaultValue("")] public string Text { get { return this._text; } set { this._text = value; } }
[Description("PopupBox 消失定时器, 单位为秒"), DefaultValue(3), Bindable(true), Category("Popup Box")] public int Timer { get { return this._timer; } set { this._timer = value; } }
public string UniqueMark { get { return base.ClientID; } }
public override bool Visible { get { return true; } }
[Description("Popup Box's Width")] public override Unit Width { get { return this._width; } set { this._width = value; } } } }