I have a textbox with the property type="date". I am able to retrieve the value in backcode but when I try to assign a value, the placeholder "mm/dd/yyyy" is shown.

When I view the markup, the expected value is there - "value='01/01/1991'". I think it has to do with JavaScript overriding the value.

Any suggestion on how I can work around this?

Edit: Sorry, forgot to include the markup and code

So my markup is

<asp:TextBox ID="txtBirthdate" runat="server" class="form-control input-sm" type="date" onkeypress="handleEnter(event)" />

and c# is

txtBirthdate.Text = usr.BirthDate.ToString("MM/dd/yyyy");

the resulting markup is

<input name="ctl00$ctl00$ctl00$ctl00$Body$Body$Body$Body$txtBirthdate" value="01/20/1991" id="ctl00_ctl00_ctl00_ctl00_Body_Body_Body_Body_txtBirthdate" class="form-control input-sm" type="date" onkeypress="handleEnter(event)">

So as you can see, the value was assigned.

有帮助吗?

解决方案

I believe this only happens in Chrome. It requires value assignment to be in "yyyy-MM-dd" format. Try in your C#

txtBirthdate.Text = usr.BirthDate.ToString("yyyy-MM-dd");

This should display the correct value (and it doesn't affect the format of the display).

If will display in this format in other browsers tho. You may want to do some browser detection, e.g.

if (Request.Browser.Browser == "Chrome")
{
    txtBirthdate.Text = usr.BirthDate.ToString("yyyy-MM-dd");
} else {
    txtBirthdate.Text = usr.BirthDate.ToString("MM/dd/yyyy");
}

but perhaps something more sophisticated

其他提示

For me this worked:

txtBirthdate.Text = Convert.ToDateTime(TablaHabitacion.Rows[fila].Cells[6].Text).ToString("yyyy-MM-dd");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top