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

javascript - how to show value of textbox type=date asp.net - Stack Overflow

programmeradmin4浏览0评论

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

Share Improve this question edited May 8, 2014 at 14:33 Comfortably Numb 39.8k17 gold badges80 silver badges145 bronze badges asked May 8, 2014 at 7:41 LiaooLiaoo 4256 silver badges21 bronze badges 2
  • show some .aspx and .cs what u tried – Dgan Commented May 8, 2014 at 7:42
  • please share some html code or JSFiddle will be better – Bhushan Kawadkar Commented May 8, 2014 at 7:53
Add a ment  | 

2 Answers 2

Reset to default 4

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");
发布评论

评论列表(0)

  1. 暂无评论