DIV弹出窗口控件

DIV弹出窗口控件

在项目开发中,很多地方都使用了window.open 和window.showdialog两种弹出窗口模式。刚开始看公司老的一些项目里都是用这些的,自己也就跟着用,后来感觉每个页面都写JS,太麻烦了,即使是COPY一下,也觉得不爽,而且老是用这两种,感觉没什么意思,难看,能不能用个好看点的,呵呵。看来我对美好的事物还是有追求的。
 在网络上找到了一个不错的DIV弹出窗口,还算漂亮吧
网址是 http://www.subimage.com/dhtml/subModal
对象是找好了,接下就是给它弄点嫁妆,尤其是衣裳,好好的修饰打扮下才能出来见人啊
大多数的弹出窗口都是要有返回值的,所以决定控件的模样是一个textbox 加两个图片按钮如图
一个是选择,另外一个清空按钮
开始先写这个复合控件
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using System.Web.UI.HtmlControls;
  7. using System.ComponentModel;
  8. namespace StarTech.WebControls
  9. {
  10.     [ParseChildren(true)]
  11.     [DefaultValue("Text"), ToolboxData("<{0}:STSingleCarSelect runat='server' Width='150px' ></{0}:STSingleCarSelect>")]
  12.     public class STSingleCarSelect : BaseFormControl
  13.     {
  14.         private TextBox _textbox;
  15.         private HiddenField _hiddenField;
  16.         private HtmlImage _image;
  17.         private HtmlImage _delimage = new HtmlImage();
  18.         private bool _requiredfield = false;
  19.         private int _intTextBoxWidth = 100;
  20.         private int _intDiglogHeight = 500;
  21.         private int _intDiglogWidth = 700;
  22.         private string _strDiglogTitle = "车辆列表";
  23.         private string _sourceUrl = "~/ControlSource/Dialog/SingleCarListSelect.aspx";
  24.         属性#region 属性
  25.         [Description("文本框Text"), Category("ST属性")]
  26.         public string Text
  27.         {
  28.             get
  29.             {
  30.                 EnsureChildControls();
  31.                 return _textbox.Text;
  32.             }
  33.             set
  34.             {
  35.                 EnsureChildControls();
  36.                 _textbox.Text = value;
  37.             }
  38.         }
  39.         [Description("文本框ID"), Category("ST属性")]
  40.         public string Value
  41.         {
  42.             get
  43.             {
  44.                 EnsureChildControls();
  45.                 return _hiddenField.Value;
  46.             }
  47.             set
  48.             {
  49.                 EnsureChildControls();
  50.                 _hiddenField.Value = value;
  51.             }
  52.         }
  53.         [Description("是否必填"), Category("ST属性")]
  54.         public bool RequiredField
  55.         {
  56.             get { return _requiredfield; }
  57.             set { _requiredfield = value; }
  58.         }
  59.         private string RegName
  60.         {
  61.             get
  62.             {
  63.                 return "RegSingleCarScript";
  64.             }
  65.         }
  66.         [Description("文本框宽度"), Category("ST属性")]
  67.         public int TextBoxWidth
  68.         {
  69.             get { return _intTextBoxWidth; }
  70.             set { _intTextBoxWidth = value; }
  71.         }
  72.         [Description("弹出窗体高度"), Category("ST属性")]
  73.         public int DiglogHeight
  74.         {
  75.             get { return _intDiglogHeight; }
  76.             set { _intDiglogHeight = value; }
  77.         }
  78.         [Description("弹出窗体宽度"), Category("ST属性")]
  79.         public int DiglogWidth
  80.         {
  81.             get { return _intDiglogWidth; }
  82.             set { _intDiglogWidth = value; }
  83.         }
  84.         public string DiglogTitle
  85.         {
  86.             get { return _strDiglogTitle; }
  87.             set { _strDiglogTitle = value; }
  88.         }
  89.         #endregion
  90.         protected override void OnPreRender(EventArgs e)
  91.         {
  92.             this.Page.ClientScript.RegisterClientScriptResource(typeof(JsCommon), "StarTech.WebControls.ControlSource.JScript.common.js");
  93.             this.Page.ClientScript.RegisterClientScriptResource(typeof(JsSubModal), "StarTech.WebControls.ControlSource.JScript.subModal.js");
  94.             string resource = Page.ClientScript.GetWebResourceUrl(this.GetType(), "StarTech.WebControls.ControlSource.Css.subModal.css");
  95.             string csslink = "<link href='" + resource + "' rel='stylesheet' type='text/css' />";
  96.             Page.Header.Controls.Add(new LiteralControl(csslink));
  97.             if (!this.Page.ClientScript.IsStartupScriptRegistered(this.RegName))
  98.             {
  99.                 this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.RegName, this.RegSouceScript(), false);
  100.             }
  101.             base.OnPreRender(e);
  102.         }
  103.         protected override void AddAttributesToRender(HtmlTextWriter writer)
  104.         {
  105.             EnsureChildControls();
  106.             _image.Attributes.Add("onclick""OpenPopWinSingleCar()");
  107.             _delimage.Attributes.Add("onclick""ClearSingleCar()");
  108.             base.AddAttributesToRender(writer);
  109.         }
  110.         protected override void CreateChildControls()
  111.         {
  112.             Controls.Clear();
  113.             _textbox = new TextBox();
  114.             _textbox.ID = _textbox.UniqueID;
  115.             _textbox.CssClass = CssClass;
  116.             _textbox.TextMode = TextBoxMode.SingleLine;
  117.             _textbox.Attributes["Style"] = "border:1 solid gray;";
  118.             _textbox.Width = Unit.Pixel(_intTextBoxWidth);
  119.             _textbox.Height = this.Height;
  120.             Controls.Add(_textbox);
  121.             //空格
  122.             Controls.Add(new LiteralControl(" "));
  123.             //选择图片
  124.             _image = new HtmlImage();
  125.             _image.ID = _image.UniqueID;
  126.             _image.Alt = "选 择";
  127.             _image.Src = Page.ClientScript.GetWebResourceUrl(this.GetType(), "StarTech.WebControls.ControlSource.Images.application_add.png");
  128.             _image.Style.Value = "CURSOR:hand;";
  129.             Controls.Add(_image);
  130.             //空格
  131.             Controls.Add(new LiteralControl(" "));
  132.             //删除
  133.             _delimage.ID = _delimage.UniqueID;
  134.             _delimage.Src = Page.ClientScript.GetWebResourceUrl(this.GetType(), "StarTech.WebControls.ControlSource.Images.application_delete.png");
  135.             _delimage.Style.Value = "CURSOR:hand;";
  136.             _delimage.Alt = "删 除";
  137.             Controls.Add(_delimage);
  138.             //隐藏控件
  139.             _hiddenField = new HiddenField();
  140.             Controls.Add(_hiddenField);
  141.             if (_requiredfield)
  142.             {
  143.                 this.Controls.Add(new LiteralControl("<font color=red>*</font>"));
  144.                 RequiredFieldValidator validator = new RequiredFieldValidator();
  145.                 validator.ControlToValidate = _textbox.ID;
  146.                 validator.Text = "必填";
  147.                 validator.Font.Size = 9;
  148.                 validator.Display = ValidatorDisplay.Dynamic;
  149.                 if (this.ErrorMessage != null && this.ErrorMessage != "")
  150.                 {
  151.                     validator.ErrorMessage = this.ErrorMessage;
  152.                 }
  153.                 else
  154.                 {
  155.                     validator.ErrorMessage = "必填";
  156.                 }
  157.                 this.Controls.Add(validator);
  158.             }
  159.             base.CreateChildControls();
  160.         }
  161.         /**//// <summary>
  162.         /// 实现脚本
  163.         /// </summary>
  164.         /// <returns></returns>
  165.         private string RegSouceScript()
  166.         {
  167.             JavaScriptWriter js = new JavaScriptWriter(true);
  168.             js.AddLine("  rnd.today=new Date(); ");
  169.             js.AddLine("  rnd.seed=rnd.today.getTime(); ");
  170.             js.AddLine("  function rnd() { ");
  171.             js.AddLine("  rnd.seed = (rnd.seed*9301+49297) % 233280; ");
  172.             js.AddLine("  return rnd.seed/(233280.0); ");
  173.             js.AddLine("  }; ");
  174.             js.AddLine("  function rand(number) { ");
  175.             js.AddLine("  return Math.ceil(rnd()*number); ");
  176.             js.AddLine("  }; ");
  177.             js.AddLine("  function AlertMessageBoxSingleCar(returnValue)");
  178.             js.AddLine("  {");
  179.             js.AddLine("  var indexvalue;");
  180.             js.AddLine("  if (returnValue!=undefined){ ");
  181.             js.AddLine("    var txtname=document.getElementById('" + _textbox.ClientID + "');");
  182.             js.AddLine("    var txtid=document.getElementById('" + _hiddenField.ClientID + "');");
  183.             js.AddLine("    indexvalue=returnValue.indexOf('|');");
  184.             js.AddLine("    txtname.value = returnValue.substring(0,indexvalue);");
  185.             js.AddLine("    txtid.value = returnValue.substring(indexvalue+1);");
  186.             js.AddLine("   }");
  187.             js.AddLine("   }");
  188.             js.AddLine("   function OpenPopWinSingleCar()");
  189.             js.AddLine("   {");
  190.             js.AddLine("    showPopWin('" + _strDiglogTitle + "','" + ResolveUrl(_sourceUrl) + "?'+rand(10000000), " + _intDiglogWidth + ", " + _intDiglogHeight + ", AlertMessageBoxSingleCar,true,true);");
  191.             js.AddLine("   }");
  192.             js.AddLine(" function ClearSingleCar()");
  193.             js.AddLine(" { ");
  194.             js.AddLine("    document.all." + _textbox.ClientID + ".value=/"/";");
  195.             js.AddLine("    document.all." + _hiddenField.ClientID + ".value=/"/";");
  196.             js.AddLine(" }");
  197.             return js.ToString();
  198.         }
  199.     }
  200. }
控件是已经写好了,但是源页面,还是要通过外面程序提供,我的控件里面都已经写死了,其实也可以给他写个属性
让它可以在外部定义。
接下来就是如果获得源页面的返回值,具体代码如下
  // 返回值
window.returnVal = " nosnowwolf " ;
// 关闭窗口
 window.parent.hidePopWin( true ); 
OK,这样就可以了,可能我表达的不是很清楚,希望大家能给点意见,改天把代码放出来 
 
出自一个同事的BLOG http://www.cnblogs.com/nosnowwolf/articles/1232844.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值