Search code examples
c#jqueryjquery-uicode-behinddatetimepicker

pass the value of datetimepicker to codebehind (c#)


i want to pass the value of a datetimepicker to my Codebehind(C#) and save it to a variable in C#.

I tried it with a hiddenfield but it don't works.

here is the js code:

var dates = $("#ArrivalStartDatepicker").datetimepicker({
                            defaultDate: "+lw",
                            dateFormat: 'dd-mm-yy',
                            changeMonth: true,
                            numberOfMonths: 2,
                            ampm: true,
                            stepMinute: 10,
                            minuteGrid: 10,
                            onSelect: function (selectedDate) {
                                    document.getElementById("ArrivalStart").value = dates.datetimepicker('getDate');
                                }
                            });

the hiddenfield:

<div>
          <input type="hidden" id="ArrivalStart" runat="server"/> 
</div>

when I choose a date or a time firebug throws the following error:

document.getElementById("ArrivalStart") is null

this tells me that the ArrivalStart-Field isn't accesible in JS.

how can i manage this?

greetz

Tobi


Solution

  • Instead of

     document.getElementById("ArrivalStart")
    

    use

      document.getElementById("<%= ArrivalStart.ClientId %>")
    

    That way you inject the ID that ASP.Net created.

    EDIT
    This javascript fragment needs to be in the aspx file (not a separate js file), as ASP.Net needs to process that <%= .. %> block and needs access to the ArrivalStart member.