一:使用js
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
[assembly: WebResource("Controls.TextEditor.Resources.jVal.js", "text/javascript")]
[assembly: WebResource("Controls.TextEditor.Resources.TextEditor.js", "text/javascript")]
namespace Elane.Controls
{
[ToolboxData(@"<{0}:TextEditor runat='server'></{0}:TextEditor>")]
public class TextEditor : TextBox
{
private TextDataType FDataType;
/// <summary>
/// 数据类型
/// </summary>
[Browsable(true)]
[Description("DataType")]
[Category("Elane")]
[DefaultValue(0)]
public virtual TextDataType DataType
{
get { return FDataType; }
set { FDataType = value; }
}
private int FMaxLength;
/// <summary>
/// 最大长度
/// </summary>
[Browsable(true)]
[Description("MaxLength")]
[Category("Elane")]
[DefaultValue(0)]
public virtual int MaxLengths
{
get { return FMaxLength; }
set { FMaxLength = value; }
}
private int FMinLength;
/// <summary>
/// 最大长度
/// </summary>
[Browsable(true)]
[Description("MinLength")]
[Category("Elane")]
[DefaultValue(0)]
public virtual int MinLengths
{
get { return FMinLength; }
set { FMinLength = value; }
}
private bool FIsRequired;
/// <summary>
/// 是否必填
/// </summary>
[Browsable(true)]
[Description("是否必填")]
[Category("Elane")]
[DefaultValue(false)]
public virtual bool IsRequired
{
get { return FIsRequired; }
set { FIsRequired = value; }
}
/// <summary>
/// 重写text
/// </summary>
public override string Text
{
get { return base.Text.Trim(); }
set { base.Text = value; }
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
this.Attributes.Add("onclick", "testTextEditor();");
base.AddAttributesToRender(writer);
}
protected override void OnPreRender(EventArgs e)
{
if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "TextEditorLibrayJs"))
{
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "TextEditorLibrayJs", this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Controls.TextEditor.Resources.jVal.js"));
}
if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "TextEditorJs"))
{
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "TextEditorJs", this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Controls.TextEditor.Resources.TextEditor.js"));
}
}
}
}
设置资源的属性:生成操作=嵌入的资源;
引入资源时的路径:Controls.TextEditor.Resources.TextEditor.js 而不是Elane.Controls.TextEditor.Resources.TextEditor.js
文档上说是根命名空间加资源的路径,但我使用Elane.Controls.TextEditor.Resources.TextEditor.js却不行,锤子。