我正在寻找为在asp.net网站中使用的JQuery日期时间选择器控件创建一个包装器控件。一旦用户控件准备就绪,它将用于简单的Web表单/网格/数据列表或转发器控件。用户控件还将公开下面提到的属性以进行自定义。
关注JQuery Date Time Picker Implementation。请参阅Demo的实际操作。
我愿意接受任何想法或建议。随意评论或分享您的想法。
提前致谢。
答案 0 :(得分:8)
我认为你想要创建一个可重用的控件,它使用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();
}