1
<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.

1
  • remove incorrect spaces in "<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); Commented Feb 17, 2013 at 8:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Steve B : I understand that @user1820649 wants to try protected string MyProperty; I may not have fully understood the purpose of the application.
I get CS0103: The name 'MyProperty' does not exist in the current context

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.