When working in the code behind:
string func = "displaySuccessMessage("+name+");";
ClientScript.RegisterStartupScript(this.GetType(), "success", func, true);
If in a separate JavaScript file:
function displaySuccessMessage(user)
{
$("#box").dialog({
title:"User Registration",
html: user.toString() + " Successfully Registered",
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
var s = "../Pages/main.aspx";
window.location = s;
}
}
});
} // end function
Everything works fine when passing the function string with no arguments.
However, adding an argument to the function string results in a browser error:
"Uncaught ReferenceError: john is not defined"
(Here, "john" represents the value of name)
I suspect that this issue arises because of how the function is registered, causing it to misunderstand between a variable and its value, resulting in an undefined type like john.
This leads to the question:
How can one successfully send arguments to a JavaScript function from code-behind?
Thank you in advance, eran.
For related information, check out this similar question:
similar question