将Javascript变量传递给后面的C#代码 [英] Pass Javascript Variable To C# Code Behind

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

问题描述

如何从javascript到背后的C#代码获取价值?我对下面的代码有个想法.在JavaScript代码中,我想用字符串值分配"HiddenField"控件的值,然后在后面的代码中从"HiddenField"中获取此值.但是用这个代码我做不到.你能告诉我怎么做吗?

How to get value from javascript to C# code behind ? I have an idea with the code below. In javascript code I want to assign the value of "HiddenField" Control with string value and then I want to take this value from "HiddenField" in code behind. But with this code I cannot do it. Can you tell me how to make it ?

<script>
    $(function () {
        document.getElementById('HiddenField').value = "active";
        console.log(<%= this.HiddenField.Value %>)
    });
</script>
<asp:HiddenField ID="HiddenField" runat="server" Value="5" Visible="true" />

推荐答案

您需要使用控件的 ClientID 属性来获取DOM中的实际元素ID.

you need to use ClientID property of control to get actual element ID in DOM.

<script>
        $(function () {
                document.getElementById('<%= HiddenField.ClientID%>').value = "active";
                console.log(document.getElementById('<%= HiddenField.ClientID%>').value)
        });
</script>

<asp:HiddenField ID="HiddenField" runat="server" Value="5" Visible="true" />

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

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