下一步,将给pimedouttextbox控件新增两个属性。其中,我们设想,当用户在文本框中输入或者文本框获得焦点时,文本框的颜色有变化,所以命名新的属性BackColoron;当控件失去焦点时,文本框的颜色命名为backcoloroff。
| line 1: private Color _colOff; line 2: [Category( "Appearance" ), Description( "The background color when the control loses focus" )] line 3: public Color BackColorOff line 4: { line 5: get{return _colOff;} line 6: set{_colOff = value line 7: } line 8: private Color _colOn; line 9: [Category( "Appearance" ), Description( "The background color when the control has the focus" )] line 10: public Color BackColorOn line 11: { line 12: get{return _colOn; } line 13: set{_colOn = value;} line 14: } |
| line 1: protected override void AddAttributesToRender( HtmlTextWriter writer ) line 2: { line 3: base.AddAttributesToRender( writer ); line 4: //only add the client-side Javascript for design mode or IE line 5: if( inDesignMode() || System.Web.HttpContext.Current.Request.Browser.Type.IndexOf( "IE" ) > -1 ) line 6: { line 7: writer.AddAttribute( "onFocus", "JavaScript:this.style.backgroundColor='" + ColorTranslator.ToHtml( _colOn ) + "';" ); line 8: if( _colOff.Equals( Color.Empty ) ) line 9: { line 10: _colOff = this.BackColor; line 11: } line 12: writer.AddAttribute( "onBlur", "JavaScript:this.style.backgroundColor='" + ColorTranslator.ToHtml( colOff ) + "';" ); line 13: } line 14: } |
| line 1: private bool inDesignMode() line 2: { line 3: bool blnOut = false; line 4: if( object.ReferenceEquals( System.Web.HttpContext.Current, null ) ) line 5: { line 6: blnOut = true; line 7: } line 8: else line 9: { line 10: blnOut = false; line 11: } line 12: return blnOut; line 13: } |
http://dev.xuezhishi.net/website/NET/2007-10-17/20794.html