简体   繁体   中英

Pass Javascript Variable To C# Code Behind

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" />

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" />

Use the Control ID for HTML markup that is generated by ASP.NET.

document.getElementById('<%= HiddenField.ClientID%>').value = "active";

When a Web server control is rendered as an HTML element, the id attribute of the HTML element is set to the value of the ClientID property. The ClientID value is often used to access the HTML element in client script by using the document.getElementById method.

Then send the Hidden value through the javascript function as a variable while calling the controller

Surely works,

Cheers Phani*

你可以看看mshtml据我所知你用你的javascript代码调用这个C#函数;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM