简体   繁体   中英

How pass a variable from code behind to javascript

I am trying to pass a value from code behind to JavaScript. I have tried all ways but when I try to display, it gives undefined.

This is my code behind

try
{
   int total = manager.GetAllBooks().Tables[0].Rows.Count;
   lblmsg1.Text = total.ToString();

   filesPercentage.Value = total.ToString();

}
catch (Exception ex)
{
   throw ex;
}

And this is the JavaScript:

var pct = document.getElementById("<%=filesPercentage.ClientID%>").value;
alert(pct);

I will appreciate any help. Am trying to render a chart using chart.js and trying to pass dynamic values instead of static.

try this define a property

protected string FilesPercentage
{
set {
ViewState["x"]=value;
}
get
{
if(ViewState["x"] != null)
return (string)ViewState["x"];
}
}

in your java script:

alert('<%=FilesPercentage%>')

in your Code Behinde:

    try
{
   int total = manager.GetAllBooks().Tables[0].Rows.Count;
   lblmsg1.Text = total.ToString();

   FilesPercentage = total.ToString();

}
catch (Exception ex)
{
   throw ex;
}

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