Send variable from vb net to javascript

4 posts, 0 answers
  1. Fabio Cirillo
    Fabio Cirillo avatar
    181 posts
    Member since:
    Jan 2010

    Posted 29 Nov 2012 Link to this post

    Hi,
    i've a web page write with vbnet 2010, how do to send the value of a variable from vb net to javascript?
     
    thanks bye
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Nov 2012 Link to this post

    Hi,

    One suggestion is that to make your server variable as Public Property and access it in java script as follows.

    JS:
    <script type="text/javascript">
        function OnClientClick() {
            var serverVariable = '<%= ServerVariable %>';
            alert(serverVariable);
        }
    </script>

    Hope this helps.

    Regards,
    Princy.
  3. Fabio Cirillo
    Fabio Cirillo avatar
    181 posts
    Member since:
    Jan 2010

    Posted 29 Nov 2012 Link to this post

    Not work, I blank, I did so

    Public Property _image As String
    Private Sub Imgbtnupload_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles Imgbtnupload.Click
            _image = "/public/image_profile/" & RadUpload1.UploadedFiles.Item(0).FileName
    End Sub
    function Close_and_load() {
        var _image = '<%= _image %>'; 
        GetRadWindow().close(_image);
    }
  4. rdmptn
    rdmptn avatar
    72 posts
    Member since:
    Aug 2011

    Posted 03 Dec 2012 Link to this post

    The server code block is evaluated with the rendering of the page. If you are using AJAX and you change the server variable this will not affect the script if it is not included in the AJAX request. What I'd suggest you do is to register a script from the code-behind and populate some global variable with the value you need (e.g. the ScriptManager.RegisterStartupScript() method). Or, simply use a hidden field.
Back to Top