using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Voucher
{
    [DefaultProperty("jText")]
    [ToolboxData(@"<{0}:VoucherInput jText = ''  
    runat='server'></{0}:VoucherInput>")
    ]
    //设置默认的属性
    public class VoucherInput : WebControl
    {
      
        private TextBox _TextBox;    //整数位
        private TextBox _PTextBox;   //小数位

        #region 属性
        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("金额")
        ]
        public string jText
        {
            get
            {
                EnsureChildControls();
                return _TextBox.Text + "." + _PTextBox.Text;
            }
            set
            {
                EnsureChildControls();
                if (value == "" || value == null)
                {
                   
                }
                else
                {
                    string[] Allstring = value.Split('.');
                    _TextBox.Text = Allstring[0];
                    _PTextBox.Text = Allstring[1]; 
                }
            }
        }

        #endregion Properties delegated to child controls

        #region 方法
        protected override void CreateChildControls()
        {
            Controls.Clear();
            _TextBox = new TextBox();
            _TextBox.ID = "TextBox1";
            _TextBox.MaxLength = 13;
            _TextBox.Width = 120;
            _TextBox.Height = 20;
            _TextBox.Attributes.Add("onkeypress", "keyPress()");
            

            _TextBox.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url(text.gif)");
            _TextBox.Style.Add(HtmlTextWriterStyle.Direction, "rtl");
            _TextBox.Style.Add(HtmlTextWriterStyle.BorderWidth, "0");            

            _PTextBox = new TextBox();
            _PTextBox.Width = 15;
            _PTextBox.Height = 20;
            _PTextBox.MaxLength = 2;
            _PTextBox.ID = "PTextBox";
            _PTextBox.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url(textPoint.gif)");

            _PTextBox.Style.Add(HtmlTextWriterStyle.BorderWidth, "0");
            _PTextBox.Attributes.Add("onFocus", "PFocus()");
            _PTextBox.Attributes.Add("onkeypress", "PkeyPress()");

            this.Controls.Add(_TextBox);
            this.Controls.Add(_PTextBox);

        }

        protected override void Render(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);
            _TextBox.RenderControl(writer);
            _PTextBox.RenderControl(writer);
            writer.Write("<script language=javascript>function keyPress(){if(!(event.keyCode>=48&&event.keyCode<=57 ||event.keyCode==46 || event.keyCode==45)){event.keyCode = 0;}if(event.keyCode==46){event.keyCode = 0;document.getElementById('PTextBox').focus();}}</script>");   //对字符的控制
            writer.Write("<script language=javascript>function PFocus(){ var box=document.getElementById('PTextBox'); var rng = box.createTextRange(); rng.collapse(true); rng.moveEnd('character',2); rng.moveStart('character',0); rng.select(); }</script>");
            writer.Write("<script language=javascript>function PkeyPress(){if(event.keyCode==46){event.keyCode = 0;} if(!(event.keyCode>=48&&event.keyCode<=57 ||event.keyCode==46 || event.keyCode==45)){event.keyCode = 0;} }</script>");
        }
        #endregion Overriden methods

    }
}

 

posted on 2015-06-30 17:05  EricLi  阅读(431)  评论(0编辑  收藏  举报