Setting Content in RadEditor - c#

I have visited the Telerik's website and viewed their demos etc...
But I am having problems trying to load content (html) in the RadEditor.
I have a Button_Click event where I get my html string and then set it to the RadEditor. The RadEditor is inside a RadWindow and only becomes visible when the button is clicked.
protected void btnSubmitHtml_Click(object sender, EventArgs e)
{
RadEditor1.Content = "<p>hello there</p>";
RadWindow1.Visible = true;
}
This doesn't show the html inside the RadEditor for some odd reason. I suspect it is the page life cycle that is involved with this problem.
Are there any suggestions to solve this?

I have encountered this problem multiple times and never found a "Proper" resolution.
However, a great work around is to simply set the content from the clientside via injected script. The end result is the same, and if you can tolerate the 10 millisecond delay, worthy of consideration.
EDIT after comment requested reference
Basically all you need to get an instance of the editor using ASP.NET WebForms $find function. That takes the html ID of the root of the rendered object and returns the client side viewModel if one exists.
The $(setEditorInitialContent) call at the end assumes that jQuery is present and delays the execution of the function till page load.
<telerik:radeditor runat="server" ID="RadEditor1">
<Content>
Here is sample content!
</Content>
</telerik:radeditor>
<script type="text/javascript">
function setEditorInitialContent() {
var editor = $find("<%=RadEditor1.ClientID%>"); //get a reference to RadEditor client object
editor.set_html("HEY THIS IS SOME CONTENT INTO YOUR EDITOR!!!!");
}
$(setEditorInitialContent);
</script>

Take a look here to see how to get a RadEditor to work in a RadWindow: http://www.telerik.com/help/aspnet-ajax/window-troubleshooting-radeditor-in-radwindow.html.
Said shortly, here is what you need to have in the OnClientShow event of the RadWindow:
function OnClientShow()
{
$find("<%=RadEditor1.ClientID %>").onParentNodeChanged();
}

To edit Html code only you can add -
EnableTextareaMode="true"
Add this property to the RadEditor.
I suspect that the way the control tries to interpret the html might be one of the problems. The other thing that may be causing this problem is the page life cycle.

Related

Disable postback of certain properties of Telerik RadEditor control

We have a page with Telerik RadEditor on a tab strip. There are scenarios when RadEditor contains a lot of html and when doing a post back in order to switch the tab, all its contents is being post back to the server. This results in gigantic performance loss (there are times when post backs are sending tens of MiB of data).
Is it possible to tweak RadEditor in such a way that it does not send its contents over to server on postbacks? Our code-behind does not rely on RadEditors Content property accessor (does not read its content explicitly), only its mutator (its contents are set from within the control's code-behind).
Is it even possible to do such things with any of Telerik controls and if it is, then how do we achieve such result?
It's worth pointing out that we use relatively old Telerik UI version (2013.2.611.35) and we can't switch to a newer version at the moment.
Thank you in advance.
Consider using the ContentUrl of the PageViews. This will let you load separate pages in iframes, so they will postback independently of the main page. Thus, you can have a standalone page with the editor and standalone pages for your other tabs.
On the possibility to exclude something from the POST request - I don't know of a way to do this, as it is not supposed to happen. The whole point is to transfer the current page state to the server.
Another option you may consider is using AJAX and the PageRequestManager's beingRequest event to try to blank out the editor. I have not tried it and I do not know whether it will actually work out, since so much data may simply be too much for the JS engine to process before the postback begins. Here is a bit of code that illustrates the idea:
var currContent = null;
function BeginRequestHandler(sender, args) {
var editor = $find("<%=RadEditor1.ClientID%>");
currContent = editor.get_html(true);
editor.set_html("");
}
function EndRequestHandler(sender, args) {
var editor = $find("<%=RadEditor1.ClientID%>");
editor.set_html(currContent);
currContent = null;
}
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

ASP.NET, ScriptManager, History Points and Dynamic Javascript

I have an ASP.NET user control that I want to write some dynamic Javascript to (basically a JQuery call to open an accordion node).
To complicate matters, I'm using history points. In a nutshell, I need to open a JQueryUI accordion based on a value in the history point data stored in the URL.
I've got the part that sets the history points working, and I can step through my code (below) as I navigate the history. The problem is, in this example, my script never renders on the page.
protected void uxScriptManager_OnNavigate(Object sender, HistoryEventArgs e)
{
if(!String.IsNullOrEmpty(e.State["activeTab"]))
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "xScript", "alert('Hello,world!');", true);
}
}
Am I doing something wrong?
Addendum
I've been tinkering, and it seems like my call to register the clientscriptblock works fine in other events...but in the Navigate event for the scriptmanager, I can't write new script out to the page. I'm thinking what I'm trying to do isn't possible...
From what I've found, it doesn't look like it's possible to write new client script out to a page - or change values in an existing script block - on the Navigate event. I'm noting this in case anyone else tries to do the same thing.
CORRECTION
I found out it is possible to do what I want!
I need to set the history point in an early part of the page lifecycle
I need to set the dynamic script in the RegisterStartupScript method for the page
(Ivan's post pointed me in the right direction, so I've flagged his post as the answer)
It looks like you need to add script by calling ScriptManager.RegisterStartupScript method to show elements on page:
ScriptManager.RegisterStartupScript(this, this.GetType(), this.ID,
""alert('Hello,world!');", true);

Javascript event like window.onload for subsequent loads

We have a function that changes the iframe height at the window.onload event so we can adjust it to the page contents. The problem is that after clicking in an asp:menu the height its restored to its default and the window.onload event doesnt fire...so we need the event that would fire in subsequent loads (tried window.unload but didnt trigger)
The resize function cant be called on the asp:menu click because the window wouldnt have finished loading so the height calculation would fail...
Any ideas??
ASP.Net AJAX exposes a client event model. To execute code after the content is refreshed, use this to bind to the pageLoaded event:
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedFunction);
Learn more about all of the ASP.Net AJAX JavaScript events here: http://msdn.microsoft.com/en-us/library/bb386417.aspx
ASPNet uses background calls via AJAX and then updates the interface.
You should use servr-side code. Inspect the AJAX resolver you're using and look for its declared events. I'm sure there's one event that triggers after updating a webpage and you can attach an eventListener to it.
I had this same problem with a similar script 2 years ago. I solved it using JavaScript in the newly loaded page's window.onload to call up to the parent document and execute the script. In the child page we had this script:
function goSetHeight(){
if (parent == window) return;
else parent.setIframeHeight('iframe_name');
}
// Edit: Forgot to add the window.onload call
window.onload = goSetHeight;
This script called up to the parent page where we had a script file included that had this function:
function setIframeHeight(iframename) {
var iframeEl = document.all[iframename];
var iframeWin = window.frames[iframename];
if(iframeEl && iframeWin) {
//var docHt = getDocHeight(iframeWin.document);
//if(docHt) iframeEl.style.height = docHt + 30 + "px";
iframeEl.style.height = iframeWin.document.body.offsetHeight + 30 + "px";
}
}
Please note that the above function was written solely with IE in mind (it was an application specification), so to be usable cross browser it would need modification. The only real problem we encountered with it were 2 particular cases:
We had a couple of instances where the pages that were loaded in the iframe were from a different domain than the page holding the iframe. This causes a JavaScript security error because the browser thinks this is an XSS attack and denies it.
We also encountered several situations where the iframe was nested 2 deep. It was a hack-job work-around that I came up with that I was extremely unhappy about, but it worked while we refactored from classic asp into .Net. I have since lost the script that was used to perform the pass through, but it wasn't complicated, it simply performed a similar parent check and kept going.
In the end we used a hiddenfield where we saved the height calculated in the window.onload
document.getElementById("iframex").value = document.getElementById("iframeheight").height;
...then in the Menu1_MenuItemClick(object sender, MenuEventArgs e) we reassigned the height to the iframe: iframex.Attributes["height"] = iframeheight.Value;
This works for us (Because in our case luckily the page height doesnt change on each option selected) but doesnt resolve the original question about getting fired the window.onload event or something similar after subsequent loads in order to recalculate the new height...
We will wait so someone can give the best answer to this problem...

ASP.Net: Page_Load() being called multiple times

I don't know alot about ASP.Net but I'm trying to make a new control for a message box. You enter some info and press a button.
However, for some bizarre reason when the button is pressed, Page_Load() gets called a second time, and all of the member variables are reset to null! I need those variables, and Page_Load() has not reason to be called a second time! Of course the callstack is useless.
Remember, in ASP.Net every time you cause a postback of any kind, including handling events like button clicks, you're working with a brand new instance of your page class that must be rebuilt from scratch. Any work you've done previously to build the page on the server is gone. That means running the entire page life cycle, including your page load code, and not just the click code.
Always two there are, no more, no less. A request and a response.
When the page posts back, the Page_Load method is called. Then, once the server actually processes the page and sends you a new one based on changes, the Page_Load is called again, actually the first time on the new page sent to you.
So if you are pulling data in the Page_Load event or setting some values, enclose it in the following block:
if(!Page.IsPostBack)
{
}
to preserve some of your state. Otherwise, the instructions that you put into the Page_Load event will execute every time.
It helps to review the ASP.Net page lifecycle :)
As Joel mentioned, instance variables will be lost once the page is sent back to the client. However, there are various methods of storing the values of your variables so you can retrieve them later. This page on State Management is a good starting point if you want to learn more.
Any tag/element which requires url reference like img, anchor, object etc must be checked for the empty reference.
e.g. href="", url="", src="" are some common errors.
This code works for me:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["something"] == null)
{
Session["something"] = "1";
}
else
{
Session["something"] = null;
//your page load code here
}
}
}
For me, the issue was a bit complicated, but I found that the
protected override void OnPreRender(EventArgs e)
handler is only called once, so it's safer to put some actions in there if it's not too late in the pipeline for you.
An extension of #user3207728's response, I found this link explains it well and has a simple solution...
http://www.aspdotnet-suresh.com/2010/04/detect-browser-refresh-to-avoid-events.html
Unfortunately checking just for if (!Page.IsPostBack) is not enough as IsPostBack will always be FALSE on a refresh.
Just a shot in the dark but maybe add this after your page_load:
if (!IsPostBack)
{
you can use sessions or viewstate to retain the values of variables..
if you want to redirect to a different page , use session[]
else if you want to stay on the same page , use viewstate[]
In my Case the Problem Was Related to Iframe, One Time I removed the Iframe Everithing Work Fine
<iframe id="pdf2"
src="#"
width="100%"
height="100%">
</iframe>

Showing/Hiding div CollapsePanel after ClientScript.RegisterClientScriptBlock

This question is an extension to Showing/Hiding div.
As stated, I found a workaround to prevent a Collapse panel from flashing upon load.
My solution was this:
In header:
function showDivs() {
divMenuContent.style.visibility = 'visible';
divMenuContent.style.display='block';
}
</script>
and panel hidden in div as:
<div id="divMenuContent" style="visibility:hidden; display:none;">
<asp:Panel ID="pnlAddNewContent" runat="server" CssClass="collapsePanel" Width="500px">
</asp:Panel>
</div>
and body load is
onLoad="javascript:showDivs();"
The thing is, it works perfectly on a blank page. But I think when I do a
ClientScript.RegisterClientScriptBlock(this.GetType(), "", sb.ToString());
it doesn't work - as in the panel doesnt collapse/show. The above code does work.
Do you believe the clientScipt conflicts with my javascript to show div? I don't receive any Javascript run errors within browser.
More info: The clientScript is called if its not a postback. I have also tried to call the javascript from the clientscript by adding the following code at the end of it:
sb.Append("\n}\nshowDivs();</script>\n");
But this time I get the following error:
divMenuConent is undefined.
ANy solutions?
If I remember right the RegisterClientScriptBlock injects its JS into the head. Could be that its injecting it above your code. Now that could mean a couple of things depending on what that injected code is doing.
Is it referencing any elements straight away (remember this is in the head so you won't be able to mess with the DOM as it would have loaded it yet, all that stuff needs to be after onload)
Is it referencing divMenuContent which you might only be declaring below it
Is it throwing an error which is stopping the rest of your inpage JS from running.
Use firebug in Firefox to double check you are not getting any funny JS errors and view the source and double check you are not trying to reference anything before you should be.
Also I'd suggest you look into using one of the JS frameworks to allow you to wire up multiple onload events so you don't run the risk of over-writing any previous attached events. This allows you to run multiple function when the page loads (or even better, aka faster, when the DOM loads). I'd suggest MooTools in which case you can do something like this
// You can add as many of these as you like and they will all run :)
window.addEvent( "domready", function() {
// Code to run goes here
});
You can use RegisterStartupScript for this kind Requirement.

Categories

Resources