在某些浏览器中不起作用

在某些浏览器中不起作用

本文介绍了输入类型=“日期"在某些浏览器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET 网页表单页面,我在代码隐藏中设置输入值 (type="date"),它在 IE 11 和 Firefox 51 中正确显示,但在 Chrome 56 中和 Opera 43,它只显示 mm/dd/yyyy 占位符.下面是我的代码.

ASPX...

<head runat="服务器"><title>测试页</title><link rel="stylesheet" type="text/css" href="../Content/bootstrap.css"/><link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"/><link rel="stylesheet" type="text/css" href="../Content/formValidation.min.css"/><script type="text/javascript" src="../Scripts/jquery-2.2.3.min.js"></script><script type="text/javascript" src="../Scripts/jquery-ui-1.11.4.min.js"></script><script type="text/javascript" src="../Scripts/bootstrap.min.js"></script><script type="text/javascript" src="//momentjs.com/downloads/moment.min.js"></script><script type="text/javascript" src="../Scripts/formValidation.popular.js"></script><script type="text/javascript" src="../Scripts/framework/bootstrap.js"></script><script type="text/javascript" src="../Scripts/bootbox.min.js"></script><身体><form id="form1" runat="server"><div><label for="txtChildDOB1" class="control-label col-xs-1">DOB *</label><div class="col-xs-1"><input type="date" class="form-control" style="position: absolute; z-index: 999" id="txtChildDOB1" name="txtChildDOB1" value="" runat="server"/>

</表单>

还有代码隐藏...

 protected void Page_Load(object sender, EventArgs e){this.txtChildDOB1.Value = new DateTime(2002, 3, 19).ToShortDateString();}
解决方案

在回答您的问题之前.asp.net中有两种输入日期,server sideclient side.
在客户端输入日期如下:

在服务器端输入日期是这样的:

<asp:TextBox TextMode="Date" ID="datePicker" class="form-control" runat="server" ></asp:TextBox>

您可以将它们都用于您的目的,但在此之前,您需要准确地设置您的时间,输入日期.
输入日期仅接受这些格式类型 yyyy-MM-dddd-MM-yyyy.
您必须选择 - 作为日期分隔符,当然年份是 4 位数字,月份是两位数字,天是两位数字.
为了清除我的答案,我在这里写了我的代码:

string makeDate = DateTime.Now.ToString("yyyy") +"-" + DateTime.Now.ToString("MM") +"-" + DateTime.Now.ToString("dd");

或者在自定义日期中,您可以使用此方法:

public static string makeDate8(DateTime dt){字符串日期 = dt.Year.ToString().PadLeft(4,'0') + "-" + dt.Month.ToString().PadLeft(2, '0') + "-" + dt.Day.ToString().PadLeft(2, '0');归期;}

现在您可以像这样设置输入值:
在客户端:

datePicker.Value = 今天;

在服务器端:
datePicker.Text=今天;

I've got an ASP.NET web forms page where I'm setting the value of an input (type="date") in code-behind and it displays correctly in IE 11 and Firefox 51, but in Chrome 56 and Opera 43, it merely displays the mm/dd/yyyy placeholder. Below is my code.

The ASPX...

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test Page</title>
    <link rel="stylesheet" type="text/css" href="../Content/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
    <link rel="stylesheet" type="text/css" href="../Content/formValidation.min.css" />

    <script type="text/javascript" src="../Scripts/jquery-2.2.3.min.js"></script>
    <script type="text/javascript" src="../Scripts/jquery-ui-1.11.4.min.js"></script>
    <script type="text/javascript" src="../Scripts/bootstrap.min.js"></script>
    <script type="text/javascript" src="//momentjs.com/downloads/moment.min.js"></script>
    <script type="text/javascript" src="../Scripts/formValidation.popular.js"></script>
    <script type="text/javascript" src="../Scripts/framework/bootstrap.js"></script>
    <script type="text/javascript" src="../Scripts/bootbox.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <label for="txtChildDOB1" class="control-label col-xs-1">DOB *</label>
        <div class="col-xs-1">
            <input type="date" class="form-control" style="position: absolute; z-index: 999" id="txtChildDOB1" name="txtChildDOB1" value="" runat="server" />
        </div>
    </div>
    </form>
</body>

And the code-behind...

        protected void Page_Load(object sender, EventArgs e)
    {
        this.txtChildDOB1.Value = new DateTime(2002, 3, 19).ToShortDateString();
    }
解决方案

Before answering your question. There are two types input date in asp.net, server side and client side.
On the client side the input date like this:

<input type="date" id="datePicker" class="form-control" runat="server" />

On the server side the input date is like this:

<asp:TextBox TextMode="Date" ID="datePicker" class="form-control" runat="server" ></asp:TextBox>

You can use both of them for your purpose but before that, you need set your time exactly input date want it.
input date only accepts these format types yyyy-MM-dd or dd-MM-yyyy.
you must choose - as date separators and certainly 4 digits on the year, two digits on month and two digits on days.
to clear my answer I wrote my code here:

string makeDate = DateTime.Now.ToString("yyyy") +
            "-" + DateTime.Now.ToString("MM") +
            "-" + DateTime.Now.ToString("dd");

Or in custom dates, you can use this method:

public static string makeDate8(DateTime dt)
{
    string date = dt.Year.ToString().PadLeft(4,'0') + "-" + dt.Month.ToString().PadLeft(2, '0') + "-" + dt.Day.ToString().PadLeft(2, '0');
    return date;
}

Now you can set input value like this:
On client side :

datePicker.Value = today;

On server side:
datePicker.Text= today;

这篇关于输入类型=“日期"在某些浏览器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 16:00