简体   繁体   中英

How do I pass a very long string to my javascript function from my code behind?

I'm trying to pass a string from my code behind to a JavaScript function in my .aspx file. This works fine if I pass a short dummy string, like "bla." But when I pass it a full exception message + stack trace the program hangs and does not enter the JavaScript function.

Here is how I'm trying to accomplish this:

ScriptManager.RegisterStartupScript(this, typeof(string), "loading log message popup", String.Format("LogMessagePopup('{0}')", logMessage), true);

On my server-side is the LogMessagePopup JavaScript function,

function LogMessagePopup() {
...

The contents of the JavaScript function don't matter.

The problem, possibly, is that the logMessage is very long. It is an entire exception and stack trace, over 4KB.

If I pass a shorter message, my code works fine.

There may also be issues with special characters, but after using logMessage.replace to escape special characters in JavaScript the problem persists.

I've been reading that you can pass longer strings via POST to the server-side code, but I don't understand how to do this, if this is even the solution I'm looking for, because I don't know how to pass it to my JavaScript LogMessagePopup function specifically. The string gets lost somewhere along the way.

take a look at this thread ScriptManager.RegisterStartupScript code not working - why? it looks like you need to use GetType instead of typeof

protected void Page_PreRender(object sender, EventArgs e)
{
   ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", 
    "alert('This pops up');", true);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM