5

I get a data from database in aspx.cs like:

string abc = sdr["aaa"].ToString()

How can I call "string abc" in javascript part

thanks for answers

1

3 Answers 3

3

Simply render out your C# variable to the page so you can access it via JavaScript.

In .aspx.cs:

protected string abc {get;set;}

protected void Page_Load(object sender, EventArgs e)
{
  var sdr = GetData();
  abc = sdr["aaa"].ToString();
}

In .aspx:

<script>
var abc = '<%=abc%>';
alert(abc);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Try with ClientScriptManager.RegisterStartupScript. (http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx)

Comments

0

Use this code:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "<script language='javascript'>alert(" + abc + ");</script>", false)

1 Comment

If you specify last parameter as true you don't need to wrap your script with <script...

Your Answer

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