封装JS和CSS文件封装为服务器端控件方法:
首先:一个JS的日期组件,带封装。
然后:建一个日期类文件CalendarBox.cs代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
[assembly: WebResource("Wisesoft.Web.Control.Wisesoft.Calendar.calendar.js", "application/x-javascript", PerformSubstitution=true)]
namespace Wisesoft.Web.Control
{
[ToolboxBitmap(typeof(CalenderBox), "Wisesoft.Calendar.CalendarBox.ico")]
public class CalenderBox : TextBox
{
protected override void OnPreRender(EventArgs e)
{
string calendar = Calender.;
calendar = calendar.Replace("$ImaginURL$", this.ImaginURL);
if (!Page.ClientScript.IsClientScriptBlockRegistered("_calendar"))
Page.ClientScript.RegisterClientScriptBlock(typeof(string), "_calendar", calendar);
this.Page.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
this.CssClass = "Wdate";
this.Attributes.Add("onfocus","setday(this)");
this.Attributes.Add("onchange", "checkDate(this.value)");
base.OnPreRender(e);
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Wisesoft.Web.Control.Wisesoft.Calendar.calendar.js");
}
/**////
/// 弹出日期控件小图片的地址
///
[Bindable(true)]
[Category("图标设置")]
[DefaultValue("imagin/calender.gif")]
[Localizable(true)]
public string ImaginURL
{
get
{
String s = (String)ViewState["ImaginURL"];
return ((s == null) ? "imagin/calender.gif" : s);
}
set
{
ViewState["ImaginURL"] = value;
}
}
/**////
/// 设置日期,时间的初始格式。
///
[Bindable(true)]
[Category("初始化设置")]
[DefaultValue(false)]
[Localizable(true)]
public bool ShowTime
{
get
{
bool s = (bool)ViewState["ShowTime"];
if (ViewState["ShowTime"] != null)
{
return s;
}
return false;
}
set
{
ViewState["ShowTime"] = value;
}
}
/**////
/// 注样式文件
///
///
private void RegisterCssFile(string path)
{
HtmlLink link1 = new HtmlLink();
link1.Attributes["type"] = "text/css";
link1.Attributes["rel"] = "stylesheet";
link1.Attributes["href"] = path;
this.Page.Header.Controls.Add(link1);
}
}
}