Textbox disabled automatically after postback

In a ASP.NET form there are two textbox controls. One control set today's date and another control initially disabled. Based on date enable the control (disabled property). If date is current date, disable the textbox, else enable the control. It worked well.

Now the date is not today and text box is enabled. But when click the button, the form is posted back and the text box is disabled again. I didn't set enable/disable setting in any other places in javascript and code behind. I am not sure why this is happening.

if (condition)
{
    $('#txt').prop('disabled', true); }
else
{
    $('#txt').prop('disabled', false);
}

If anyone faced this kind of issue and any solution available, please help me to resolve this.

1 answer

  • answered 2021-01-18 17:21 Albert D. Kallal

    Well, if there is a post-back, then why not set the enable with code behind?

    eg:

    DateTime dtToday = DateTime.Today;
    
    if (TextBox1.Text == dtToday.ToShortDateString())
        TextBox2.Enabled = false;
    else
        TextBox2.Enabled = true;
    

    So, above in the on-load of the page should work rather well.

    Now I like client side code (js), but if a post back is going to occur here, then might as well run code behind to manage this.