WebContorl示例--ClientDateTimePicker

 

 

ClientDateTimePicker代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Text;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;

[assembly: WebResource(
" WebUIControl.Js.ClientDateTimePicker.js " " application/x-javascript " )]
namespace  WebUIControl
{
    
///   <summary>
    
///  使用此控件时,必须执行下列操作
    
///  1、将DateTimePicker目录内容Copy到web项目中
    
///  2、在页面中引入DateTimePicker目录的WdatePicker.js;[示例: <script type="text/javascript" src="../DateTimePicker/WdatePicker.js"></script> ]
    
///   </summary>
    [DefaultProperty( " EditValue " )]
    [ToolboxData(
" <{0}:ClientDateTimePicker runat=server></{0}:ClientDateTimePicker> " )]
    
public   class  ClientDateTimePicker : TextBox
    {
        [Bindable(
true )]
        [Category(
" Appearance " )]
        [DefaultValue(
" yyyy-MM-dd " )]
        [Localizable(
true )]
        
public   string  DateTimeFormat
        {
            
get
            {
                
object  obj  =  ViewState[ " DateTimeFormat " ];
                
if  (obj  !=   null )
                {
                    
return  ( string )obj;
                }
                
return   " yyyy-MM-dd " ;
            }
            
set
            {
                
if  ( string .IsNullOrEmpty(value)  ==   false )
                {
                    ViewState[
" DateTimeFormat " =  value;
                }
            }
        }

        [Bindable(
true )]
        [Category(
" Appearance " )]
        [Localizable(
true )]
        
public  DateTime ?  EditValue
        {
            
get
            {
                DateTime dt;
                
if  (DateTime.TryParse( this .Text,  out  dt))
                {
                    
return  dt;
                }
                
return   null ;
            }
            
set
            {
                
if  (value  ==   null )
                {
                    
this .Text  =   "" ;
                }
                
else
                {
                    
this .Text  =  value.Value.ToString( this .DateTimeFormat);
                }
            }
        }

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

        
protected   override   void  OnLoad(EventArgs e)
        {
            
base .OnLoad(e);

            
this .Attributes.Add( " class " " Wdate " );
            
this .Attributes.Add( " contentEditable " " false " );
            
if  ( ! string .IsNullOrEmpty( this .DateTimeFormat)  &&   this .DateTimeFormat  !=   " yyyy-MM-dd " )
            {
                
this .Attributes.Add( " DateTimeFormat " this .DateTimeFormat);
            }
            
if  ( this .EditValue  !=   null )
            {
                
this .Attributes.Add( " initvalue " this .EditValue.Value.ToString( " yyyy-MM-dd " ));
            }
            
this .Attributes.Add( " onfocus " " WdatePicker({skin:'whyGreen',dateFmt:' "   +   this .DateTimeFormat  +   " ',onpicked:function(dp){this.datevalue = new Date(dp.cal.date.y,dp.cal.date.M-1,dp.cal.date.d)},oncleared:function(dp){this.datevalue=null;this.initvalue=null;} }) " );
            
// this.Attributes.Add("onfocus", "alert(dd); WdatePicker({skin:'whyGreen',dateFmt:'" + this.DateTimeFormat + "'})");
        }
    }
}

 

 

 

 

js代码
function  PL_ClientDateTimePicker_getValue(control)
{
    
if (control.datevalue  !=  undefined)
    {
        
return  control.datevalue;
    }
    
if (control.initvalue  !=  undefined)
    {
        
var  initvalue  =  control.initvalue;
        
return   new  Date(initvalue.substring( 0 , 4 ), initvalue.substring( 5 , 7 ) - 1 , initvalue.substring( 8 , 10 ));
    }
}

 

 

My97DateTimePicker控件地址:

http://www.my97.net/dp/index.asp

 

你可能感兴趣的:(datetimepicker)