使用CKeditor遇到的问题

本文介绍了解决在ASP.NET中使用CKEditor富文本编辑器时遇到的表单提交验证错误的方法。通过在web.config文件中设置requestValidationMode为2.0,并在ASPX页面上禁用ValidateRequest,可以避免因CKEditor提交的数据引发的服务器验证错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在aspx文件中 使用服务器控件 表单提交会有个验证   后台直接获取CK表单提交过来的值  会报错  


解决方案是  在 web.config文件中添加 <httpRuntime requestValidationMode="2.0" />  在ASPX文件添加ValidateRequest="false"


完整代码 

前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CKeditor.aspx.cs" Inherits="CKeditor.CKeditor" ValidateRequest="false" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="ckeditor/ckeditor.js" type="text/javascript"></script>  
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <textarea id="CK" cols="20" rows="2" runat="server"></textarea>
     <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>


 <script type="text/javascript">
        var ck = CKEDITOR.replace('CK');

        function getdata() {
            var t = ck.document.getBody().getText(); //取得纯文本
            var h = ck.document.getBody().getHtml(); //取得html文本
        }
</script>  


后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CKeditor
{
    public partial class CKeditor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string  a = CK.Value.ToString();
        }
    }
}

配置文件

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细消息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
      <httpRuntime requestValidationMode="2.0" />
    </system.web>

</configuration>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值