0

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?

4 Answers 4

9

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',
        });
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Oh hey, that's much simpler than my one.
1

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>

Comments

0

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

Comments

0

$.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.

Comments

Your Answer

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.