admin管理员组

文章数量:1400182

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

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" />
Share Improve this question asked Feb 20, 2013 at 10:25 TheChamppTheChampp 1,4375 gold badges25 silver badges41 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

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*

you may take a look at mshtml As far as I know you call with this C# functions from your javascript code ;-)

本文标签: Pass Javascript Variable To C Code BehindStack Overflow