Monday, December 24, 2007

Send Values From Code-behind to Client Side

Issue

Passing values to the client script is sometimes necessary. One example can be when you want to pass an error message generated by code-behind to JavaScript to be shown in an alert message box.

Solution

Embedded code blocks can be used as follows:
<script type="text/javascript>
    alert("<%=MyVariable%>");
</script>

Note: MyVariable is a variable that is accessible from out side the class.
public string MyVariable;

No comments: