Asp:Textbox type="date" is not completed

Asked

Viewed 291 times

2

I am creating a form in Asp where a asp:Textbox should load the expiry date of a product, which should be filled in page_load() with the database data, and be free to change if necessary

The problem is, with the type="date" I can’t fill the field in the page’s pageload:

the date the bank returns: {28/02/2020 00:00:00}

Textbox:

<asp:TextBox type="date" id="txtValidade" class="TextBoxSaldo" Width="50%" runat="server" />

Call to fill in the field:

txtValidade.Text = lstValidade[0].DT_VALIDA.ToString("dd/MM/yyyy");

What I’ve already tried:

Change the date format: In that other question, the accepted answer says the problem is in date format, so I tried to change the format to

lstValidade[0].DT_VALIDA.ToString("yyyy/MM/dd")

and did not roll.

Define the Cultureinfo:

in Codebihide I tried to define the formats for "en"

    Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR");

if I change the input type to text, it works, but I lose the date format function

3 answers

1


The format of the attribute value= to the type=date should be this:

<input type="date" value="2020-02-05">

And not this:

<input type="date" value="02/05/2020">

See that the second does not work, appears dd/mm/aaaa, if the browser is in "Portuguese of Brazil", then the problem is the value that you went through, I think you fit in so:

txtValidade.Text = lstValidade[0].DT_VALIDA.ToString("yyyy-MM-dd");

I couldn’t test to confirm because I don’t have Asp.net here, if it fails let me know

  • It worked, only the formatting on ToString("yyyy/MM/dd") which, as you said, is with - and not /

  • 1

    @Gabrieloliveira was my typo, the intention was - same, as I did in the first HTML, edited, now should be OK

-2

I did something like this and it worked, only I was picking it up from a Datatable object:

Datetime dtCadastro = Convert.Todatetime(dt.Rows[0]["Dt_cadastre"].Tostring()); txtdtcadastro. Text = dtCadastro.Tostring(@"yyyy-MM-dd"); //

-2

But as commented above, this has to do with the applied mask that in the case was yyyy-MM-dd.

I also made something similar for a textbox field of type Textmode="Number" and it worked well.

double precomCusto = Convert.Todouble(dt.Rows[0]["Preco_custo"].Tostring()); txtprecocusto. Text = precomCusto.Tostring("F2",Cultureinfo.Invariantculture);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.