.net TinyMCE with AJAX(更新面板)永远不会有值
我想为更新面板内的文本区域使用富文本编辑器 我找到了这个帖子:通过这个问题: 我决定使用TinyMCE,就像我以前在非AJAX情况下使用它一样,它在列表中说它与AJAX兼容。好的,我做了很好的ol'.net TinyMCE with AJAX(更新面板)永远不会有值,.net,javascript,asp.net-ajax,updatepanel,tinymce,.net,Javascript,Asp.net Ajax,Updatepanel,Tinymce,我想为更新面板内的文本区域使用富文本编辑器 我找到了这个帖子:通过这个问题: 我决定使用TinyMCE,就像我以前在非AJAX情况下使用它一样,它在列表中说它与AJAX兼容。好的,我做了很好的ol'tinyMCE.init({//settings here}) 测试它,它会在执行更新面板更新后消失。我从这里的一个问题中了解到,它应该位于page\u load函数中,这样它甚至可以在异步回发中运行。好的,这样面板就不会动了。但是,在尝试从我的textarea提交值时,它的文本总是返回为空,因为我的
tinyMCE.init({//settings here})代码>
测试它,它会在执行更新面板更新后消失。我从这里的一个问题中了解到,它应该位于page\u load
函数中,这样它甚至可以在异步回发中运行。好的,这样面板就不会动了。但是,在尝试从我的textarea提交值时,它的文本总是返回为空,因为我的表单验证程序总是说“您必须输入描述”,即使我在其中输入文本也是如此。这发生在页面第一次加载时以及对页面执行异步回发后
好的,我找到了这个。我尝试在tinyMCE.init之后将此代码添加到页面加载函数中。这样做会破坏我的jquery在页面加载之后被调用的所有功能,它仍然存在同样的问题
我仍然是客户端脚本编写的新手,所以也许我需要将代码放在不同的位置,而不是页面加载?我不确定我链接的帖子不太清楚该把代码放在哪里
我的Javascript:
<script type="text/javascript">
var redirectUrl = '<%= redirectUrl %>';
function pageLoad() {
tinyMCE.init({
mode: "exact",
elements: "ctl00_mainContent_tbDescription",
theme: "advanced",
plugins: "table,advhr,advimage,iespell,insertdatetime,preview,searchreplace,print,contextmenu,paste,fullscreen",
theme_advanced_buttons1_add_before: "preview,separator",
theme_advanced_buttons1: "bold,italic,underline,separator,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,link,unlink,separator,styleselect,formatselect",
theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,separator,removeformat,cleanup,charmap,search,replace,separator,iespell,code,fullscreen",
theme_advanced_buttons2_add_before: "",
theme_advanced_buttons3: "",
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
extended_valid_elements: "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
paste_auto_cleanup_on_paste: true,
paste_convert_headers_to_strong: true,
button_tile_map: true
});
tinyMCE.triggerSave(false, true);
tiny_mce_editor = tinyMCE.get('ctl00_mainContent_tbDescription');
var newData = tiny_mce_editor.getContent();
tinyMCE.execCommand('mceRemoveControl', false, 'your_textarea_name');
//QJqueryUI dialog stuff
}</script>
我怎样才能得到这些值,这样后面的代码才能真正得到文本区域中键入的内容,而我的验证器不会说它是空的?即使在异步回发之后,因为我在表单上有多个按钮在实际提交之前更新它
谢谢
编辑:为了进一步澄清,我在后端进行了表单验证,如下所示:
If tbDescription.Text = "" Or tbDescription.Text Is Nothing Then
lblDescriptionError.Text = "You must enter a description."
isError = True
Else
lblDescriptionError.Text = ""
End If
此错误将始终导致显示错误消息
编辑:
好吧,我在这里越来越绝望了,我在这上面花了好几个小时。我最终在专家交换中找到了我认为是赢家的东西,它声明如下(有一部分是关于用xml编码值的,但我跳过了这一部分):
对于希望将tinyMCE与AJAX.Net结合使用的任何人:
将开始/结束处理程序附加到AJAX请求对象。这些将在发送数据之前删除tinyMCE控件(开始),并重新创建tinyMCE控件(结束):
现在,我已经尝试过将它放在自己的脚本标记中,将开始和结束请求放在自己的脚本标记中,将ed.onChange放在页面加载中,将所有内容放在页面加载中,并将所有3个都放在自己的脚本标记中。在所有情况下,它都不起作用,甚至有时会破坏我的页面加载中的jquery。。。(是的,我更改了上述代码以适应我的页面)
有人能让这个工作或提供解决方案吗
代码我想你应该看看这个帖子:
确保使用scriptmanager
ScriptManager.RegisterStartupScript(this.Page,
this.Page.GetType(), mce.ClientID, "pageLoad();", true);
我有两种方法来解决这个问题,但实际上并没有解决问题,只是避免了它
一个是使用FCKeditor.net控件,这个控件的重新加载速度非常慢,对我来说,大约2-3秒。所以我决定让表单有两个更新面板,然后把文本区域放在中间,基本上把文本区域从更新面板中去掉。这感觉有点像一个廉价的把戏,不应该是必要的,但它工作得很好。任何人发布的解决方案或建议都不适合我,所以我就是这么做的。但是,如果我必须将文本区域放入更新面板中,这将不起作用。在将TinyMCE放入更新面板时,我遇到的一件事是,我必须采取各种技巧使其正常工作。请记住更新面板是如何工作的,当您想要刷新它时,它会完全替换其中的DOM,包括TinyMCE
我使用的解决方法是从更新面板中删除TinyMCE,并将需要在更新面板中动态刷新的任何其他内容包装起来。然后,如果我需要TinyMCE内容是动态的,我会通过它提供的javascript API向TinyMCE添加数据。回传时必须触发保存功能,使用来注册脚本TinyMCE.triggerSave()代码>
我注意到tinyMCE的init
函数只能在选择所有textareas或具有特定类的textareas时调用。精确的
不起作用。我只想在这篇文章中添加我的解决方案,因为我已经为同一个问题绞尽脑汁好几天了。我意识到这是一篇老文章,但也许我的回答会对某人有所帮助,因为我相信这个问题仍然是相关的
我正在开发一个ASP.NET web表单应用程序,其中一个页面的UpdatePanel中包含一个textarea控件。tinyMCE绑定到此文本区域。textarea的文本来自repeater控件内的绑定文本框,因为我想从ObjectDataSource控件获取文本,这是一种稍微笨拙的方法。在我看来,ObjectDataSource控件很方便,执行速度也很快
这是我的标记,包含ObjectDataSource控件、repeater、绑定的textbox和textarea(asp:textbox设置为multiline)。请注意,绑定的文本框设置为“显示:无”:
以下是单击“保存”按钮时激发的服务器代码的一部分:
Private Sub Save()
'Retrieve tinyMCE text from cookie
Dim cookieName As String = "tinyMCEText"
Dim cookies As HttpCookieCollection = Request.Cookies
Dim text As String = cookies(cookieName).Value
'Save text to database...
'Delete cookie
cookies.Remove(cookieName)
'Databind text for tinyMCE
repeaterTinyMCE.DataBind()
Dim encodedText As String = DirectCast(repeaterTinyMCE.Controls(0).Controls(1), TextBox).Text
textboxTinyMCE.Text = encodedText
End Sub
希望这对其他人有所帮助。我已经看到了这个线程,我已经做到了,如果您阅读了标记答案下面的答案,它会说您可以注册启动脚本或将其放入page_load JavaScript函数中。我选择了后者,因为我并没有和那个人一样的问题。那个人在回发后失去了控制,我没有这个问题,我的问题是,即使在一次回发之前(以及之后),当我填写文本区域并尝试提交时,它告诉我tbDescription.text是空的。部分var textToSave=tinyMCE.activeEdito
ed.onChange.add(function(ed, l) {
tinyMCE.triggerSave(true, true);
});
ScriptManager.RegisterStartupScript(this.Page,
this.Page.GetType(), mce.ClientID, "pageLoad();", true);
<asp:ObjectDataSource ID="odsDetailText" runat="server" TypeName="Data.Document" SelectMethod="GetDocumentDetailText" />
<asp:Repeater ID="repBody" runat="server" DataSourceID="odsDetailText">
<ItemTemplate>
<asp:TextBox ID="tbxBodyBound" runat="server" Text='<%# Eval("Body") %>' CssClass="hidden" />
</ItemTemplate>
</asp:Repeater>
<asp:TextBox ID="tbxBody" runat="server" TextMode="MultiLine" />
// #########################################################
// Events
// #########################################################
// ---------------------------------------------------------
// Check for full and partial postbacks
// ---------------------------------------------------------
function pageLoad(sender, args) {
// Register event handler for async postback beginning
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (!prm.get_isInAsyncPostBack()) {
prm.add_beginRequest(onBeginRequest);
};
// Configure HTML editor
HTMLEditorConfig();
// Format HTML editor text
HTMLEditorFormat();
};
// ---------------------------------------------------------
// When page unloads after full or partial postback
// ---------------------------------------------------------
function pageUnload(sender, args) {
// Deregister event handler for async postback beginning
Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(onBeginRequest);
};
// ---------------------------------------------------------
// Event handler for async postback beginning
// ---------------------------------------------------------
function onBeginRequest() {
// Check whether to save text editor text
HTMLEditorSave();
};
// #########################################################
// Functions
// #########################################################
// ---------------------------------------------------------
// Configure HTML text editor. tinyMCE converts standard textarea controls
// ---------------------------------------------------------
function HTMLEditorConfig() {
// Determine edit mode
var editMode = $('input:hidden[id*=hfEditMode]').val().toLowerCase();
// If not in edit mode, prevent edits
var editorReadOnly = null;
var editorHeight = null;
if (editMode == 'true') {
editorReadOnly = '';
editorHeight = '332';
} else {
editorReadOnly = 'true';
editorHeight = '342';
};
// Initialise HTML text editor
tinyMCE.init({
mode: "textareas",
plugins: "advhr,insertdatetime,print,preview,fullscreen",
width: "488",
height: editorHeight,
// Theme options
theme: "advanced",
theme_advanced_buttons1: "newdocument,|,print,preview,|,cut,copy,paste,|,undo,redo,removeformat,|,bold,italic,underline,strikethrough,sub,sup,|,forecolor,backcolor",
theme_advanced_buttons2: "justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,|,fontselect,fontsizeselect",
theme_advanced_buttons3: "insertdate,inserttime,|,advhr,|,charmap,|,fullscreen",
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_statusbar_location: "none",
theme_advanced_resizing: false,
// Skin options
skin: "o2k7",
skin_variant: "silver",
// Custom css
content_css: "../../Script/tiny_mce/custom.css",
// Allow edits?
readonly: editorReadOnly
});
};
// ---------------------------------------------------------
// Format HTML editor text by ensuring its HTML is decoded
// ---------------------------------------------------------
function HTMLEditorFormat() {
// Check bound textbox containing HTML for text editor
var bodyText = $('input:text[id*=tbxBody]').val();
// If HTML exists, decode it
if (bodyText !== null) {
tinyMCE.activeEditor.setContent(decodeURIComponent(bodyText));
};
};
// ---------------------------------------------------------
// Save HTML text editor text to cookie for server-side processing.
// Can't save to hidden field or asp control as this function fires after viewstate is captured (I think).
// Extra content in viewstate would slow down page load anyway.
// ---------------------------------------------------------
function HTMLEditorSave() {
// Determine edit mode
var editMode = $('input:hidden[id*=hfEditMode]').val().toLowerCase();
// If in edit mode, create cookie with encoded text editor HTML. Server code will save this to database.
if (editMode == 'true') {
var textToSave = tinyMCE.activeEditor.getContent();
$.cookie('HTMLEditorText', textToSave);
}
};
Private Sub Save()
'Retrieve tinyMCE text from cookie
Dim cookieName As String = "tinyMCEText"
Dim cookies As HttpCookieCollection = Request.Cookies
Dim text As String = cookies(cookieName).Value
'Save text to database...
'Delete cookie
cookies.Remove(cookieName)
'Databind text for tinyMCE
repeaterTinyMCE.DataBind()
Dim encodedText As String = DirectCast(repeaterTinyMCE.Controls(0).Controls(1), TextBox).Text
textboxTinyMCE.Text = encodedText
End Sub