Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set value to input type="date" from codebehind

Tags:

html

c#

asp.net

I want to assign value to my HTML tag input type="date" from C# codebehind in ASP.NET web application. After page is loaded, the value is not visible, but in chrome dev tools i can see the value:

enter image description here

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?


EDIT:

Compare on datetime and date input type. C#:

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

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:

enter image description here

like image 583
tadej Avatar asked Jun 18 '26 05:06

tadej


2 Answers

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.

like image 70
Justin Shi Avatar answered Jun 20 '26 19:06

Justin Shi


This Worked for me.

TextBox.Text = Convert.ToDateTime('01-01-2020').Date.ToString("yyyy-MM-dd");
like image 42
Saurabh Singh Avatar answered Jun 20 '26 20:06

Saurabh Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!