Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hello Friends,
Can anyone tell me, how I can get the value of hidden field using javascript. I'm using master pages.
 
Here is aspx code
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" CssClass="delbutton" CommandArgument='<%#Eval("noticeid") %>'>Delete
<asp:HiddenField ID="hfID" Value='<%# Eval("noticeid") %>' runat="server" />
 
Here what I'm trying, I'm binding value to hidden field. Using this value I'm going to delete record using Json.
 
I tried following JavaScript
var id = document.form1.item("hfID").value;
alert(id);
 
It gives error saying: document.form1 is undefined
Is this error due to use of master page, in master page the form has id='form1'
 
Any idea, how to get value??
 
Thanks in advance
Posted 1-Jul-11 19:27pm
Comments
dhage.prashant01 at 7-Jul-11 7:50am
   
I made use of Jquery
var id = $('#hfID').val();
 
But it gives error saying: undefined
Any idea, what is going wrong?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Got solution
 
check the id of hiddenfield by viewing page source.
 
document.getElementById("ctl00_cphMain_gvJobs_ctl05_hfID").value;
 
This is what actual id I have given to hidden filed:hfID
 
This is added by asp.net:ctl00_cphMain_gvJobs_ctl05_
 
So if you try to use
document.getElementById("hfID").value;
 
it gives u error saying: document.getElementById("hfID").value is null
 
So, final solution is
document.getElementById("ctl00_cphMain_gvJobs_ctl05_hfID").value;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

While the answer you've been given is 'correct', a better way is to use the ClientID for your hidden control in code to emit the value, if you find the value by looking at your source, that value can change. Using ClientID will never break.
  Permalink  
Comments
dhage.prashant01 at 2-Jul-11 1:59am
   
how to address it?
document.getElementById('hfID').value;
 
it gives me error saying: document.getElementById('hfID').value is null
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Like CG said use the ClientID.
var id = document.getElementById('<%= hfID.ClientID %>').value;
alert(id);
 
Further Reading
Control.ClientID Property[^]
How-to use ClientIDs in JavaScript without the ugliness[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Not sure my solution at this time is relevant, but just to let you knowSmile | :) .
 
if you are using ASP.NET v4.0 then it is quite simple. We can retain the id by applying the ClientIdMode = "Static" attribute to the HiddenField control. Of course, this attribute can be applied to any control be it child or parent.Applying to Page or Master directive will affect all the child controls whose ID value remains unchanged.
 
Then in your javascript,
 
document.getElementById("hfID").value;
 
or if you are using jquery,
 
$("#hfID").val();
 
Hope this helps.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 540
1 CPallini 290
2 OriginalGriff 265
3 ChauhanAjay 124
4 George Jonsson 120
0 OriginalGriff 6,474
1 Sergey Alexandrovich Kryukov 6,190
2 CPallini 5,180
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100