How To Get A Server-side Variable Into JavaScript
I want to pass a value to this JavaScript from the database (using datareader in asp.net(C#)). The script is from http://www.dynamicdrive.com/dynamicindex14/leftrightslide.htm I ne
Solution 1:
See following:
http://www.velocityreviews.com/forums/t76971-populating-javascript-array-from-vb-net.html
Create string from serverside using datareader and register it.
OR
use RegisterArrayDeclaration(arrayName, arrayValue) from server side. See following for this:
http://msdn.microsoft.com/en-us/library/aa479302.aspx
Solution 2:
You can use a webservice
Solution 3:
you have to use the script manager http://msdn.microsoft.com/en-us/library/system.web.ui.page.clientscript.aspx for doing this. check the link it will help.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script type=text/javascript> alert('Hello World!') </");
cstext1.Append("script>");
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>RegisterStartupScript</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Post a Comment for "How To Get A Server-side Variable Into JavaScript"