Reputation: 2057
I am creating a user control that uses the jQuery date picker widget, as follows:
$(function () {
$("#datePickerYearMonth").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd-M-yy',
});
});
But instead of hard coding the options I would like to create properties or something like that so that I can change the properties dynamically from code behind.
Is this possible?
Upvotes: 0
Views: 1493
Reputation: 12618
You can use inline code, like this:
In code behind:
protected bool ChangeMonth
{
get{
return true;
}
}
And this in javascript:
$(function () {
$("#datePickerYearMonth").datepicker({
changeMonth: <%=ChangeMonth%>,
changeYear: true,
dateFormat: 'dd-M-yy',
});
});
Upvotes: 9
Reputation: 50114
If you create an anonymous object, e.g.
var myAO = new { changeMonth = true, changeYear = true, dateFromat = "dd-M-yy" };
you can convert it to a JSON string with the JavaScriptSerializer
var myJsonString = new JavaScriptSerializer().Serialize(myAO);
You can then use this string to build up your JavasSript, e.g. you could put it in a property in your codebehind and then have a script block in your ASPX:
<script type="text/javascript">
$(function () {
$("#datePickerYearMonth").datepicker(<%= MyJsonStringProperty %>);
});
</script>
Upvotes: 1
Reputation: 129792
$.fn.datepicker
is a function that runs at the client, after the server has done its processing. You could always write server code that generates the corresponding JavaScript.
If you want to change these properties in an AJAX call, say, you'll have to make sure that the AJAX call yields the code you need in order to re-execute the datepicker
function.
Upvotes: 0
Reputation: 22619
Render some JavaScript block that has a variable list from code behind and use the same in datepicker option
Check this Create javaScript variable in code behind of asp.net
Upvotes: 0