如何封装JS和CSS文件封装为服务器端控件---ASP.NET 2.0

如何封装JS和CSS文件为服务器端控件---ASP.NET 2.0
我们以封装一个JS的日期控件为列子,将它和服务器的TextBox结合在一起做成一个服务器控件,以达到直接托上去就可以使用的效果。其实很简单,大家共同学习。先看看效果图:
date.JPG
方法:
首先:下载一个JS的日期组件,带封装。
然后:建一个日期类文件CalendarBox.cs代码如下:
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Text;
None.gif
using  System.Web;
None.gif
using  System.Drawing;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif[assembly: WebResource(
" Wisesoft.Web.Control.Wisesoft.Calendar.calendar.js " " application/x-javascript " , PerformSubstitution = true )]
None.gif
None.gif
namespace  Wisesoft.Web.Control
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ToolboxBitmap(
typeof(CalenderBox), "Wisesoft.Calendar.CalendarBox.ico")]
InBlock.gif    
public class CalenderBox : TextBox 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected override void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string calendar = CalenderCSS.CSS;
InBlock.gif            calendar 
= calendar.Replace("$ImaginURL$"this.ImaginURL);
InBlock.gif            
if (!Page.ClientScript.IsClientScriptBlockRegistered("_calendar"))
InBlock.gif                Page.ClientScript.RegisterClientScriptBlock(
typeof(string), "_calendar", calendar);
InBlock.gif            
InBlock.gif            
this.Page.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
InBlock.gif            
this.CssClass = "Wdate";
InBlock.gif            
this.Attributes.Add("onfocus","setday(this)");
InBlock.gif            
this.Attributes.Add("onchange""checkDate(this.value)");
InBlock.gif            
base.OnPreRender(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void Page_PreRenderComplete(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Page.ClientScript.RegisterClientScriptResource(
this.GetType(), "Wisesoft.Web.Control.Wisesoft.Calendar.calendar.js");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 弹出日期控件小图片的地址
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Bindable(true)]
InBlock.gif        [Category(
"图标设置")]
InBlock.gif        [DefaultValue(
"imagin/calender.gif")]
InBlock.gif        [Localizable(
true)]
InBlock.gif        
public string ImaginURL
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                String s 
= (String)ViewState["ImaginURL"];
InBlock.gif                
return ((s == null? "imagin/calender.gif" : s);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"ImaginURL"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置日期,时间的初始格式。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Bindable(true)]
InBlock.gif        [Category(
"初始化设置")]
InBlock.gif        [DefaultValue(
false)]
InBlock.gif        [Localizable(
true)]
InBlock.gif        
public bool ShowTime
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
bool s = (bool)ViewState["ShowTime"];
InBlock.gif                
if (ViewState["ShowTime"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return s;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"ShowTime"= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 注样式文件
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="path"></param>

InBlock.gif        private void RegisterCssFile(string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HtmlLink link1 
= new HtmlLink();
InBlock.gif            link1.Attributes[
"type"= "text/css";
InBlock.gif            link1.Attributes[
"rel"= "stylesheet";
InBlock.gif            link1.Attributes[
"href"= path;
InBlock.gif            
this.Page.Header.Controls.Add(link1);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
注意:[assembly: WebResource("Wisesoft.Web.Control.Wisesoft.Calendar.calendar.js", "application/x-javascript", PerformSubstitution=true)]是用来封装你的JS文件,要使用你项目的名字加上你JS文件的名字,还需要将你的JS文件做一点设置,点右件,选择属性--->然后选择高级,选择生成操作-->选择嵌入的资源,这样才能将JS文件封装进去,当然如果有图片或者是CSS文件也是一样的。
再看  void Page_PreRenderComplete(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Wisesoft.Web.Control.Wisesoft.Calendar.calendar.js");
        }
是在页面呈现之前将你的JS文件注册到页面上。
        [Bindable(true)]
        [Category("图标设置")]
        [DefaultValue("imagin/calender.gif")]
        [Localizable(true)]
是利用反射的元数据信息,来设置属性。就是给你的日期控件旁边加上个小图标,把它做成一个属性,可以让程序员自定义设置图片,也可以把它继承进去。
好了,这样就可以把JS文件封装好了,再来看看CSS文件如何封装并写入客户端。
我们可以看见下面有一个方法,
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 注样式文件
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="path"></param>

None.gif          private   void  RegisterCssFile( string  path)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            HtmlLink link1 
= new HtmlLink();
InBlock.gif            link1.Attributes[
"type"= "text/css";
InBlock.gif            link1.Attributes[
"rel"= "stylesheet";
InBlock.gif            link1.Attributes[
"href"= path;
InBlock.gif            
this.Page.Header.Controls.Add(link1);
ExpandedBlockEnd.gif        }
它就是用来注册你的CSS文件的,大家都知道在我们的页面代码里面是<link href="StyleSheet.css" rel="stylesheet" type="text/css" />引入外部样式文件。这个方法就是达到这个目的。那么我们现在还没有样式文件。
因此我们就必须建立一个CSS,然后把它设置一下(和JS的设置方式一样)。再到void Page_PreRenderComplete(object sender, EventArgs e)方法调用就可以了,但是还有其他方法,我们来介绍第二种(对JS文件也可以这样使用),建立一个CalendarCSS.cs文件,代码如下:
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  Wisesoft.Web.Control
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class CalenderCSS
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static string CSS = @"<style type=""text/css"">
InBlock.gif        .Wdate{
InBlock.gif        border:#999 1px solid;
InBlock.gif        height:18px;
InBlock.gif        background:url($ImaginURL$) no-repeat right;
InBlock.gif        }
InBlock.gif        .WdateFmtErr{
InBlock.gif        font-weight:bold;
InBlock.gif        color:red;
InBlock.gif        }
InBlock.gif        </style>
";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
这个类其实是将我们的CSS文件,写为一个字符串形式,然后供主函数调用,以注册到客户端使用。
那么我们再看看CalendarBox.cs文件里的这段代码
string calendar = CalenderCSS.CSS;
 calendar = calendar.Replace("$ImaginURL$", this.ImaginURL);
 if (!Page.ClientScript.IsClientScriptBlockRegistered("_calendar"))
 Page.ClientScript.RegisterClientScriptBlock(typeof(string), "_calendar", calendar);
它就是将我们已经写好的一段字符串以快的形式注册到客户端(当然还有更多的注册方式,可以在MSDN看看ClientScript类)。
好了,基本上就可以使用了。编译一下呢?
源码: Calendar.rar
如果有什么问题可以给我留言,刚开始写Blog,写的不好的地方请大家原谅哈。谢谢

转载于:https://www.cnblogs.com/wcp066/archive/2007/07/28/834392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值