4

I am using a jQueryUI ProgressBar to show users how much allowed file storage they have used. The percentage is calculated in code-behind and should be passed to Javascript.

Aspx Code

 <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
 <script type="text/javascript">
 $(function () {
 var pct = document.getElementById("filesPercentage").value;
 $("#progressbar").progressbar({
 value: pct
 });
 });
 </script>
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 ...
 <input type="hidden" runat="server" id="filesPercentage" /> 
 <div id="progressbar"></div> 
 ...
 </asp:Content>

Code Behind

protected void Page_Load(object sender, EventArgs e)
{
 filesPercentage.Value = "85";
}

It seems like it can't get the percentage number from the hidden field. Any help would be appreciated.

asked Apr 13, 2011 at 22:15
2
  • Does the hidden field contain the value if you view the source? Commented Apr 13, 2011 at 22:18
  • filesPercentage is likely not going to be id of the control once it is rendered. Commented Apr 13, 2011 at 22:21

5 Answers 5

3

You need to get the rendered id of your hidden input

var pct = document.getElementById("<%=filesPercentage.ClientID%>").value;

and from the moment that you run the input on server its better to use the asp:HiddenField and not the input

answered Apr 13, 2011 at 22:21

Comments

2

since your hidden field is a server control it could be that the ID is getting generated to something other than filesPercentage (probably something like ctl00_ctl00_filesPercentage)

  • You may need to apply the generated client ID to your javascript document.getElementById("<%=filesPercentage.ClientID%>").value;
  • Or use another way of selecting the hidden value, such as $('[hidden's parent element] input[type="hidden"]').val()

additionally, it looks like progressbar value is expecting a number, so you may need to do value: pct * 1 or value: parseInt(pct)

http://jsfiddle.net/pxfunc/QyZSs/

answered Apr 13, 2011 at 22:21

1 Comment

Good job on solving my first problem and anticipating the next problem I would have run into. This worked perfectly.
1

Try this

var pct = document.getElementById("<%=filesPercentage.ClientID %>").value;
answered Apr 13, 2011 at 22:21

Comments

0

.net will modify the id you give a control to ensure it is unique, so you are not accessing it with the correct id. If you give the hidden field a unique class name, you can access the value that way:

<input type="hidden" runat="server" id="filesPercentage" class="hiddenClass" /> 
var pct = $('.hiddenClass').val();
answered Apr 13, 2011 at 22:20

Comments

0

This is a bit cleaner IMHO :-)

$("#progressbar").progressbar({
 value: $("#<%=filesPercentage.ClientID%>").val()
});
answered Apr 13, 2011 at 22:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.