行者

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

在网上找到一个js的日期选着控件
calendar.htc


加工成自定义控件
InputCalendar.cs
using System;
using System.ComponentModel;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[assembly: TagPrefix(
"Jxmstc""Jxmstc")]
namespace Jxmstc
{
    [
    DefaultProperty(
"Text"),
    ToolboxData(
@"<{0}:InputCalender 
    ID='iptCad'
    runat='server'></{0}:InputCalender>
")
    ]
    
/// <summary>
    
/// 日期输入控件,
    
/// </summary>

    public class InputCalender : CompositeControl
    
{
        
private TextBox _textBox;
         [
         Bindable(
true), Category("Appearance"), DefaultValue(""), Description("获取或设置文本框输入文本")
        ]
        
public string Text
        
{
            
get
            
{
                EnsureChildControls();
                
return _textBox.Text;
            }

            
set
            
{
                EnsureChildControls();
                _textBox.Text 
= value;
            }

        }


        [
        Bindable(
true), Category("Appearance"), DefaultValue(""), Description("长度")
        ]
        
public int CssWidth
        
{
            
get return (ViewState["Length"!= null ? (int)ViewState["Length"] : 100); }
            
set { ViewState["Length"= value; }        
        }

        [
        Bindable(
true), Category("Appearance"), DefaultValue(""), Description("最大输入长度")
        ]
        
public int MaxLength
        
{
            
get return (ViewState["MaxLength"!= null ? (int)ViewState["MaxLength"] :10); }
            
set { ViewState["MaxLength"= value; }    
        }

        [
        Bindable(
true), Category("Appearance"), DefaultValue(""), Description("是否只读")
       ]
        
public bool ReadOnly
        
{
            
get return (ViewState["ReadOnly"!= null ? (bool)ViewState["ReadOnly"] : false); }
            
set { ViewState["ReadOnly"= value; }        
        }

        [
        Bindable(
true), Category("Appearance"), DefaultValue(""), Description("脚本资源文件地址")
        ]
        
public string ScriptUrl
        
{
            
get return (ViewState["ScriptUrl"!= null ? (string)ViewState["ScriptUrl"] :""); }
            
set { ViewState["ScriptUrl"= value; }    
        }

        
public InputCalender()
        
{
            
//this.ScriptUrl = GetAppRootPath() + "Images/calendar.htc";
            
//Jxmstc.RootPath mypath = new RootPath();
            

        }

        
protected override void CreateChildControls()
        
{
            Controls.Clear();
            _textBox 
= new TextBox();
            _textBox.ID 
= "iptCad";
            _textBox.ReadOnly 
= this.ReadOnly;
            _textBox.MaxLength 
= this.MaxLength;
            _textBox.Width 
= this.CssWidth;
            
if (this.ScriptUrl =="")
            
{
                
this.ScriptUrl = GetAppRootPath() +"Images/calendar.htc";
            }

            _textBox.Style.Add(
"behavior""url(" + ScriptUrl + ")");
            
this.Controls.Add(_textBox);
        }


        
protected override void Render(HtmlTextWriter writer)
        
{
            
            AddAttributesToRender(writer);
            _textBox.RenderControl(writer);

        }

        
//<summary>
        
//获取网站根目录
        
//</summary>
        
//<returns></returns>
        protected string GetAppRootPath()
        
{
            
try
            
{
                
string s = HttpContext.Current.Request.ApplicationPath;
                
//string s = "";
                if (s != "/")
                
{
                    s 
+= "/";
                }

                
return s;
            }

            
catch
            
{
                
return "";
            }

 
        }

  


    }

}

使用的时候,将calendar.htc放入网站根目录的Images中,同时在web.config中添加
<globalization requestEncoding="GB2312" responseEncoding="GB2312"/>
源码/Files/longren629/InputCalender.rar
posted on 2007-01-28 21:28  汪洋怡舟  阅读(652)  评论(0)    收藏  举报