<script type="text/javascript">
function abc()
{
var id = document.getElementById('123');
var caption= <%=MyProperty %>;
}
</script>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterStartupScript(Guid.NewGuid().ToString(),"<script language = 'javascript'>abc();</script>");
}
protected int MyProperty
{
get
{
return 123;
}
}
i need to pass string value to javascript, with int this codes works fine.
protected string MyProperty
{
get
{
return "123";
}
}
when i tried to pass string this code does't works.
Igor Jerosimić
13.7k7 gold badges47 silver badges55 bronze badges
1 Answer 1
if you need to pass a String you have to specify the delimiters " or '
var caption= "<%=MyProperty %>";
or
var caption= '<%=MyProperty %>';
without them the javascript interprets the string value of MyProperty as the name of a Javascript variable.
answered Feb 17, 2013 at 8:05
2 Comments
Franco Rondini
Steve B : I understand that @user1820649 wants to try protected string MyProperty; I may not have fully understood the purpose of the application.
Fandango68
I get CS0103: The name 'MyProperty' does not exist in the current context
lang-js
"<script language = 'javascript'>abc();</script>"
. This should be"<script language='javascript' type="text/javascript">abc();</script>"
. You can also use this overload to automatically generate the script block:Page.ClientScript.RegisterStartupScriptBlock(typeofyourpage, "somekey", "abc();", true);