将 javascript 变量传递给代码隐藏 [英] Pass javascript variable to Codebehind

查看:42
本文介绍了将 javascript 变量传递给代码隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 javascript 变量中获取一个值并将其用于可视化基本代码中,而无需在任何控件上添加值.

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

推荐答案

如果没有服务器控件的任何帮助,您将无法从代码隐藏访问 js 变量.您可以将页面重定向到自身并将该值作为 URL-Parameter(window.location.href = window.location.href + "?value=test";) 传递.但我认为这不是您想要的,因为它强制回发.所以最好的方法是使用隐藏域:

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:

在javascript函数中:

In javascript function:

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

在 aspx 上:

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

在代码后面

Protected hidValue As HtmlControls.HtmlInputHidden

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim hiddenValue = hidValue.Value
End Sub

这篇关于将 javascript 变量传递给代码隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆