DIV弹出窗口控件
在项目开发中,很多地方都使用了window.open 和window.showdialog两种弹出窗口模式。刚开始看公司老的一些项目里都是用这些的,自己也就跟着用,后来感觉每个页面都写JS,太麻烦了,即使是COPY一下,也觉得不爽,而且老是用这两种,感觉没什么意思,难看,能不能用个好看点的,呵呵。看来我对美好的事物还是有追求的。
在网络上找到了一个不错的DIV弹出窗口,还算漂亮吧
网址是 http://www.subimage.com/dhtml/subModal
对象是找好了,接下就是给它弄点嫁妆,尤其是衣裳,好好的修饰打扮下才能出来见人啊
大多数的弹出窗口都是要有返回值的,所以决定控件的模样是一个textbox 加两个图片按钮如图
一个是选择,另外一个清空按钮
开始先写这个复合控件
在网络上找到了一个不错的DIV弹出窗口,还算漂亮吧
网址是 http://www.subimage.com/dhtml/subModal
对象是找好了,接下就是给它弄点嫁妆,尤其是衣裳,好好的修饰打扮下才能出来见人啊
大多数的弹出窗口都是要有返回值的,所以决定控件的模样是一个textbox 加两个图片按钮如图
一个是选择,另外一个清空按钮
开始先写这个复合控件
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- using System.ComponentModel;
- namespace StarTech.WebControls
- {
- [ParseChildren(true)]
- [DefaultValue("Text"), ToolboxData("<{0}:STSingleCarSelect runat='server' Width='150px' ></{0}:STSingleCarSelect>")]
- public class STSingleCarSelect : BaseFormControl
- {
- private TextBox _textbox;
- private HiddenField _hiddenField;
- private HtmlImage _image;
- private HtmlImage _delimage = new HtmlImage();
- private bool _requiredfield = false;
- private int _intTextBoxWidth = 100;
- private int _intDiglogHeight = 500;
- private int _intDiglogWidth = 700;
- private string _strDiglogTitle = "车辆列表";
- private string _sourceUrl = "~/ControlSource/Dialog/SingleCarListSelect.aspx";
- 属性#region 属性
- [Description("文本框Text"), Category("ST属性")]
- public string Text
- {
- get
- {
- EnsureChildControls();
- return _textbox.Text;
- }
- set
- {
- EnsureChildControls();
- _textbox.Text = value;
- }
- }
- [Description("文本框ID"), Category("ST属性")]
- public string Value
- {
- get
- {
- EnsureChildControls();
- return _hiddenField.Value;
- }
- set
- {
- EnsureChildControls();
- _hiddenField.Value = value;
- }
- }
- [Description("是否必填"), Category("ST属性")]
- public bool RequiredField
- {
- get { return _requiredfield; }
- set { _requiredfield = value; }
- }
- private string RegName
- {
- get
- {
- return "RegSingleCarScript";
- }
- }
- [Description("文本框宽度"), Category("ST属性")]
- public int TextBoxWidth
- {
- get { return _intTextBoxWidth; }
- set { _intTextBoxWidth = value; }
- }
- [Description("弹出窗体高度"), Category("ST属性")]
- public int DiglogHeight
- {
- get { return _intDiglogHeight; }
- set { _intDiglogHeight = value; }
- }
- [Description("弹出窗体宽度"), Category("ST属性")]
- public int DiglogWidth
- {
- get { return _intDiglogWidth; }
- set { _intDiglogWidth = value; }
- }
- public string DiglogTitle
- {
- get { return _strDiglogTitle; }
- set { _strDiglogTitle = value; }
- }
- #endregion
- protected override void OnPreRender(EventArgs e)
- {
- this.Page.ClientScript.RegisterClientScriptResource(typeof(JsCommon), "StarTech.WebControls.ControlSource.JScript.common.js");
- this.Page.ClientScript.RegisterClientScriptResource(typeof(JsSubModal), "StarTech.WebControls.ControlSource.JScript.subModal.js");
- string resource = Page.ClientScript.GetWebResourceUrl(this.GetType(), "StarTech.WebControls.ControlSource.Css.subModal.css");
- string csslink = "<link href='" + resource + "' rel='stylesheet' type='text/css' />";
- Page.Header.Controls.Add(new LiteralControl(csslink));
- if (!this.Page.ClientScript.IsStartupScriptRegistered(this.RegName))
- {
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.RegName, this.RegSouceScript(), false);
- }
- base.OnPreRender(e);
- }
- protected override void AddAttributesToRender(HtmlTextWriter writer)
- {
- EnsureChildControls();
- _image.Attributes.Add("onclick", "OpenPopWinSingleCar()");
- _delimage.Attributes.Add("onclick", "ClearSingleCar()");
- base.AddAttributesToRender(writer);
- }
- protected override void CreateChildControls()
- {
- Controls.Clear();
- _textbox = new TextBox();
- _textbox.ID = _textbox.UniqueID;
- _textbox.CssClass = CssClass;
- _textbox.TextMode = TextBoxMode.SingleLine;
- _textbox.Attributes["Style"] = "border:1 solid gray;";
- _textbox.Width = Unit.Pixel(_intTextBoxWidth);
- _textbox.Height = this.Height;
- Controls.Add(_textbox);
- //空格
- Controls.Add(new LiteralControl(" "));
- //选择图片
- _image = new HtmlImage();
- _image.ID = _image.UniqueID;
- _image.Alt = "选 择";
- _image.Src = Page.ClientScript.GetWebResourceUrl(this.GetType(), "StarTech.WebControls.ControlSource.Images.application_add.png");
- _image.Style.Value = "CURSOR:hand;";
- Controls.Add(_image);
- //空格
- Controls.Add(new LiteralControl(" "));
- //删除
- _delimage.ID = _delimage.UniqueID;
- _delimage.Src = Page.ClientScript.GetWebResourceUrl(this.GetType(), "StarTech.WebControls.ControlSource.Images.application_delete.png");
- _delimage.Style.Value = "CURSOR:hand;";
- _delimage.Alt = "删 除";
- Controls.Add(_delimage);
- //隐藏控件
- _hiddenField = new HiddenField();
- Controls.Add(_hiddenField);
- if (_requiredfield)
- {
- this.Controls.Add(new LiteralControl("<font color=red>*</font>"));
- RequiredFieldValidator validator = new RequiredFieldValidator();
- validator.ControlToValidate = _textbox.ID;
- validator.Text = "必填";
- validator.Font.Size = 9;
- validator.Display = ValidatorDisplay.Dynamic;
- if (this.ErrorMessage != null && this.ErrorMessage != "")
- {
- validator.ErrorMessage = this.ErrorMessage;
- }
- else
- {
- validator.ErrorMessage = "必填";
- }
- this.Controls.Add(validator);
- }
- base.CreateChildControls();
- }
- /**//// <summary>
- /// 实现脚本
- /// </summary>
- /// <returns></returns>
- private string RegSouceScript()
- {
- JavaScriptWriter js = new JavaScriptWriter(true);
- js.AddLine(" rnd.today=new Date(); ");
- js.AddLine(" rnd.seed=rnd.today.getTime(); ");
- js.AddLine(" function rnd() { ");
- js.AddLine(" rnd.seed = (rnd.seed*9301+49297) % 233280; ");
- js.AddLine(" return rnd.seed/(233280.0); ");
- js.AddLine(" }; ");
- js.AddLine(" function rand(number) { ");
- js.AddLine(" return Math.ceil(rnd()*number); ");
- js.AddLine(" }; ");
- js.AddLine(" function AlertMessageBoxSingleCar(returnValue)");
- js.AddLine(" {");
- js.AddLine(" var indexvalue;");
- js.AddLine(" if (returnValue!=undefined){ ");
- js.AddLine(" var txtname=document.getElementById('" + _textbox.ClientID + "');");
- js.AddLine(" var txtid=document.getElementById('" + _hiddenField.ClientID + "');");
- js.AddLine(" indexvalue=returnValue.indexOf('|');");
- js.AddLine(" txtname.value = returnValue.substring(0,indexvalue);");
- js.AddLine(" txtid.value = returnValue.substring(indexvalue+1);");
- js.AddLine(" }");
- js.AddLine(" }");
- js.AddLine(" function OpenPopWinSingleCar()");
- js.AddLine(" {");
- js.AddLine(" showPopWin('" + _strDiglogTitle + "','" + ResolveUrl(_sourceUrl) + "?'+rand(10000000), " + _intDiglogWidth + ", " + _intDiglogHeight + ", AlertMessageBoxSingleCar,true,true);");
- js.AddLine(" }");
- js.AddLine(" function ClearSingleCar()");
- js.AddLine(" { ");
- js.AddLine(" document.all." + _textbox.ClientID + ".value=/"/";");
- js.AddLine(" document.all." + _hiddenField.ClientID + ".value=/"/";");
- js.AddLine(" }");
- return js.ToString();
- }
- }
- }
让它可以在外部定义。
接下来就是如果获得源页面的返回值,具体代码如下
//
返回值
window.returnVal = " nosnowwolf " ;
// 关闭窗口
window.parent.hidePopWin( true );
window.returnVal = " nosnowwolf " ;
// 关闭窗口
window.parent.hidePopWin( true );
OK,这样就可以了,可能我表达的不是很清楚,希望大家能给点意见,改天把代码放出来
出自一个同事的BLOG http://www.cnblogs.com/nosnowwolf/articles/1232844.html