Set Date in TextBox when TextMode is Date using C# and VB.Net in ASP.Net

Last Reply 10 days ago By itsme

Posted 11 days ago

I am trying to set date in textbox control (Type Date) from database after edit button click.

It is fetching date from database but it is not setting date in textbox control.

<tr>
    <td>
        <b>Last Date of Apply:</b>
    </td>
    <td>
        <asp:TextBox ID="Apply" runat="server" Width="250px" type="Date" Height="32px"></asp:TextBox>
    </td>
</tr>

 

protected void btnEdit_Click(object sender, EventArgs e)
{
    try
    {
        Button btn = sender as Button;
        GridViewRow grow = btn.NamingContainer as GridViewRow;
        Id.Value = (grow.FindControl("Id") as Label).Text;
        salary.Text = (grow.FindControl("lblsal") as Label).Text;
        email.Text = (grow.FindControl("lblemail") as Label).Text;
        title.Text = (grow.FindControl("lbltitle") as Label).Text;
        cname.Text = (grow.FindControl("lblname") as Label).Text;
        details.Text = (grow.FindControl("lbldetails") as Label).Text;
        loc.Text = (grow.FindControl("lblLocation") as Label).Text;
        Apply.Text = (grow.FindControl("lblapply") as Label).Text;
        string skill = (grow.FindControl("lblskills") as Label).Text;
        Skill.ClearSelection();
        Skill.Items.FindByText(skill).Selected = true;
        string quali = (grow.FindControl("lbldegree") as Label).Text;
        Degree.ClearSelection();
        Degree.Items.FindByText(quali).Selected = true;
        string exper = (grow.FindControl("lblexp") as Label).Text;
        exp.ClearSelection();
        exp.Items.FindByText(exper).Selected = true;
        string City = (grow.FindControl("lblcity") as Label).Text;
        city.ClearSelection();
        city.Items.FindByText(City).Selected = true;
        string age = (grow.FindControl("lblage") as Label).Text;
        Age.ClearSelection();
        Age.Items.FindByText(age).Selected = true;
        btnUpdate.Visible = true;
    }
    catch (Exception ex)
    {
        //Response.Write(ex);
    }
}

 

Posted 11 days ago

Hi itsme,

TextBox with TextMode Date only supports the yyyy-MM-dd format.

So you need to set the date format before assigning it to TextBox.

The browsers that do not support TextMode Date will show as 2019-08-13, but on browsers that support it will display accordingly.

Check this example. Now please take its reference and correct your code.

HTML

<asp:TextBox runat="server" ID="Apply" TextMode="Date" />

Code

C#

Apply.Text = DateTime.Now.AddDays(5).ToString("yyyy-MM-dd");

VB.Net

 Apply.Text = DateTime.Now.AddDays(5).ToString("yyyy-MM-dd")

Screenshot


Posted 10 days ago

I found the answer

here is the working code.

<tr>
    <td><b>Last Date of Apply:</b></td>
    <td><asp:TextBox ID="Apply" runat="server" Width="250px" type="Date"  Height="32px"></asp:TextBox></td>
</tr>

 

string conv = (grow.FindControl("lblapply") as Label).Text;
conv = DateTime.Parse(conv).ToString("yyyy-MM-dd");
Apply.Text = conv.ToString();