2

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

3 Answers 3

2

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

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

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>
2
  • this works great $('#<% = HiddenField1.ClientID %>').val('xxxxxxxxxxxxxxxxxxxx'); thanks guys
    – Marky68
    May 22, 2012 at 16:36
  • 1
    @Marky68 no worries, consider upvoting the answers which have helped you and accepting the answer which best/first answers your question. May 22, 2012 at 16:51
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.