給 控制項加一些功能。(只能在 ie 下、 瀏覽 )
例如 textBox控制項、 、 、 、
大致 就是:加了個屬性 CheckCode ,又給 textBox 加了onkeypress 事件 、 把指令碼寫入一個 Sring 、、
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"key1",ScriptString,true); 實行 指令碼、 、 、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace My_CustomWebContrls
{
[DefaultProperty("Text"),
ToolboxData("<{0}:MytextBox runat='server' Text=''></{0}:MytextBox>")]
public class MytextBox:TextBox
{
public MytextBox()
{
this.Load+=new EventHandler(MytextBox_Load);
}
public String CheckCode { get; set; }
void MytextBox_Load(object sender, EventArgs e)
{
if (this.Page.ClientScript.IsClientScriptBlockRegistered("key1")) {
return;
}
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"key1",ScriptString,true);
this.Attributes.Add("onkeypress","return checkinput('"+CheckCode+"')");
}
String temp = String.Empty;
public String ScriptString {
get {
temp += "function checkinput(code) {";
temp += "for (var i = 0; i < code.length; i++) {";
temp += "if (code.charCodeAt(i) == event.keyCode) {";
temp += "return true;}}";
temp += "return false;}";
return temp;
}
}
}
}