简体   繁体   English

用于JQuery日期时间选择器的ASP.Net包装器控件

[英]ASP.Net wrapper control for JQuery datetime picker

I'm looking to create a wrapper control for JQuery date time picker control to be used in asp.net website. 我正在寻找为在asp.net网站上使用的JQuery日期时间选择器控件创建一个包装器控件。 Once the user control is ready, it will be used in simple web forms / grids / data lists or repeater controls. 一旦用户控件准备就绪,它将用于简单的Web表单/网格/数据列表或转发器控件。 User control will also expose below mentioned properties for customization. 用户控件还将公开下面提到的属性以进行自定义。

  1. TimeHourFormat: "12" or "24" (12 (AM/PM) or 24 hour) TimeHourFormat:“12”或“24”(12(上午/下午)或24小时)
  2. TimeAMPMCondense: True (If 12 hour format, display AM/PM with only 1 letter and no space ie 1:00A or 5:05P) TimeAMPMCondense:True(如果是12小时格式,显示只有1个字母且没有空格的AM / PM,即1:00A或5:05P)
  3. TimeFormat: "HH/MM" (Leading zeros on Hours and Minutes. Default to always have leading zeros.) 时间格式:“HH / MM”(小时和分钟前导零。默认为始终为前导零。)
  4. CssClass: "calendarClass" (Name of the CSS class/style sheet for formatting) CssClass:“calendarClass”(用于格式化的CSS类/样式表的名称)
  5. ReadOnly: True (Set textbox to readonly mode and disable pop up calendar If false, then enable pop up calendar and enable access to textbox) ReadOnly:True(将文本框设置为只读模式并禁用弹出日历如果为false,则启用弹出日历并启用对文本框的访问)
  6. DateFormat: "MM/DD/YYYY" (Pass C# standard formatting to also include YY no century formats. Default to always have leading zeros and century.) DateFormat:“MM / DD / YYYY”(传递C#标准格式也包括YY没有世纪格式。默认为始终具有前导零和世纪。)
  7. Display: "C" (Pass C to display Calendar only, CT for Calendar and Time, and T for time only display) 显示:“C”(传递C仅显示日历,CT显示日历和时间,T显示仅限时间显示)
  8. Placement: "Popup" (Default for pop up of the control, could also be inline) 放置:“弹出”(弹出控件的默认值,也可以是内联的)
  9. DateEarly: "01/01/1900" (If date is equal to or less than, then display and return a null (blank) value) DateEarly:“01/01/1900”(如果date等于或小于,则显示并返回null(空白)值)
  10. WeekStart: "Sun" (Day of week to start calendar) WeekStart:“太阳报”(开始日历的星期几)
  11. Image: "/image/calendar.ico" (Name and path to use for the image used on the right of the textbox to click and have it display. If not specified, then clicking in the enabled field will 'pop up' the control.) 图像:“/ image / calendar.ico”(用于文本框右侧用于单击并显示它的图像的名称和路径。如果未指定,则单击启用的字段将“弹出”控件。)

Follow the JQuery Date Time Picker Implementation . 遵循JQuery日期时间选择器实现 See Demo in action. 请参阅演示中的演示

I'm open for any idea or suggestion. 我愿意接受任何想法或建议。 Feel free to comment or share your ideas. 随意评论或分享您的想法。

Thanks in advance. 提前致谢。

I take it you want to create a re-useable control that uses jQuery functionality and wraps everything up nicely? 我认为你想要创建一个可重用的控件,它使用jQuery功能并很好地包装所有内容? If I've understood you correctly you need to create an IScriptControl. 如果我理解正确你需要创建一个IScriptControl。

Create two files in your project, ie: 在项目中创建两个文件,即:

Project
|...Controls
    |...MyDateTimePicker.cs
    |...MyDateTimePicker.js

Set MyDateTimePicker.js as an embedded resource and then add the following line to your assembly info: 将MyDateTimePicker.js设置为嵌入式资源,然后将以下行添加到程序集信息中:

[assembly: System.Web.UI.WebResource("Project.Controls.MyDateTimePicker.js", "text/javascript")]

Once you've done that, go to the MyDateTimePicker.cs class and create a basic template as follows: 完成后,转到MyDateTimePicker.cs类并创建一个基本模板,如下所示:

[DefaultProperty("ID")]
[ToolboxData("<{0}:MyDateTimePicker runat=server />")]
public class MyDateTimePicker : WebControl, IScriptControl
{

}

Once you've done that, you need to register the control as a ScriptControl, so add the following: 完成后,您需要将控件注册为ScriptControl,因此添加以下内容:

protected override void OnPreRender(EventArgs e)
{

    if (!this.DesignMode)
    {

        // Link the script up with the script manager
        ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
        if (scriptManager != null)
        {
            scriptManager.RegisterScriptControl(this);
            scriptManager.RegisterScriptDescriptors(this);
            scriptManager.Scripts.Add(new ScriptReference(
                "Project.Controls.MyDateTimePicker.js", "Project"));
        }
        else
        {
            throw new ApplicationException("You must have a ScriptManager on the Page.");
        }

    }

    base.OnPreRender(e);

}

This now means that the control can pass properties client side. 这意味着控件可以传递属性客户端。 So, start by adding your properties, ie 所以,首先添加你的属性,即

public virtual string TimeHourFormat {get;set;}
public virtual string TimeFormat {get;set;}

Once you have some properties you need to pass them as script descriptors: 一旦你有了一些属性,你需要将它们作为脚本描述符传递:

IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
{
    ScriptControlDescriptor desc = new ScriptControlDescriptor("Project.MyDateTimePicker", 
        this.ClientID);

    // Properties
    desc.AddProperty("timeHourFormat", this.TimeHourFormat);
    desc.AddProperty("timeFormat", this.TimeFormat);

    yield return desc;
}

IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
{
    ScriptReference reference = new ScriptReference();
    reference.Assembly = Assembly.GetAssembly(typeof(MyDateTimePicker)).FullName;
    reference.Name = "Project.MyDateTimePicker.js";
    yield return reference;
}

We now have everything we need to implement the client side script, which can contain all the jQuery you want. 我们现在拥有了实现客户端脚本所需的一切,它可以包含您想要的所有jQuery。 Pop the following template into MyDateTimePicker.js and away you go! 将以下模板弹出到MyDateTimePicker.js中然后离开!

Type.registerNamespace('Project');

Project.MyDateTimePicker = function (element) {

    this._timeHourFormat = null;
    this._timeFormat = null;

    // Calling the base class constructor
    Project.MyDateTimePicker.initializeBase(this, [element]);

}

Project.MyDateTimePicker.prototype =
{

    initialize: function () {

        // Call the base initialize method
        Project.MyDateTimePicker.callBaseMethod(this, 'initialize');

        $(document).ready(
            // See, jQuery!
        );

    },

    dispose: function () {

        // Call the base class method
        Project.MyDateTimePicker.callBaseMethod(this, 'dispose');

    },


    //////////////////
    // Public Methods 
    ////////////////// 

    // Hides the control from view
    doSomething: function (e) {

    },

    //////////////////
    // Properties 
    //////////////////   

    get_timeHourFormat: function () {
        return this._timeHourFormat;
    },
    set_timeHourFormat: function (value) {
        var e = Function._validateParams(arguments, [{ name: 'value', type: String}]);
        if (e) throw e;
        if (this._timeHourFormat != value) {
            this._timeHourFormat = value;
            this.raisePropertyChanged('timeHourFormat');
        }
    },

    get_timeFormat: function () {
        return this._timeFormat;
    },
    set_timeFormat: function (value) {
        var e = Function._validateParams(arguments, [{ name: 'value', type: String}]);
        if (e) throw e;
        if (this._timeFormat != value) {
            this._timeFormat = value;
            this.raisePropertyChanged('timeFormat');
        }
    }

}


Project.MyDateTimePicker.registerClass('Project.MyDateTimePicker', Sys.UI.Control);

if (typeof(Sys) != 'undefined')
{
    Sys.Application.notifyScriptLoaded();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM