- 爱易网页
-
ASP.NET教程
- |M| 还是学习控件的有关问题,小弟我想给要验证的控件添加onfocus onblur 事件
日期:2014-05-19 浏览次数:20471 次
|M| 还是学习控件的问题,我想给要验证的控件添加onfocus onblur 事件
http://community.csdn.net/Expert/topic/5440/5440391.xml?temp=.6125299
在以上的源码的
protected override void RenderChildren(HtmlTextWriter writer)
后面加上
-------------------------
String cvID = " ";
Control cv = this.NamingContainer.FindControl(this.ControlToValidate);
if (cv != null)
{
cvID = cv.ClientID;
}
StringBuilder script = new StringBuilder();
script.Append( " <script type= 'text/javascript '> " + "\r\n ");
script.Append( " <!-- " + "\r\n ");
script.Append( "document.getElementById( ' " + cvID + " ').onfocus=function(){document.getElementById( ' " + ((WebControl)this.Controls[1]).ClientID + " ').innerHTML= ' " + this.Message + " '; " + this.FocusClientScript + "} " + "\r\n ");
script.Append( "document.getElementById( ' " + cvID + " ').onblur=function(){document.getElementById( ' " + ((WebControl)this.Controls[1]).ClientID + " ').innerHTML= ' " + this.ErrorMessage + " '; " + this.BlurClientScript + "}; " + "\r\n ");
script.Append( "//--> " + "\r\n ");
script.Append( " </script> ");
//writer.Write(script.ToString());
Page.ClientScript.RegisterClientScriptBlock(typeof(Type), " ", script.ToString());
------------------------------------------------
开始我是用
writer.Write(script.ToString());
的,但是这样当要验证的控件在这个验证控件之后的时候就会出错
因为document.getElementById找不到验证控件,因为他在后面还没有显现出来
然后我就用Page.ClientScript.RegisterClientScriptBlock(typeof(Type), " ", script.ToString());
但这样就什么js也没有注册
我想是因为这里只是控件,而不是页面的问题
遇到这种事情要怎么来解决
谢谢
------解决方案--------------------
控件.Attributs.Add( "onFocus ", "JavaSript ");