FredCK.FCKeditorV2和vs2005中RequiredFieldValidator配合使用的问题

1protected void Page_Load(object sender, EventArgs e)
2    {
3        this.form1.Attributes.Add  ("onsubmit""return CustomValidate()");
4}
FredCK.FCKeditorV2是一款相当好的开源html在线编辑器,我们在项目中用到了它,但是发现验证它是否有输入值的RequiredFieldValidator控件却每次要点击两次才能postback,比我我们原来是空的editor,打开页面之后,输入"aaaaaaaaa",点击RequiredFiledValidator,验证控件竟然认为它没有输入值。再点一下才可以完成postback,分析了一下,主要问题发生在FredCK.FCKeditorV2使用了iframe和input hidden两个控件作为客户端,这两个控件的value不能保持同步。原来以为二者根本没有添加同步功能,只是每次象动态创建的Control一样,LoadViewState加载进去的是上一次的value,然后在render方法里面在写入input hidden,但是奇怪的问题出现了,我给button加了一个客户端onclick事件,竟然发现是在验证的客户端处理中,iframe和input来个同步,郁闷。二者都不好改亚。FredCK.FCKeditorV2虽然开源,但问题又不是在这里。好不容易,终于google出来一条有建议的帖子,还是FredCK.FCKeditorV2官方的、原话这样说:

With ASP.Net, I need to submit twice when using the RequiredFieldValidator in a FCKeditor instance

FCKeditor will not work properly with the Required Field Validator when the "EnableClientScript" property of the validator is set to "true" (default). Due to a limitation in the default validation system, you must set it to "false".

If you want to do client side validation, you must use a Custom Validator instead and provide the appropriate validation function, using the FCKeditor JavaScript API.


终于明白了,呵呵,原来人家把这个没有当成bug处理。怪不得没有现成方法呢,按照他的建议,用FCKeditor JavaScript API.实现了这个功能。其实非常简单,在页面里面添加下面code
 1
 2    <script language="javascript"  type="text/javascript">
 3    var oEditer;
 4    function CustomValidate()
 5    {
 6        var value = oEditer.GetXHTML(true);
 7        if(value=='')
 8        {
 9           var obj = document.getElementById('errorDiv');
10           obj.innerHTML="<font color='red'>必填!</font>";
11           return false;     
12        }

13        return true;
14    }

15    function FCKeditor_OnComplete( editorInstance )
16    {  
17        oEditer = editorInstance;
18    }

19    </script>
20
后台,

FredCK.FCKeditorV2稍稍作了改动,render事件最后一行加上writer.Write("<div id='errorDiv'></div>");
运行效果还不错。呵呵,比较简单,另外有一个比较好的工具,文本比较,经常碰到这样的情形,说两个基本没有更改的不同版本的同一页面,怎么运行效果不一样的,页面又太大。一行一行查源码,烦死人。这样近似的文件,我们可以象vss里面比较功能一样,找出两个文件不同的地方,就好说了。
工具如下:/Files/jillzhang/TextDiff.rar
另外,我参考的一些网址:
http://wiki.fckeditor.net/Troubleshooting#head-9b3ef5962fb1f578c84005f3bff3ff725d3f84c4
http://fckeditor.wikiwikiweb.de/Developer%27s_Guide/Javascript_API
原文地址:https://www.cnblogs.com/jillzhang/p/518838.html