在使用网页编辑器的时候不仅要求异步还要求方便,下面是使用ExtJS+CKfinder+CKEditor的小结,不废话,直接上代码:
前台aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PublishNews.aspx.cs" Inherits="PublishNews"
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>
<title></title>
<link href="../../../Ext/resources/css/ext-all.css" rel="stylesheet" type="text/css" />
<script src="../../../Ext/adapter/ext/ext-base.js" type="text/javascript"></script>
<script src="../../../Ext/ext-all.js" type="text/javascript"></script>
<script src="../../../Ext/source/locale/ext-lang-zh_CN.js" type="text/javascript"></script>
<script src="../kindeditor.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="replyMain">
<div id="replyMain_body">
<%--!@Important--%>
<div style="display:none"><asp:TextBox ID="content" runat="server"></asp:TextBox></div>
</div>
</div>
</form>
</body>
</html>
<script type="text/javascript">
var app = {};
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';
var isTopStore = new Ext.data.SimpleStore(
{
fields: ["isTop", "isTopName"],
data: [['0', '游戏新闻'], ['1', '公会新闻']]
}
);
var newscategoryStore = new Ext.data.JsonStore({
totalProperty: "total", root: "data", //后台返回的分页后的数据
fields: ['category_id', 'category_name']
});
var replyMainPanel = new Ext.Panel({
title: '<%if (nNewsID > 0 && model != null){%>编辑<%}else{%>发布<% }%>新闻 ',
layout: 'form',
contentEl: 'replyMain_body',
frame: true,
bodyStyle: 'background-color:#FFFFFF;padding:15px;',
applyTo: 'replyMain',
height: true,
autoWidth: true,
bodyBorder: false,
boder: false,
buttonAlign: 'center',
labelAlign: 'right',
collapsible: true,
collapsed: false,
items: [new Ext.form.TextField({
fieldLabel: '标题',
name: 'Title',
id: 'Title',
allowBlank: false,
blankText: '请输入标题',
growMax: 300,
width: 700,
emptyText: '请输入标题'
<%if( nNewsID > 0 && model != null){%>,
value: '<%=model.n_title%>'<%}%>
}), new Ext.Panel({
layout: 'column',
width: 827,
border: false,
items: [new Ext.Panel({
columnWidth: .5,
layout: 'form',
border: false,
labelWidth: 100,
labelAlign: 'right',
items: [{
xtype: 'combo',
forceSelection: true,
fieldLabel: '新闻类型',
id: 'IsTop',
name: 'IsTop',
store: isTopStore,
valueField: 'isTop',
displayField: 'isTopName',
triggerAction: 'all',
listeners: { 'select': function () {
Ext.getCmp('category').setValue("");
newscategoryStore.removeAll();
var url = '../../../Service/NewsSrv.ashx?type=getcategory';
newscategoryStore.proxy = new Ext.data.HttpProxy({ url: url });
newscategoryStore.load({ params: { nNewsType: this.value} });
}
},
mode: 'local',
lazyRender: false,
hiddenName: 'IsTopValue',
allowBlank:false,
blankText:'你丫必须选择',
emptyText:'请选择类型',
anchor: '95%'
}]
}), new Ext.Panel({
columnWidth: .5,
layout: 'form',
border: false,
labelWidth: 100,
labelAlign: 'right',
items: [{
xtype: 'combo',
forceSelection: true,
fieldLabel: '新闻栏目',
id: 'category',
name: 'category',
store: newscategoryStore,
valueField: 'category_id',
displayField: 'category_name',
triggerAction: 'all',
mode: 'local',
lazyRender: false,
hiddenName: 'categoryValue',
emptyText: '请选择栏目',
allowBlank:false,
blankText:'你丫必须选择',
anchor: '95%'
<%if( nNewsID > 0 && model != null){%>,
value: '<%=model.category%>'<%}%>
}]
})]
}), new Ext.Panel({
layout: 'column',
width: 827,
border: false,
items: [new Ext.Panel({
columnWidth: .5,
layout: 'form',
border: false,
labelWidth: 100,
labelAlign: 'right',
items: [{
xtype: 'textfield',
fieldLabel: '作者',
name: 'Author',
id: 'Author',
allowBlank: false,
blankText: '填写作者',
emptyText: '请输入作者',
growMax: 30,
forceSelection: true,
anchor: '95%'
<%if( nNewsID > 0 && model != null){%>,
value: '<%=model.n_author%>'<%}%>
}]
}), new Ext.Panel({
columnWidth: .5,
layout: 'form',
border: false,
labelWidth: 100,
labelAlign: 'right',
items: [{
xtype: 'textfield',
fieldLabel: '来源',
name: 'Source',
id: 'Source',
allowBlank: false,
blankText: '填写来源',
emptyText: '请输入新闻来源',
growMax: 30,
anchor: '95%'
<%if( nNewsID > 0 && model != null){%>,
value: '<%=model.news_source%>'
<%}%>
<%else {%>,value:'本站'<%} %>
}]
})]
}), new Ext.form.TextArea({
id: 'keeditor',
fieldLabel: '新闻内容',
width: 710,
height: 500
})],
buttons: [new Ext.Button(
{ text: '<%if( nNewsID > 0 && model != null){%>编辑新闻<%}else{ %>发布新闻<%} %>',
handler: function () {
<%if(nNewsID > 0 && model != null){ %>EditNews();<%} else{%>AddNews();<%} %>
}
})
],
listeners: {
'render': function () {
KE.show({
id: 'keeditor',
imageUploadJson: '../../asp.net/Service/upload_json.ashx',
fileManagerJson: '../../asp.net/Service/file_manager_json.ashx',
allowFileManager: true,
afterCreate: function (id) {
// !@Important
var temp=document.getElementById('content').value;
KE.html(id, temp);
KE.event.ctrl(document, 13, function () {
KE.util.setData(id);
document.forms['form1'].submit();
});
KE.event.ctrl(KE.g[id].iframeDoc, 13, function () {
KE.util.setData(id);
document.forms['form1'].submit();
});
}
});
}
}
});
//发布新闻
function AddNews() {
var strTitle = Ext.get("Title").dom.value;
var strAuthor = Ext.get("Author").dom.value;
// var strContent = encodeURI(new String(GetContents("content")));
// editor.sync();
var strContent = document.getElementById('keeditor').value;
var strSource = Ext.getCmp('Source').getValue();
var nCategory = Ext.getCmp('category').getValue();
if (strTitle.trim() == "" || strTitle == "请输入标题") {
Ext.Msg.alert("系统提示", '对不起,请输入新闻标题');
return;
}
else if(nCategory==""||nCategory<0){
Ext.Msg.alert("系统提示","请选择栏目");
return;
}
else if (strAuthor.trim() == "" || strAuthor == "请输入作者") {
Ext.Msg.alert("系统提示", '请输入作者');
return;
}
if (strContent.trim() == "") {
Ext.Msg.alert("系统提示", "对不起,请输入新闻内容");
return;
}
Ext.Ajax.request({
url: '../../../Service/NewsSrv.ashx',
method: 'POST',
params: {
type: 'addnews',
Title: strTitle,
Content: strContent,
Author: strAuthor,
Source: strSource,
Category: nCategory
}, success: function (response) {
var nSuccessCount = 0;
if (!isNaN(response.responseText)) {
nSuccessCount = parseInt(response.responseText);
}
else {
Ext.Msg.alert("系统提示", "对不起,发布失败");
}
if (nSuccessCount <= 0) {
Ext.Msg.alert("系统提示", "对不起,发布失败");
}
else {
Ext.Msg.alert("系统提示", "发布新闻成功");
//location.reload();
}
},
failure: function (response) {
Ext.Msg.alert("系统提示", "连接服务器失败");
}
});
}
//编辑新闻
function EditNews() {
var nNewsID = "";
<%if (nNewsID > 0 && model != null)
{%>
nNewsID = '<%=model.n_id %>';
<%
}%>
var strTitle = Ext.get("Title").dom.value;
var strAuthor = Ext.get("Author").dom.value;
// var strContent = encodeURI(new String(GetContents("content")));
// editor.sync();
var strContent = document.getElementById('keeditor').value;
var strSource = Ext.getCmp('Source').getValue();
var nCategory = Ext.getCmp('category').getValue();
if (strTitle.trim() == "" || strTitle == "请输入标题") {
Ext.Msg.alert("系统提示", '对不起,请输入新闻标题');
return;
}
else if(nCategory==""||nCategory<0){
Ext.Msg.alert("系统提示","请选择栏目");
return;
}
else if (strAuthor.trim() == "" || strAuthor == "请输入作者") {
Ext.Msg.alert("系统提示", '请输入作者');
return;
}
else if (strContent.trim() == "") {
Ext.Msg.alert("系统提示", "对不起,请输入新闻内容");
return;
}
Ext.Ajax.request({
url: '../../../Service/NewsSrv.ashx',
method: 'POST',
params:
{
type: 'edit',
id:nNewsID,
Title: strTitle,
Content: strContent,
Author: strAuthor,
Source: strSource,
Category: nCategory
}, success: function(response) {
var nSuccessCount = 0;
if (!isNaN(response.responseText)) {
nSuccessCount = parseInt(response.responseText);
}
else {
Ext.Msg.alert("系统提示", "对不起,编辑失败");
}
if (nSuccessCount < 0) {
Ext.Msg.alert("系统提示", "对不起,编辑失败");
}
else {
Ext.Msg.alert("系统提示","编辑新闻成功");
//location.reload();
}
},
failure: function(response) {
Ext.Msg.alert("系统提示", "连接服务器失败");
}
});
}
</script>
后台数据读取很简单:
public int nNewsID = -1;
public NewsInfo model = new NewsInfo();
protected void Page_Load(object sender, EventArgs e)
{
if (Request["id"] != null)
{
nNewsID = Convert.ToInt32(Request["id"].ToString()); //WebUtil.ParseInt(Request["id"].ToString().Trim(), -1);
if (nNewsID > 0)
{
model = NewsBLL.GetModelNewsByID(nNewsID);
content.Text = model.n_content.ToString();
}
}
}
实现异步的关键:ashx文件的处理:
<%@ WebHandler Language="C#" Class="NewsSrv" %>
using System;
using System.Web;
using MOD;
using BLL;
using WebUnity;
public class NewsSrv : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/Html";
context.Response.Charset = "UTF-8";
string strType = string.Empty;
//读取操作类型
if (!string.IsNullOrEmpty(context.Request.Params["type"].ToString()))
{
strType = context.Request.Params["type"].ToLower().ToString().Trim();
}
//读取不到操作则返回
if (string.IsNullOrEmpty(strType))
{
return;
}
switch (strType)
{
//添加新闻
case "addnews":
ProcessAddNews(context);
break;
//得到新闻栏目编号
case "getcategory":
ProcessGetCategory(context);
break;
//获得新闻列表
case "getnewslist":
ProcessGetNewsList(context);
break;
//删除新闻
case "delectnews":
ProcessDelectNews(context);
break;
//处理编辑新闻请求
case "edit":
ProcessEditNews(context);
break;
default:
break;
}
}
/// <summary>
/// 编辑新闻
/// </summary>
/// <param name="context"></param>
public void ProcessEditNews(HttpContext context)
{
int nNewsID = -1;
string strTitle = string.Empty;
string strAuthor = string.Empty;
string strContent = string.Empty;
string strSource = "本站";
int nCategory = -1;
DateTime dtDate = DateTime.Now;
//读取传送的数据
//获得编辑新闻ID、
if (context.Request.Params["id"].ToString() != null)
{
nNewsID = Convert.ToInt32(context.Request.Params["id"].ToString());
}
//获得新闻标题
if (context.Request.Params["Title"].ToString() != null)
{
strTitle = context.Request.Params["Title"].ToString();
}
//获得新闻作者
if (context.Request.Params["Author"].ToString() != null)
{
strAuthor = context.Request.Params["Author"].ToString();
}
//获得新闻内容
if (context.Request.Params["Content"].ToString() != null)
{
strContent = context.Request.Params["Content"].ToString().ToLower().Trim();
}
//获得新闻来源
if (context.Request.Params["Source"].ToString() != null)
{
strSource = context.Request.Params["Source"].ToString();
}
//获得新闻栏目
if (Convert.ToInt32(context.Request.Params["Category"].ToString()) >= 0)
{
nCategory = Convert.ToInt32(context.Request.Params["Category"].ToString());
}
dtDate = DateTime.Now;
NewsInfo model = new NewsInfo();
model.n_id = nNewsID;
model.news_source = strSource;
model.category = nCategory;
model.guild_id = 0;//暂时为0,当选择公会新闻时显示公会选择
model.n_title = strTitle;
model.published_time = dtDate;
model.n_author = strAuthor;
model.n_content = strContent;
model.click_num = 0;//暂时为0,添加点击量
model.media_id = 0;//暂时为0,此处为添加媒体图片时的第一个ID值
context.Response.Write(NewsBLL.UpdataNews(model));
}
/// <summary>
/// Delect News
/// </summary>
/// <param name="context"></param>
public void ProcessDelectNews(HttpContext context)
{
string strIDCollection = string.Empty;
if (context.Request["n_idlist"] != null)
{
strIDCollection = context.Request["n_idlist"].ToString().Trim();
}
int nRet = 0;
if (!string.IsNullOrEmpty(strIDCollection))
{
if (strIDCollection.Substring(0, 1) == ",")
{
strIDCollection = strIDCollection.Substring(1);
}
nRet = NewsBLL.DeleteNews(strIDCollection);
}
context.Response.Write(nRet);
}
/// <summary>
/// 添加新闻
/// </summary>
/// <param name="context"></param>
public void ProcessAddNews(HttpContext context)
{
string strTitle = string.Empty;
string strAuthor = string.Empty;
string strContent = string.Empty;
string strSource = "本站";
int nCategory = -1;
DateTime dtDate = DateTime.Now;
//读取传送的数据
//获得新闻标题
if (context.Request.Params["Title"].ToString() != null)
{
strTitle = context.Request.Params["Title"].ToString();
}
//获得新闻作者
if (context.Request.Params["Author"].ToString() != null)
{
strAuthor = context.Request.Params["Author"].ToString();
}
//获得新闻内容
if (context.Request.Params["Content"].ToString() != null)
{
strContent = context.Request.Params["Content"].ToString().ToLower().Trim();
}
//获得新闻来源
if (context.Request.Params["Source"].ToString() != null)
{
strSource = context.Request.Params["Source"].ToString();
}
//获得新闻栏目
if (Convert.ToInt32(context.Request.Params["Category"].ToString()) >= 0)
{
nCategory = Convert.ToInt32(context.Request.Params["Category"].ToString());
}
dtDate = DateTime.Now;
NewsInfo model = new NewsInfo();
model.news_source = strSource;
model.category = nCategory;
model.guild_id = 0;//暂时为0,当选择公会新闻时显示公会选择
model.n_title = strTitle;
model.published_time = dtDate;
model.n_author = strAuthor;
model.n_content = strContent;
model.click_num = 0;//暂时为0,添加点击量
model.media_id = 0;//暂时为0,此处为添加媒体图片时的第一个ID值
context.Response.Write(NewsBLL.AddNews(model));
}
/// <summary>
/// 得到新闻栏目表
/// </summary>
/// <param name="context"></param>
public void ProcessGetCategory(HttpContext context)
{
int nNewsType = int.Parse(context.Request.Params["nNewsType"].ToString());
//输出信息(JSON格式)
context.Response.Write(CategoryBLL.GetCategoryList(nNewsType));
}
/// <summary>
/// 抽取新闻列表
/// </summary>
/// <param name="context"></param>
public void ProcessGetNewsList(HttpContext context)
{
//读取查询条件
//1、读取标题
string strTitle = string.Empty;
if (context.Request.Params["strTitle"] != null)
{
strTitle = context.Request.Params["strTitle"].ToString().Trim();
}
//2、读取栏目名
string strCategory = string.Empty;
if (context.Request.Params["strCategory"] != null)
{
strCategory = context.Request.Params["strCategory"].ToString().Trim();
}
//3、读取作者
string strAuthor = string.Empty;
if (context.Request.Params["strAuthor"] != null)
{
strAuthor = context.Request.Params["strAuthor"].ToString().Trim();
}
//4、读取来源
string strSource = string.Empty;
if (context.Request.Params["strSource"] != null)
{
strSource = context.Request.Params["strSource"].ToString().Trim();
}
//5、读取读取分页信息
int nPage = 1;
int nPageSize = PageSize.Admin;
try
{
if (context.Request.Params["limit"] != null)
{
nPageSize = int.Parse(context.Request.Params["limit"].ToString().Trim());
}
else
{
nPageSize = PageSize.Admin;
}
}
catch (Exception ex)
{
nPageSize = PageSize.Admin;
}
int nStart = 0;
try
{
if (context.Request.Params["start"] != null)
{
nStart = int.Parse(context.Request.Params["start"].ToString().Trim());
}
else
{
nStart = 0;
}
}
catch (Exception ex)
{
nStart = 0;
}
if (nStart <= 0)
{
nPage = 1;
}
else
{
nPage = nStart / nPageSize + 1;
}
//输出信息(JSON格式)
context.Response.Write(NewsBLL.GetNewsList(strTitle, strCategory, strAuthor, strSource, nPage, nPageSize));
}
public bool IsReusable
{
get
{
return false;
}
}
}
实现效果如下:
其中很关键的几点:
KE.html(id, value);