我正在寻找要在asp.net网站中使用的JQuery日期时间选择器控件的包装器控件。一旦用户控件准备就绪,它将以简单的Web表单/网格/数据列表或转发器控件使用。用户控件还将公开以下提及的属性以进行自定义。
遵循JQuery Date Time Picker Implementation。请参见Demo的实际应用。
我愿意接受任何想法或建议。随时发表评论或分享您的想法。
提前致谢。
最佳答案
我认为您想创建一个可重用的控件,该控件使用jQuery功能并将所有内容包装好吗?如果我对您的理解正确,则需要创建一个IScriptControl。
在您的项目中创建两个文件,即:
Project
|...Controls
|...MyDateTimePicker.cs
|...MyDateTimePicker.js
将MyDateTimePicker.js设置为嵌入式资源,然后将以下行添加到您的程序集信息中:
[assembly: System.Web.UI.WebResource("Project.Controls.MyDateTimePicker.js", "text/javascript")]
完成此操作后,转到MyDateTimePicker.cs类,并按如下所示创建一个基本模板:
[DefaultProperty("ID")]
[ToolboxData("<{0}:MyDateTimePicker runat=server />")]
public class MyDateTimePicker : WebControl, IScriptControl
{
}
完成此操作后,需要将控件注册为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);
}
现在,这意味着控件可以传递客户端属性。因此,请先添加您的属性,即
public virtual string TimeHourFormat {get;set;}
public virtual string TimeFormat {get;set;}
一旦有了一些属性,就需要将它们作为脚本描述符传递:
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;
}
现在,我们拥有实现客户端脚本所需的一切,其中可以包含所需的所有jQuery。将以下模板弹出到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();
}
关于c# - JQuery日期时间选择器的ASP.Net包装器控件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2833603/