|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 ");
不知道行不行......
------解决方案--------------------
JavaScript是你自己写的函数名..
[解决办法]
RegisterClientScriptBlock或RegisterStartupScript
不行么?先上班去,一会做例子试。
[解决办法]
看下先
[解决办法]
writer.Write(script.ToString());
可以
[解决办法]
LZ的写法似乎是正确的
请检查一下页面源码中是否已经包含了你的JS代码,
如果包含了那是JS的问题,你可以将整个JS做为body的onload里处理
原因是你的JS太靠前,取不到后面的控件
[解决办法]
试了下,搞不定!
[解决办法]
try: this.Page.ClientScript.RegisterStartupScript(typeof(string), "aaa ", script.ToString());