来源:PowerEasy
添加后直接引用就可以了
附上控件代码
InteractiveMessager
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Design;
using System.ComponentModel;
using System.Drawing;
namespace UserCenter
{
public class InteractiveMessager : Literal
{
// Methods
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
WebControl control = this.Parent.FindControl(this.ControlToMessage) as WebControl;
if (control != null)
{
control.Attributes.Add("onfocus", "onControlFocus(" + control.ClientID + "," + this.ClientID + ");");
control.Attributes.Add("onblur", "onControlBlur(" + control.ClientID + "," + this.ClientID + ");");
//control.Style.Add("float", "left");
//control.Style.Add("line-height", "100%");
}
this.Page.ClientScript.RegisterExpandoAttribute(this.ControlToMessage, "MessageId", this.ClientID);
this.Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "ValidatorOkMessage", this.ValidatorOkMessage);
this.Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "Message", base.Text);
this.Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "IsValidEmpty", this.IsValidEmpty.ToString());
string script = "<script type=\"text/javascript\">\r\nvar currentClassName = \"\";\r\nfunction onControlFocus(obj,messager)\r\n{\r\n currentClassName = obj.className;\r\n obj.className = 'input_onFocus';\r\n messager.className = 'd_on';\r\n messager.innerHTML = messager.Message;\r\n}\r\n \r\nfunction onControlBlur(obj,messager)\r\n{\r\n obj.className = currentClassName;\r\n messager.className ='';\r\n for (i = 0; i < Page_Validators.length; i++) \r\n {\r\n val = Page_Validators[i];\r\n if(val.controltovalidate == obj.id)\r\n {\r\n val.isvalid = true;\r\n if ((typeof(val.enabled) == \"undefined\" || val.enabled != false)) \r\n {\r\n if (typeof(val.evaluationfunction) == \"function\")\r\n {\r\n val.isvalid = val.evaluationfunction(val);\r\n }\r\n }\r\n \r\n if(messager.IsValidEmpty==\"True\")\r\n {\r\n if(val.isvalid == false)\r\n {\r\n messager.innerHTML = val.errormessage;\r\n messager.className = 'd_err';\r\n break;\r\n }\r\n else\r\n {\r\n messager.innerHTML = messager.ValidatorOkMessage;\r\n messager.className = 'd_ok';\r\n }\r\n }\r\n else\r\n {\r\n if(obj.value!='')\r\n {\r\n if(val.isvalid == false)\r\n {\r\n messager.innerHTML = val.errormessage;\r\n messager.className = 'd_err';\r\n break;\r\n }\r\n else\r\n {\r\n messager.innerHTML = messager.ValidatorOkMessage;\r\n messager.className = 'd_ok';\r\n }\r\n }\r\n else\r\n {\r\n obj.className ='';\r\n messager.className = '';\r\n }\r\n }\r\n\r\n ValidatorUpdateDisplay(val);\r\n }\r\n }\r\n}\r\n</script>";
if (!this.Page.ClientScript.IsStartupScriptRegistered(this.Page.GetType(), "MessageJS"))
{
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MessageJS", script);
}
}
public string MessageFunction()
{
string ret = string.Empty;
WebControl control = this.Parent.FindControl(this.ControlToMessage) as WebControl;
if (control != null)
{
ret = "onControlBlur(" + control.ClientID + "," + this.ClientID + ");";
}
return ret;
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<li class=\"msg\" id=\"");
writer.Write(this.ClientID);
writer.Write("\">");
if (this.IsShowText)
{
writer.Write(base.Text);
}
writer.Write("</li>");
}
// Properties
[DefaultValue("")]
public string ControlToMessage
{
get
{
object obj2 = this.ViewState["ControlToMessage"];
if (obj2 != null)
{
return (string)obj2;
}
return "";
}
set
{
this.ViewState["ControlToMessage"] = value;
}
}
[DefaultValue(false)]
public bool IsShowText
{
get
{
object obj2 = this.ViewState["IsShowText"];
return ((obj2 != null) && ((bool)obj2));
}
set
{
this.ViewState["IsShowText"] = value;
}
}
[DefaultValue(true)]
public bool IsValidEmpty
{
get
{
object obj2 = this.ViewState["IsValidEmpty"];
if (obj2 != null)
{
return (bool)obj2;
}
return true;
}
set
{
this.ViewState["IsValidEmpty"] = value;
}
}
[DefaultValue("")]
public string ValidatorOkMessage
{
get
{
object obj2 = this.ViewState["ValidatorOkMessage"];
if (obj2 != null)
{
return (string)obj2;
}
return "";
}
set
{
this.ViewState["ValidatorOkMessage"] = value;
}
}
}
}
附上页面代码
<asp:TextBox ID="txtAnswer" runat="server" style="width:180px" ></asp:TextBox>
<span style ="color :Red">*</span>
<asp:RequiredFieldValidator
ControlToValidate ="txtAnswer"
ID="RequiredFieldValidator4"
runat="server"
ErrorMessage="(此项必填)"
Display="None">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ControlToValidate="txtAnswer"
ID="RegularExpressionValidator8"
runat="server"
ErrorMessage="您的答案少于四个字符或者多于五十个字符"
ValidationExpression="^.{4,50}$"
Display="None">
</asp:RegularExpressionValidator>
</li>
<uc:InteractiveMessager ID ="interactiveMessager5"
ControlToMessage ="txtAnswer"
runat ="server"
Text="用于取回密码。请尽量设得复杂一些,以防被人用找回密码功能窃取您的帐号。长度在四到五十个字符之间。"
ValidatorOkMessage ="答案已经输入" >
</uc:InteractiveMessager>
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。