Reputation: 491
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.
Upvotes: 4
Views: 26506
Reputation: 27415
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
)
document.getElementById("<%=filesPercentage.ClientID%>").value;
$('[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/
Upvotes: 2
Reputation: 36256
This is a bit cleaner IMHO :-)
$("#progressbar").progressbar({
value: $("#<%=filesPercentage.ClientID%>").val()
});
Upvotes: 0
Reputation: 1070
Try this
var pct = document.getElementById("<%=filesPercentage.ClientID %>").value;
Upvotes: 1
Reputation: 66649
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
Upvotes: 3
Reputation: 4886
.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();
Upvotes: 0