WebContorl示例--PopupFormTextBox

PopupFormTextBox代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Text;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Drawing.Design;

[assembly: WebResource(
" WebUIControl.Js.ButtonEdit.js " " application/x-javascript " )]
[assembly: WebResource(
" WebUIControl.Images.ico_view.gif " " image/gif " )]   
namespace  WebUIControl
{
    
///   <summary>
    
///  PopupFormURL为选取页面URL,js返回对象必须包含EditValue和DisplayValue属性。
    
///   </summary>
    [DefaultProperty( " PopupFormUrl " )]
    [ToolboxData(
" <{0}:ButtonEdit runat=server></{0}:ButtonEdit> " )]
    
public   class  ButtonEdit : CompositeControl
    {
        
// 记录Value、即DisplayValue
         private  TextBox _textBox;
        
// 记录ID、即EditValue
         private  HiddenField _hiddenField;
        
private  ImageButton _imageButton;

        
#region  Appearance
        [Browsable(
false )]
        [Themeable(
false )]
        
public   string  EditValue
        {
            
get
            {
                
this .EnsureChildControls();
                
return   this ._hiddenField.Value;
            }
            
set
            {
                
this .EnsureChildControls();
                
this ._hiddenField.Value  =  value;
            }
        }

        
///   <summary>
        
///  返回显示值的文本框
        
///   </summary>
        [Browsable( false )]
        
public  TextBox TextBox
        {
            
get
            {
                EnsureChildControls();
                
return  _textBox;
            }
        }

        
///   <summary>
        
///  返回隐藏控件
        
///   </summary>
        [Browsable( false )]
        
public  HiddenField HiddenField
        {
            
get  { EnsureChildControls();
                
return  _hiddenField; }
        }

        [Browsable(
false )]
        [Themeable(
false )]
        
public   string  DisplayValue
        {
            
get
            {
                
this .EnsureChildControls();
                
return   this ._textBox.Text;
            }
            
set
            {
                
this .EnsureChildControls();
                
this ._textBox.Text  =  value;
            }
        }

        [Bindable(
true )]
        [Category(
" Appearance " )]
        [DefaultValue(
"" )]
        [Localizable(
true )]
        
// [CssClassProperty]
         public   string  TextCssClass
        {
            
get
            {
                
object  obj  =   this .ViewState[ " TextCssClass " ];
                
if  (obj  !=   null )
                {
                    
return  ( string )obj;
                }
                
return   string .Empty;
            }
            
set
            {
                
this .EnsureChildControls();
                
this .ViewState[ " TextCssClass " =  value;
                
this ._textBox.CssClass  =  value;
            }
        }

        [Bindable(
true )]
        [Category(
" Appearance " )]
        [DefaultValue(
"" )]
        [Localizable(
true )]
        
// [CssClassProperty]
         public   string  ImageCssClass
        {
            
get
            {
                
object  obj  =   this .ViewState[ " ImageCssClass " ];
                
if  (obj  !=   null )
                {
                    
return  ( string )obj;
                }
                
return   string .Empty;
            }
            
set
            {
                
this .EnsureChildControls();
                
this .ViewState[ " ImageCssClass " =  value;
                
this ._imageButton.CssClass  =  value;
            }
        }

        
#endregion

        
#region  PopupForm
        [Bindable(
true )]
        [Category(
" PopupForm " )]
        [DefaultValue(
"" )]
        [Localizable(
true )]
        [Editor(
" System.Web.UI.Design.UrlEditor, System.Design " typeof (UITypeEditor))]
        
public   string  PopupFormUrl
        {
            
get
            {
                String s 
=  (String)ViewState[ " PopupFormUrl " ];
                
return  ((s  ==   null ?  String.Empty : s);
            }
            
set
            {
                ViewState[
" PopupFormUrl " =  value;
            }
        }

        [Bindable(
true )]
        [Category(
" PopupForm " )]
        [DefaultValue(
640 )]
        [Localizable(
true )]
        
public   int  PopupFormWidth
        {
            
get
            {
                
object  obj  =  ViewState[ " PopupFormWidth " ];
                
if  (obj  !=   null )
                {
                    
return  ( int )obj;
                }

                
return   640 ;
            }
            
set
            {
                
this .ViewState[ " PopupFormWidth " =  value;
            }
        }

        [Bindable(
true )]
        [Category(
" PopupForm " )]
        [DefaultValue(
800 )]
        [Localizable(
true )]
        
public   int  PopupFormHeight
        {
            
get
            {
                
object  obj  =  ViewState[ " PopupFormHeight " ];
                
if  (obj  !=   null )
                {
                    
return  ( int )obj;
                }

                
return   800 ;
            }
            
set
            {
                
this .ViewState[ " PopupFormHeight " =  value;
            }
        }
        
#endregion

        
protected   override   void  OnInit(EventArgs e)
        {
            
base .OnInit(e);
            
if  ( ! Page.ClientScript.IsClientScriptIncludeRegistered(Page.GetType(),  " ButtonEdit " ))
            {
                
string  webUrl  =  Page.ClientScript.GetWebResourceUrl(GetType(),  " WebUIControl.Js.ButtonEdit.js " );
                Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), 
" ButtonEdit " , webUrl);
            }
            
this .EnsureChildControls();

            
this ._imageButton.ImageUrl  =  Page.ClientScript.GetWebResourceUrl( this .GetType(),  " WebUIControl.Images.ico_view.gif " );
        }
        
protected   override   void  OnLoad(EventArgs e)
        {
            
base .OnLoad(e);
            
this .EnsureChildControls();

            
string  path  =  HttpUtility.UrlPathEncode( this .ResolveClientUrl( this .PopupFormUrl));
            
this .Attributes.Add( " PopupFormUrl " ,path);
            
if  ( this .PopupFormWidth  !=   640 )
            {
                
this .Attributes.Add( " PopupFormWidth " this .PopupFormWidth.ToString());
            }
            
if  ( this .PopupFormHeight  !=   800 )
            {
                
this .Attributes.Add( " PopupFormHeight " this .PopupFormHeight.ToString());
            }
            
this ._textBox.Attributes.Add( " ReadOnly " " ReadOnly " );
            
this ._imageButton.Attributes.Add( " onclick " "  return PL_ButtonEdit_PopupForm(' "   +   this .ClientID  +   " '); " );
        }

        
protected   override   void  CreateChildControls()
        {
            
this .Controls.Clear();
            
this ._textBox  =   new  TextBox();
            
this ._imageButton  =   new  ImageButton();
            
this ._hiddenField  =   new  HiddenField();
            
this .Controls.Add( this ._textBox);
            
this .Controls.Add( this ._imageButton);
            
this .Controls.Add( this ._hiddenField);
            
this .ClearChildViewState();
        }

        
protected   override   void  LoadViewState( object  savedState)
        {
            
base .LoadViewState(savedState);
            EnsureChildControls();
            
this .ViewState[ " TextCssClass " =   this ._textBox.CssClass;
            
this .ViewState[ " ImageCssClass " =   this ._imageButton.CssClass;
        }
    }
}

 

 

 

js代码
// 作为嵌入的资源
function  PL_ButtonEdit_PopupForm(id)
{
    
var  control  =  document.getElementById(id);
    
if  (control  !=   null   &&  control !=  undefined)
    {
        url 
=  control.PopupFormUrl;
        
if (url  !=   null   &&  url  !=  undefined)
        {
            
var  width  =   ' 640 ' ;
            
if (control.PopupFormWidth  !=  undefined)
                width 
=  control.PopupFormWidth;
            
var  height  =   ' 800 ' ;
            
if (control.PopupFormHeight  !=  undefined)
                height 
=  control.PopupFormHeight;
            
var  param  =   ' dialogWidth: '   +  width  +   ' px;dialogHeight: '   +  height  +   ' px;help:no;resizable:no;status:no ' ;
            
// 返回对象必须包含EditValue和DisplayValue属性
             var  ret  =  window.showModalDialog(url, '' ,param);
            
if  (ret  !=   null   &&  ret !=  undefined)
            {
                control.firstChild.readonly 
= ""  ;
                control.firstChild.value 
=  ret.DisplayValue;
                control.firstChild.readonly 
= " readonly " ;
                control.lastChild.value 
=  ret.EditValue;
            }
        }
    }
    
return   false ;
}

 

 

 

你可能感兴趣的:(form)