Loading Multiple JS variables up using RegisterStartupScript
I need to get multiple variables up into my JS code from my C# backend (ASP 2.0). Currently I use the following code
string js;
if (swapSize == null && VMWare == null)
js = "var stackedUnknownExemptCheck = " + stacked.ToString().ToLower() + ";";
else if (VMWare == null)
js = "v开发者_JAVA技巧ar stackedUnknownExemptCheck = " + stacked.ToString().ToLower() + "; var swapSize = " + swapSize.ToString() + ";";
else if (swapSize == null)
js = "var stackedUnknownExemptCheck = " + stacked.ToString().ToLower() + "; var VMWare = " + VMWare.ToString().ToLower() + ";";
else
js = "var stackedUnknownExemptCheck = " + stacked.ToString().ToLower() + "; var VMWare = " + VMWare.ToString().ToLower() + "; var swapSize = " + swapSize.ToString() + ";";
Page.ClientScript.RegisterStartupScript(this.GetType(), "", js, true);
However, the output always ends up just being the first variable up to the semicolon (see below). Is there anyway to make sure all the variables get uploaded? Thanks.
<script type="text/javascript">
//<![CDATA[
var clusterMS = false;//]]>
</script>
Seeing as I don't see "clusterMS" listed in any of that javascript, I'm assuming you're never naming your javascript blocks. The part where you're just doing "" is the name of the script, if you have other RegisterStartupScript blocks you're overwriting them each time unless they have a unique name.
Change
Page.ClientScript.RegisterStartupScript(this.GetType(), "", js, true);
to
Page.ClientScript.RegisterStartupScript(this.GetType(), "VMSwapScript", js, true);
(And give every script you're registering a unique name)
精彩评论