将值从JQuery Datepicker传递给后面的代码

时间:2022-08-25 21:53:30

I am using JQuery Datepicker in my .aspx file.

我在.aspx文件中使用JQuery Datepicker。

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:

我想要更新的隐藏值,它位于同一.aspx页面上:

<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?

在后面的代码中使用Datepicker中的日期的正确方法是什么?

3 个解决方案

#1


1  

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

考虑在.aspx文件上使用以下代码,删除runat服务器:

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

Now make the following change in your jquery datepicker function:

现在在jquery datepicker函数中进行以下更改:

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

这样,只要更改了datepicker控件的值,就会更新dueDate的隐藏字段值。此外,由于您的隐藏字段现在具有与之关联的名称和值属性,因此只要您的表单发布,您的代码就会以字符串形式接收其值。

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

现在在您的代码隐藏文件中,按如下方式创建DateTime对象:

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

#2


3  

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

如果你不确定它每次都转换成哪种类型,即。并不总是约会,所以试试这4并检查

#3


1  

Provide a name for the datepicker

提供datepicker的名称

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

and use the below

并使用以下

String text = Page.Request.Form["dueDate"]

#1


1  

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

考虑在.aspx文件上使用以下代码,删除runat服务器:

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

Now make the following change in your jquery datepicker function:

现在在jquery datepicker函数中进行以下更改:

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

这样,只要更改了datepicker控件的值,就会更新dueDate的隐藏字段值。此外,由于您的隐藏字段现在具有与之关联的名称和值属性,因此只要您的表单发布,您的代码就会以字符串形式接收其值。

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

现在在您的代码隐藏文件中,按如下方式创建DateTime对象:

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

#2


3  

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

如果你不确定它每次都转换成哪种类型,即。并不总是约会,所以试试这4并检查

#3


1  

Provide a name for the datepicker

提供datepicker的名称

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

and use the below

并使用以下

String text = Page.Request.Form["dueDate"]