views:

456

answers:

3

On the web page I try to initialize TinyMCE for a specifice asp:textbox web control.

This is the javascript in the header:

  tinyMCE.init({
        mode: "exact",
        elements: '<% = txtBody.ClientID %>',

The problem is that the txtBody control is generated inside template of DetailsView control:

<asp:TemplateField HeaderText="Body" SortExpression="Body">           
   <EditItemTemplate>
      <asp:TextBox ID="txtBody" runat="server" Rows="10" TextMode="MultiLine" Width="100%" Height="400px"></asp:TextBox>                  
   </EditItemTemplate>
</asp:TemplateField>

So txtBody.ClientID can't find it on the loading stage.

A: 

Put tinyMCE's loading sequence into the onload event. There are more elegant ways to do this using one of the JS frameworks, but the basic way is

document.onload = function() { tinyMCE.init ...... }

in the <head> part of your document.

Pekka
But I get compile error:The name 'txtBody' does not exist in the current context
kenny
the error is on the line: '<% = txtBody.ClientID %>',
kenny
A: 

You can use TinyMce server control instead of a simple TextBox. You can find here or another one that written by myself, here.

Mehdi Golchin
downloaded tinymce_package_dotnet_1_0a2.zip, and added the dll to the toolbox, then moved the control on the page but got Error Creating Control
kenny
Maybe I can help, if you post the error message.
Mehdi Golchin
The type or namespace name 'ICSharpCode' could not be found (are you missing a using directive or an assembly reference?) Do i need to add something else beside, Moxiecode.TinyMCE.dll? (I removed the tinymce folder and tinyMCE.init from the web page)
kenny
`ICSharpCode` is the root namespace of `CSharpZipLib` and IMO does not have any relation with TinyMce. Does it work at run-time?
Mehdi Golchin
no, this is the error that is displayed on compile time.Can you please try to open web project and this control yourself?
kenny
That works for me. Are you using `CSharpZipLib` in your project?
Mehdi Golchin
no, just the Moxiecode.TinyMCE.dll
kenny
Required installPath setting is missing, add it to your web.config. You can also add it directly to your tinymce:TextArea element using InstallPath but the web.config method is recommended since it allows you to switch over to gzip compression.
kenny
Maybe I need to set some thing in web.config? maybe i didn't download the right package(inymce_package_dotnet_1_0a2.zip)?
kenny
Sorry for delay. Yes, you should set some configuration in the web.config. Check the `tinymce_package_dotnet_1_0a2\tinymce\examples\web.config`.
Mehdi Golchin
Not I get this: Failed to map the path '/js/tiny_mce/tiny_mce_src.js'.So DO I need the javascript folder? I thought there is no need of it and the control has it embedded!
kenny
No, you should copy/paste the tinymce directory in the root of your application.
Mehdi Golchin
oh, so what is the point for this control if it's not a stand alone?
kenny
It makes using TinyMce editor simply and IMO, resolves the ClientId problem you have.
Mehdi Golchin
There is another way too. Set the mode to `textareas` while you have only a textarea in your page. `tinyMCE.init({ mode: "textareas",`
Mehdi Golchin
I have many textboxes on the page and i need to apply tinymce only on one.
kenny
A: 

For know I solved this like this:

For the textbox I added OnInit="txtBody_OnInit" event:

protected void txtBody_OnInit(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(GetType(), "SetEditor", string.Format("SetEditor('{0}');", ((TextBox)sender).ClientID), true);
}

And then put tinymce initialization inside SetEditor(theID) javascript function.

It's not the perfect answer so if there is a better answer I'll accept it.

kenny