Asp.net自定义控件之DateTimePicker源代码

写在前面:要实现DateTimePicker功能,最简单的就是一个input,旁边有一个日历的小图标,加入大量的javascript代码,然后,点击日历后,就出现一个div用来选择日期,选定之后,input就会出现刚才选定的日期。而input应该是只读的。 下载源代码

下载源代码

控件源代码

下载Demo项目

效果图
 

标记

Register Assembly="DateTimePickerControls" Namespace="DateTimePickerControls" TagPrefix="DTP"
这是写在aspx页上面的,用于引用dll的资源。
对应于源代码中的命名空间的属性定义
[assembly: TagPrefix("DateTimePickerControls", "DTP")]

加入工具箱
可以使用下面的方法将自定义的Asp.Net控件加入到工具箱中,如果引入DLL之后,工具箱还不出现控件,可以在工具箱右击, 选择项,然后选择DLL就一定可以。下面的ToolboxData表现控件的名称,而Designer表示控件在设计界面(DesignMode)中的样 子,注意,必须存在DateTimePickerControls.DateTimePickerDesigner这个类才出现这句话。而
DescriptionAttribute则是描述控件的作用。

    [
    ToolboxData(
"<{0}:DateTimePicker runat=server></{0}:DateTimePicker>"),
    ValidationPropertyAttribute(
"Text"),
    Designer(
typeof(DateTimePickerControls.DateTimePickerDesigner)),
    DescriptionAttribute(
"一个基于 MSHTML 的 ASP.NET 时间选择器。")
    ]


 

继承
public class DateTimePicker: Control, IPostBackDataHandler, INamingContainer, IPostBackEventHandler
Control 是System.Web.UI空间下面的Control,表现Web控件的类,而IPostBackDataHandler是定义 ASP.NET 服务器控件为自动加载回发数据而必须实现的方法。也就是,使用_Control.Tex而不是Request.QueryString[“…”]. ToString()或Request.Form[“…”].ToString(),来获取Html中的数据,主要的方法是LoadPostData, RaisePostDataChangedEvent,而INamingContainer和IPostBackEventHandler则暂时没怎么使 用。笔者只是参考其它Web控件而把这两个接口加上去。

注册脚本
注意到每一个Web自定义控件,都有其对应的Javascript或Vbscript脚本,而且,当页面上有多个这样的控件。不应用出现多个相同脚本。
所 以,要使用Page.ClientScript.IsClientScriptBlockRegistered方法来注册脚本。这样的注册脚本,相当于有 一个Hashtable来保存脚本,而每一个注入的脚本都有一个Key来关联。这样的好处是,在使用多个脚本时,不会重复地写在页面上。
下面的代码,使用到资源文件中写好的脚本文件,换句话说,可以将脚本文件,如Javascript或Vbscript脚本先写好,然后,直接复制到

            if (!Page.ClientScript.IsClientScriptBlockRegistered("DateTimePickerBaseScript"))
            {
                ResourceManager manager 
= new ResourceManager(this.GetType());
                
string script = manager.GetResourceSet(
                    System.Globalization.CultureInfo.CurrentCulture, 
truetrue).GetString("DateTimePickerBaseScript");
                Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "DateTimePickerBaseScript", script);
            }


而.resx文件的源代码,可以是这样的:

<root>
<!--其它的资源-->
<data name="DateTimePickerBaseScript">
<![CDATA[
Javascript或VbScript
]]>
</data>
</root>


在资源文件中写脚本的优点是十分的明显的,也就是.js的内容可以直接复制并粘贴到这里,而不用再经过处理。

重写Control继承而来的方法
protected override void OnPreRender(EventArgs e)
protected override void CreateChildControls()
可以将OnPreRender方法当成普通Page的OnLoad方法使用,而CreateChildControls方法用于新建子控件,也就是构造自定义控件的主要代码。

获取Text属性

        public bool LoadPostData(String postDataKey, NameValueCollection values)
        {
            
string PresentValue = this.ViewStateText;
            
string PostedValue = values[base.ID];
            
if (!PresentValue.Equals(PostedValue))
            {
                
this.Text = PostedValue;
                
return true;
            }
            
else
            {
                
return false;
            }
        }


上 面是使用LoadPostData方法来获取Text属性的值。注意,这个方法有时候是不运行的,这是因为你的ChildControls中没有一个ID =base.ID的控件,则,必须有一个子控件的ID为本控件的ID,这里讲的子控件,不是指System.Web.UI空间下面的控件,而是指代那些可 以在客户端的浏览器中显示的<input id=””>形式的控件,也就是使用string来表达的。正如
WriteTimePicker方法中写到的"<input type=\"text\" id=\"" + DateTimePickerID。

获取FormID
可以使用一个遍历的过程,获取项层控件的ClientID,因为在Js脚本中,服务器端的ID是用不了的。

设置DesignMode属性类
也就是前面所提及的Designer(typeof (DateTimePickerControls.DateTimePickerDesigner)),相对来说,是比较简单的,可以说,你可以参照一个 正确的例子,然后随便修改一下就可以。需要继承System.Web.UI.Design.ControlDesigner类,重写方法 Initialize,GetDesignTimeHtml。而GetDesignTimeHtml就是最后显示在设计界面上面的样子。
 
设计代码如下:

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw 
= new HtmlTextWriter(sw);
            HtmlInputText inputText 
= new HtmlInputText();
            inputText.Value 
= dtp.ID;
            inputText.Style.Value 
= "Width:100px;border-style: none;background-color: #9EBEF5";           
            inputText.RenderControl(htw);
            
return sw.ToString();



综上述得,要定义一个比较好的自定义控件,首先要有一个非控件形式的“功能点”使用方法,即上述的时间选择功能,要Jsp,Asp,Asp.Net中都可以使用的。然后,根据Asp.Net自定义控件的语法,一步步翻译就没什么问题了。

声明:本文乃笔者原创,不过,不是首先发布在此blog.


原文地址:https://www.cnblogs.com/jingle/p/1150126.html