ASP CKEditorControl's content length on client-side - asp.net

I'm using CKEditor in my ASP.NET webforms application. I'm using CKEditor control to put it to my page:
<CKEditor:CKEditorControl ID="CKEditorControl" runat="server" />
But I'd like to get the editor's content length on client-side. I'm trying to do it this way:
CKEDITOR.instances['<%= CKEditorControl.ClientID %>'].getData().length;
Though it's not working because: "Uncaught TypeError: Cannot call method 'getData' of undefined"
So my question is. What am I doing wrong? And is it possible to get the CKEditor's length on client-side when using CKEditorControl?

this code works:
$('#cke_<%= CKEditorControl.ClientID %> iframe').contents().find('body').html()
I'm not sure now but I guess all my problems were because I was trying to get the data before the editor was loaded.

Related

Is this asp compiled somehow?

I have an aspx document (I know nothing about asp, .net, aspx, nada). It is a normal html table structure for the most part, but there are strings of asp that seem to be inserting some sort of dynamic content. They are in the form:
<asp:Image ID="imgTopImage" runat="server" ImageUrl="~/Images/topbar.jpg" />
<asp:Label ID="lblStyleCaption" runat="server" CssClass="label_caption" Text="Theme: " Visible="false" />
<asp:DropDownList ID="dropStyles" Width="150" runat="server" AutoPostBack="true" />
It seems that whenever I delete one of these——something as innocuous as, say, the line with the asp:Image tag, which I would think should just remove the image, when I load the page I get run-time errors. It's very particular. My question is, is this compiled somehow, which is making it so fragile. Even just changing the topbar.jpg to something.png gives me an error. Do I need to track down the original files this was compiled from, or is this normal server-side asp(x?) that I'm just somehow else goofing up my changes to?
ASPX pages are compiled, and those tags refer to objects that are known to the server, so removing them could cause errors.
First, some basics in layman's terms
Tags that begin with ASP: (Example, <ASP:Button id="btnSubmit" runat="Server" Text="Click Me" />)
are not standard html buttons. They are server controls. When generating the html that goes out to the browser, the ASP.NET runtime looks at the server controls and creates the appropriate content depending on the browser visiting the page.
In the case of the Button control, it's usually a standard html button, but the runtime also generates the JavaScript and such to handle the button's server-side click event.
Why you're probably seeing errors when you remove a control:
Quite often, there's server-side code that's written that accesses these controls. For example, the developer may have decided to change the Text or the Visible property due to some event.
If this is the case, and you remove the <asp:Button> tag, then there will be server-side code that references an object that no longer exists in the aspx page, hence the errors.
More at these links on Server Controls:
http://www.w3schools.com/aspnet/aspnet_controls.asp
(Actually, this older one is better for a new-to-asp.net developer: http://msdn.microsoft.com/en-us/library/zsyt68f1(VS.71).aspx
http://support.microsoft.com/kb/306459
I'd also recommend taking some time watching basic videos or going through the tutorials at http://www.asp.net/get-started
I just noticed this in your question:
Even just changing the topbar.jpg to something.png gives me an error.
That is a bit odd, but I know of at least one way it could happen...
Generally, Visual Studio will give you a warning (and not an error) if you include a relative URL to an image or a linked page that doesn't exist. The warning shouldn't block you from compiling. However, Visual Studio does have a setting that tells it to treat warnings as errors. That will block it from compiling. Here's how that would be set up:
from Project Settings> Configuration Properties select the build
setting and change the “treat warnings as errors” settings to true.
If you wish to NOT treat warnings as errors, simply change the setting to false.

ASP.NET inline code in a server control

Ok, we had a problem come up today at work. It is a strange one that I never would have even thought to try.
<form id="form1" runat="server" method="post" action="Default.aspx?id=<%= ID %>" >
Ok, it is very ugly and I wouldn't have ever tried it myself. It came up in some code that was written years ago but had been working up until this weekend after a bunch of updates were installed on a client's web server where the code is hosted.
The actual result of this is the following html:
<form name="form1" method="post" action="Default.aspx?id=<%= ID %>" id="form1">
The url ends up like this:
http://localhost:6735/Default.aspx?id=<%= ID %>
Which as you can see, demonstrates that the "<" symbol is being encoded before ASP.NET actually processes the page. It seems strange to me as I thought that even though it is not pretty by any means, it should work. I'm confused.
To make matters worse, the client insists that it is a bug in IE since it appears to work in Firefox. In fact, it is broken in Firefox as well, except for some reason Firefox treats it as a 0.
Any ideas on why this happens and how to fix it easily? Everything I try to render within the server control ends up getting escaped.
Edit
Ok, I found a "fix"
<form id="form1" runat="server" method="post" action='<%# String.Format("Default.aspx?id={0}", 5) %>' >
But that requires me to call DataBind which is adding more of a hack to the original hack. Guess if nobody thinks of anything else I'll have to go with that.
ASP.NET 3.5 added the "Action" property to the HtmlForm control. Your previous code worked just fine because "action" was just a string, and the code nugget would emit the additional data for you. Now that there is an Action property, you can't use a simple code emit nugget, since a server-side control expects a property to have a literal string value (same as any other server-side control property).
Your workaround of using the biding syntax is correct. To make it work like it used to, you would have to remove the runat=server tag on your form, which would then prevent the ASPX parser from treating it as an HtmlForm control, and instead treat it as a literal (where your code emit nugget would be allowed to work).
Your other option, which may be much cleaner - is to simply set the Form's action property through code-behind in the page_load. The reason the action property is set the way it is, was because in earlier versions of .NET Framework, there was no support for setting the Action property.
your form needs a runat=server

Explicit localization problem

when trying to translate the confirmation message to Norwegian i get the following error:
Cannot have more than one binding on property 'OnClientClick' on 'System.Web.UI.WebControls.LinkButton'. Ensure that this property is not bound through an implicit expression, for example, using meta:resourcekey.
i use Explicit localization in the following manner:
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="<%# Resources: lnkMarkInvoicedResource.OnClientClick%>"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
here's the local resource file entry:
<data name="lnkMarkInvoicedResource.OnClientClick" xml:space="preserve">
<value>return confirm('Er du sikker?');</value>
if i remove the meta attribute i get the English text(default).
how do i get the Norwegian text appearing without resorting to using the code behind?
Update:
removing the meta attribute prevents the exception from occurring but the original problem still exists. I can't get the Norwegian text to show.
only the default English text shows.
Another Update:
I know this question is getting old but i still can't get the Norwegian text to display.
If anyone has some tips please post a response.
Looks like you're making the problem harder by inlining the onclick. Why not split it out to a separate line?
<script>
function markInvoiced()
{
return confirm('<%= Resources.SomehowGet.lnkMarkInvoicedResource.OnClientClick%>');
}
</script>
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="return markInvoiced();"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
And while we're looking at your code, you realize that you're essentially building an <a> tag, right? As such, why not just build an <a> and save yourself some grief?
And finally, next project why not ditch the built-in ASP.NET localization nighmare in favor of something sane like FairlyLocal, in which case you'd write this:
<a href="#" onclick="return confirm(<%=_"really?"%>) onserverclick="lnkMarkInvoiced_OnClick" runat="server">
<%=_("Mark Invoice")%>
</a>
Are you using the .NET resource manager and satellite assemblies to store your localized resources? It looks like you have hard-coded the alternative language in your markup, rather than storing it in a language-specific resources assembly...
.NET has some extremely rich localization and globalization capabilities. If you use them properly, localization should be a pretty automatic thing (assuming your client is providing their language code as part of the HTTP headers). Even if your client has not configured their browser with the appropriate language, it is still easy enough to manually change the UI culture via a user request (clicking a flag icon, configuring a setting, etc.)
This article might be helpful: ASP.NET Web Page Resources Overview
That meta tag is using implicit localization when you're using explicit localization in the OnClientClick. You will need to choose one method or the other. If you are to use explicit localization, you'll need to do the necessary work to set the proper culture info in your application in the code-behind.

Attribute "runat" is not a valid attribute. Did you mean "content" or "target"? How to solve this asp.net validation error

see here this error - http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.sitecore.net%2F
runat="server" is asp.net server control syntax. It must not come in html. It is interpreted by ASP.NET. you must remove this attribute.
Possible Reason for this:
1. I think the template is dynamically created. the developer make static site and cut copy paste on server side to make it dynamic but use control as response.write and forgot to remove runat="server" because it must be html content in response.write.
NOTE: No ASP.NET server control gives runat="server" in HTML. It is hardcoded in your code. remove this from both anchor and image tag.
Er... remove the attributes? They aren't valid HTML, and they're only meaningful when interpreted by ASP.NET.
Your pages should not be rendered with runat="server", so something's definitely going wrong here. What does the part of your aspx look like, that corresponds to one of the elements that is giving this validation error?

Inline script does not resolve in ASP.Net custom control

Currently I am working with a custom regular expression validator (unfortunately).
I am trying to set the Regex pattern using a server side inline script like this:
ValidationExpression="<%= RegExStrings.SomePattern %>"
However, the script is not resolving to server side code. Instead it is being interpreted literally and I end up with something like this in the rendered markup:
ctl00_DefaultContent_regexValidatorInvitation.validationexpression = "<%= RegExStrings.SomePattern %>";
Any clues as to why this is not resolving properly?
This has now been cleared up my MS. The issue I discovered was caused by the fact that the "action" attribute in server forms had no effect prior to .NET 2 SP2, but now can be set. Code render blocks have never worked in attribute values - this is explained towards the end of this post.
This was a consequence of a deliberate change in behaviour introduced in Microsoft .NET Framework 3.5 SP1. Prior to the service pack, action and method attributes on server side FORM tags could not be over-ridden. If specified they would be replaced by ASP.NET with "POST" and "page name".
Previously, the ASP.NET page parser did not prevent one specifying these attributes although the documentation advised against it for the action attribute:
http://msdn.microsoft.com/en-us/library/k33801s3.aspx
In particular the comment (in the context of the FORM element):
• "The opening tag must not contain an action attribute. ASP.NET sets these attributes dynamically when the page is processed, overriding any settings that you might make. "
The issue that was originally reported by Josh, where the code block was not being interpreted is not new behaviour but is a known bug - code render blocks cannot be used within server control attributes. This is reported as a "Connect" bug:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=109257
which contains the following:
" Attributes of server controls cannot take an inline expression as value. This explains the unexpected behaviour as seen with: " <link href="<%=RootPath %> ..." However, inline code can be used for values of attributes."
But why is this? I can reproduce your problem using a simple aspx page as below:
<%# Page language="c#" AutoEventWireup="true" %>
<html>
<body >
<form id="Form1" method="post" runat="server" action="?<%=Request.QueryString%>">
Query String value: <%=Request.QueryString %>
<br />
<input type=submit />
</form>
</body>
</html>
This displays the following after submitting the form:
Query String value:
%3c%25=Request.QueryString%25%3e
For some reason, the inline code is not executed when the
runat="server" is present. The strange thing is I have 3 machines
that do not behave this way and one that does, so I can only assume
that this is an IIS/.NET config issue, possibly caused by a recent MS
Update. The software I have installed recently on the machine
exhibiting this behaviour is:
Visual Studio 2008
WSE 3.0
IE8 RC1
I wonder if any of these have caused this?
devstuff, that doesn't explain why this works on 3 of my machines, but not a 4th one, does it? They are all using the same .NET Framework version and IIS settings (I believe, having checked as many as I coukd)
I've uninstalled the .NET framework while investigating this (3.5 then 3.0 and 2.0) - I then had no problems after
Installing each of the following:
.net framework 2.0
.net framework 2.0 SP1
.net framework 3.0
.net framework 3.0 SP1
.net framework 3.5
But after I installed .net framework 3.5 SP1 the behaviour returned – I guess this is the issue. I have raised this with Microsoft and will update this thread when I get a response.
Values in a web control do not render server side code.
Rather set that from the Code Behind
RegExValidator1.ValidationExpression = RegExStrings.SomePattern;
It's being taken as a literal string, try
ValidationExpression='<%= RegExStrings.SomePattern %>'
Edit: The above doesn't work, I've tried to see how to do this without success,I usually set properties in the code-behind and only use this syntax for databinding when I have to. I'd be interested to know if it can be done too.
If your regular expression validator has the runat="server" attribute, then change it from the code behind. It would be much easier.
You're using a databinding expression on a control that is not databound. You need to call DataBind(), or use an ExpressionBuilder implementation. A simple ExpressionBuilder for binding to arbitary code can be found at http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx

Resources