0
votes

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
disabled property is not persisted/recreated in the postback. You need to re-apply (or regenerate in the HTML directly) - easiest way is to move your if (condition) to its own function and call it on both change (as it is now I assume) and also on doc.ready: $(function() { if (condition)... (but in a separate function so it's not "DRY") - freedomn-m
@freedomn-m Yes, you're right. Post this comment as answer. - Jesuraja

1 Answers

0
votes

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.