1

Using JQuery And ASP.Net C#3.0.

I have the following Script:

<script>
  $(document).ready(function () {
      $("[id$=txtHiddenDate]").datepicker({
          showOn: "button",
          buttonImage: "../../images/calendar-icon.gif",
          buttonImageOnly: true
      });
  });
</script>

There are 3 separate fields for day month and year.(txtDay, txtMonth, txtYear) How do return the date to these 3 separate fields?

Many thanks!

EDIT: also I want to keep hidden field aswell

1
  • why would you want to have a hidden date picker? Just use a hidden text field with the date or show a date picker. Commented Aug 28, 2013 at 9:06

2 Answers 2

1

HTML

Setups up the other inputs to hold the pieces of the date. You may want to make these hidden.

<input id="picker"/><br>
Day:
<input id="day"/><br>
Month:
<input id="month"/><br>
Year:
<input id="year"/>

Javascript

Specifies an onSelect function that parses the different pieces of the date and assigns them to respective inputs.

 $(document).ready(function () {
      $("#picker").datepicker({
          showOn: "button",
          buttonImage: "../../images/calendar-icon.gif",
          buttonImageOnly: true,
          onSelect: function(strDate){
             var tokens = strDate.split("/");
              $("#day").val(tokens[1]);
              $("#month").val(tokens[0]);
              $("#year").val(tokens[2]);

          }
      });
  });

JS Fiddle: http://jsfiddle.net/9Ww8g/

Sign up to request clarification or add additional context in comments.

1 Comment

perfect answer, I was about to start using the code behind C# (yup I try not to touch JQuery as It is not my area) nice a and simple!
0

You should apply the datepicker on a normal textbox. You can't have it on a hidden field or three different fields.

Because it's a textbox, on the server-side (in your code-behind) you'll only have a string. Use [DateTime.Parse][1], [DateTime.TryParse][2], [DateTime.ParseExact][3] or to get the selected date.

If you want to work with the hiddenField and have it in a certain format (so you can parse knowing the format), you can use altField and altFormat:

$("#myTextBox").datepicker({ 
    altField: "#myHiddenField",
    altFormat: "yy-mm-dd" 
});

If it's an ASP.NET hiddenfield, you might want to use the following selector:

altField: "input[id$=myHiddenField]"

because in your javascript, you can't know the actual id ASP.NET will generate (unless you construct your javascript in code-behind).

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.