最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing value from JQuery Datepicker to code behind - Stack Overflow

programmeradmin1浏览0评论

I am using JQuery Datepicker in my .aspx file.

I need to use the date value in my code behind file. This is my function which I want to update a hidden value on my page which I can use in my code behind.

$(function () {
    $("#datepicker").datepicker({ minDate: 0,
        onSelect: function () {
            var dueDate= document.getElementById('dueDate');
            dueDate.value = $(this).datepicker('getDate');
        }
    });
});

My hidden value which I want to update, which is on the same .aspx page:

<Input id="dueDate" type="hidden" runat="server" />

Now in my code behind, I want to use the date like so:

DateTime due= dueDate.Value;

This gives me an error:

Cannot implicitly convert type 'string' to 'System.DateTime'    

I get the same error when I use

DateTime due = Convert.ToDateTime(dueDate.Value);

What is the correct way to use the date from Datepicker in the code behind?

I am using JQuery Datepicker in my .aspx file.

I need to use the date value in my code behind file. This is my function which I want to update a hidden value on my page which I can use in my code behind.

$(function () {
    $("#datepicker").datepicker({ minDate: 0,
        onSelect: function () {
            var dueDate= document.getElementById('dueDate');
            dueDate.value = $(this).datepicker('getDate');
        }
    });
});

My hidden value which I want to update, which is on the same .aspx page:

<Input id="dueDate" type="hidden" runat="server" />

Now in my code behind, I want to use the date like so:

DateTime due= dueDate.Value;

This gives me an error:

Cannot implicitly convert type 'string' to 'System.DateTime'    

I get the same error when I use

DateTime due = Convert.ToDateTime(dueDate.Value);

What is the correct way to use the date from Datepicker in the code behind?

Share Improve this question asked Nov 9, 2015 at 6:18 DawsonDawson 4572 gold badges9 silver badges23 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

DateTime.Parse(...)

or

DateTime.ParseExact(...)

or

DateTime.Parse("01/01 2010");

or use

DateTime.TryParse

if you aren't sure it converts in which type every time, ie. not always a date, so try this 4 and check

Consider having the following code on your .aspx file, removing runat server:

<input type="hidden" id="dueDate" name="dueDate" value="" />

Now make the following change in your jquery datepicker function:

$(function () {
    $("#datepicker").datepicker({
        minDate: 0,
        dateFormat: "dd-mm-yyyy",
        onSelect: function() {
            $("#dueDate").val() = $(this).datepicker("getDate");
        }
    });
}

This way the hidden field value of dueDate gets updated whenever the value of your datepicker control is changed. Further since your hidden field now has a name and value attribute associated with it, your code behind would receive its value as a string whenever your form is posted.

Now in your code behind file, create your DateTime object as follows:

string[] dueDateSplit = Request.Form["dueDate"].Split('-');
DateTime due = new DateTime(dueDateSplit[2], dueDateSplit[1], dueDateSplit[0]);

Provide a name for the datepicker

<Input id="dueDate" name = "dueDate" type="hidden" runat="server" />

and use the below

String text = Page.Request.Form["dueDate"]
发布评论

评论列表(0)

  1. 暂无评论