Skip to content Skip to sidebar Skip to footer

Change Hidden Field Value With Jquery And Get The New Value In Server

I change value off the hidden field with jquery and now i want to get the new value in server. i use asp.net and this is my jquery code: $('#HiddenField').val('NewValue'); and thi

Solution 1:

I tried with this on my page,

<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
</head>
<body>

    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="goBtn" Text="Go" OnClick="goBtn_Click" />
        <input id="HiddenField" type="hidden" runat="server" value="" />
        <asp:TextBox runat="server" ID="testTxt"></asp:TextBox>
    </div>
    </form>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#<%=HiddenField.ClientID %>').val("Test");
        });
    </script>

</body>
</html>

and this in the code behind,

protected void goBtn_Click(object sender, EventArgs e)
{
    testTxt.Text = HiddenField.Value;
}

when you press the go button, the new value is available on the server.


Solution 2:

You need to use the ClientID of your hidden field in the JQuery selector such as:

$('#<%= HiddenField.ClientID %>').val("NewValue");

Or, alternatively, use a style for the hidden field and access it by the class, such as:

<input id="HiddenField" type="hidden" runat="server" value="" CssClass="hidden"/>
$('.hidden').val("NewValue");

Post a Comment for "Change Hidden Field Value With Jquery And Get The New Value In Server"