Marky68
Marky68

Reputation: 95

asp.net hiddenfield.value is not updated by javascript

I want to assign a value to an asp.net hiddenfield via javascript prior to post back.

But in the code behind the hidden fieldvalue is null. The code I am using is:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True" Visible="True">
        <Triggers>
            <asp:AsyncPostBackTrigger   ControlID="save1"    EventName="Click">         
            </asp:AsyncPostBackTrigger>
        </Triggers>
        <ContentTemplate>
            <asp:HiddenField ID="HiddenField1" runat="server" />
       </div>

        </ContentTemplate>
   </asp:UpdatePanel>

 <asp:Button ID="save1" runat="server" Text="Send"  OnClientClick="return SaveFase();"  />

       function SaveFase() {     
           var UP = jQuery.get('<% = HiddenField1.ClientID %>');
           UP.Value= "xxxxxxxxxxxxxxxxxxxx"
          return true             
       }

Protected Sub PassBackImage(sender As Object, e As EventArgs) Handles save1.Click

      dim Value = HiddenField1.Value      
End Sub

Upvotes: 2

Views: 855

Answers (3)

Pencho Ilchev
Pencho Ilchev

Reputation: 3241

You probably meant to write:

$('#<% = HiddenField1.ClientID %>').val('xxxxxxxxxxxxxxxxxxxx');

Also, put your javascript function in a sctipt block:

<script type="text/javascript">
       function SaveFase() {     
           return $('#<% = HiddenField1.ClientID %>').val('xxxxxxxxxxxxxxxxxxxx');
       }
<script>

Upvotes: 1

James Hill
James Hill

Reputation: 61793

Use the ID selector and the .val() method:

var UP = jQuery('#<% = HiddenField1.ClientID %>');
UP.val("xxxxxxxxxxxxxxxxxxxx");

Upvotes: 2

Ethan Shafer
Ethan Shafer

Reputation: 68

Instead of using jQuery.get (which is a call to an HTTP GET method http://api.jquery.com/jQuery.get/), use

jQuery.find('#' + <% = HiddenField1.ClientID %>)

Then from there it should work. If it doesn't, use an error console to see what errors are being raised when the call is made (most browsers have one built in.

Upvotes: 0

Related Questions