简体   繁体   English

从代码隐藏中将值设置为 input type="date"

[英]Set value to input type=“date” from codebehind

I want to assign value to my HTML tag input type="date" from C# codebehind in ASP.NET web application.我想从 ASP.NET Web 应用程序中的 C# 代码隐藏为我的 HTML 标记input type="date"赋值。 After page is loaded, the value is not visible, but in chrome dev tools i can see the value:页面加载后,该值不可见,但在 chrome 开发工具中我可以看到该值:

在此处输入图片说明

ASPX: ASPX:

<input type="date" runat="server" id="date_datumPrispetja" value="dd. MM. yyyy" />

Codebehind:代码隐藏:

string date = myDate.ToString("dd. MM. yyyy"); //value of date: "09. 04. 2017";
date_datumPrispetja.Value = date;

Is this is even possible, to assign value from codebehind to this HTML5 element?这甚至有可能将代码隐藏中的值分配给这个 HTML5 元素吗?


EDIT:编辑:

Compare on datetime and date input type.比较datetimedate输入类型。 C#: C#:

date_datumPrispetja.Value = DateTime.Now.ToString("dd. MM. yyyy");
hi.Value = DateTime.Now.ToString("dd. MM. yyyy");

ASPX: ASPX:

<input type="date"  runat="server" id="date_datumPrispetja" value="dd. MM. yyyy"/>
<br />
<input type="datetime" runat="server" id="hi" value="dd. MM. yyyy" />

RESULT:结果:

在此处输入图片说明

I think it should be work, where did you put this code in:我认为它应该可以工作,你把这段代码放在哪里:

string date = myDate.ToString("dd. MM. yyyy"); //value of date: "09. 04. 2017";
date_datumPrispetja.Value = date;

Page_Load()?页面加载()?

My test code as bellow:我的测试代码如下:

protected void Page_Load(object sender, EventArgs e)
    {
        hi.Value = DateTime.Now.ToString("yyyy-MM-dd");
    }

Then it worked well.然后它运作良好。

这对我有用。

TextBox.Text = Convert.ToDateTime('01-01-2020').Date.ToString("yyyy-MM-dd");

Mobile Safari, Firefox and Chromium accept the date value only in format Mobile Safari、Firefox 和 Chromium 仅接受格式中的日期值

<input type="date" value="2017-04-27">

and will print the date in the localized format.并将以本地化格式打印日期。 If they get a localized format as input (eg "27.04.2017") they print placeholders only.如果他们获得本地化格式作为输入(例如“27.04.2017”),他们只会打印占位符。

However Safari accepts但是 Safari 接受

<input type="date" value="27.04.2017">

On the other hand if Safari get the input format "2017-04-27", it will print this value in a non localized form.另一方面,如果 Safari 获取输入格式“2017-04-27”,它将以非本地化形式打印此值。

Inside the browser's DOM the input value is stored attribute defaultValue (true for all browsers) and the attribute value is empty (not true for Safari).在浏览器的 DOM 中,输入值存储在属性defaultValue (对于所有浏览器为 true)中,并且属性值为空(对于 Safari 不为 true)。

My workaround for this confusion is some javascript:我对这种混淆的解决方法是一些 javascript:

 $(function () { var datefields = $('.TTMMJJ') for (i = 0; i < datefields.length; i++) { datefields[i].type = "date"; var defaultVal = datefields[i].defaultValue; var val = datefields[i].value; if (defaultVal.length > 0 && val.length == 0) { var darr = defaultVal.split("."); if (darr.length == 3) { var year = darr[2]; var month = darr[1]; var day = darr[0]; if (year.length == 2) { year = "20" + year; } var s = year + '-' + month.padStart(2,0) + '-' + day.padStart(2,0); datefields[i].value = s; } } } });

Note that注意

Date.parse("27.04.2017") 

works with Chromium but not with Firefox.适用于 Chromium,但不适用于 Firefox。

这对我有用:

txtFechaOtorgamiento.Text = Format(servicio.FechaOtorgamiento, "yyyy-MM-dd");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM