Skip to content Skip to sidebar Skip to footer

Pass Javascript Variable To Codebehind

Is it posible to get a value from a javascript variable and use it into visual basic code without incrusting value on any control.

Solution 1:

You cannot access a js-variable from codebehind without any help of a server-control. You could redirect the page to itself and pass that value as URL-Parameter(window.location.href = window.location.href + "?value=test";). But i assume that this is not what you want because it forces a postback. So the best way is to use a hiddenfield:

In javascript function:

<scripttype="text/javascript">functionFoo(){
        var hidden=document.getElementById('hidValue');
        hidden.value="test";
    }
</script>

On aspx:

<Input id="hidValue"type="hidden" runat="server" />

In code behind

Protected hidValue As HtmlControls.HtmlInputHidden

ProtectedSub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
    Dim hiddenValue = hidValue.Value
EndSub

Post a Comment for "Pass Javascript Variable To Codebehind"