siddhuoops
asked on
make textboxes readonly
Hello experts,
Right now I am using the code below to make my textbox readonly once the date passes the 20th.
if (Datetime.Now.Day > 20)
{
Textbox1.ReadOnly = true;
}
What do I have to do if I have lets say 12 textboxes in my webform and I have to make them readonly in a monthly basis? For example, if it 21st of Feb, then I will make textbox2 readonly and if it is March 21st, then textbox3 readonly and so on.
Right now I am using the code below to make my textbox readonly once the date passes the 20th.
if (Datetime.Now.Day > 20)
{
Textbox1.ReadOnly = true;
}
What do I have to do if I have lets say 12 textboxes in my webform and I have to make them readonly in a monthly basis? For example, if it 21st of Feb, then I will make textbox2 readonly and if it is March 21st, then textbox3 readonly and so on.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if (Datetime.Now.Day > 20)
{
if ( 1 == DateTime.Now.Month )
TextBox1.ReadOnly = true;
else if ( 2 == DateTime.Now.Month )
TextBox2.ReadOnly = true;
if ( 3 == DateTime.Now.Month )
TextBox3.ReadOnly = true;
}
{
if ( 1 == DateTime.Now.Month )
TextBox1.ReadOnly = true;
else if ( 2 == DateTime.Now.Month )
TextBox2.ReadOnly = true;
if ( 3 == DateTime.Now.Month )
TextBox3.ReadOnly = true;
}
Surround Gelbert's code with your previous code so that you have 2 tests to pass:
if(DateTime.Now.Day > 20)
{
if (Date.Time.Now.Month == 1)
TextBox1.ReadOnly = true;
else if(Date.Time.Now.Month == 2)
TextBox2.ReadOnly = true;
else
etc...
}
I would make the code inside the brackets a switch statement instead of another set of if statements, because I think it's easier to read, and better for future code maintainability.
if(DateTime.Now.Day > 20)
{
if (Date.Time.Now.Month == 1)
TextBox1.ReadOnly = true;
else if(Date.Time.Now.Month == 2)
TextBox2.ReadOnly = true;
else
etc...
}
I would make the code inside the brackets a switch statement instead of another set of if statements, because I think it's easier to read, and better for future code maintainability.
ASKER
once it reads the first line if(DateTime.Now.Day > 20) then its never going to the second line. Unless the date is less than 20th, then it will go to the second line. Do you see what I am saying here?
The default value for theTextBox ReadOnly property is false. On days 1 - 20 you can enter data in all the textboxes, and on the 21st the editing property will be turned off for the current month's textbox. Isn't that what you are trying to do?
ASKER
Here, I have the default vale to true because the data gets displayed in the textboxes which is coming from a table in Sql server. They can edit the data only using Edit button where I am allowing them to make changes to the particular textbox before 20th of that month...for instance textbox1 would be in editable mode only until 20th...after that its readonly
I hope this might answer your question.
I hope this might answer your question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is what I have:
protected void btnEdit_Click(object sender, EventArgs e)
{
if (DateTime.Now.Month == 1 && DateTime.Now.Day > 20)
{
TextBox1.ReadOnly = true;
}
else if (DateTime.Now.Month == 2 && DateTime.Now.day > 20)
{
TextBox2.Text = true;
}
else if------------------
------------and so on
protected void btnEdit_Click(object sender, EventArgs e)
{
if (DateTime.Now.Month == 1 && DateTime.Now.Day > 20)
{
TextBox1.ReadOnly = true;
}
else if (DateTime.Now.Month == 2 && DateTime.Now.day > 20)
{
TextBox2.Text = true;
}
else if------------------
------------and so on
If the read-only state of the text boxes at the beginning of btnEdit_Click is false, you should be set. If the read-only state of the text boxes is already true, then you need the reverse test --
if(dateTime.Now.Month == 1 && DateTime.Now.Day <= 20)
TextBox1.ReadOnly = false;
if(dateTime.Now.Month == 1 && DateTime.Now.Day <= 20)
TextBox1.ReadOnly = false;
ASKER
But the problem is lets say I want to edit a record in April 2nd. If I have
if (DateTime.Now.Month == 1 && DateTime.Now.Day > 20)
{
TextBox1.ReadOnly = true;
}
else if (DateTime.Now.Month == 2 && DateTime.Now.day > 20)
{
TextBox2.Text = true;
}
else if (DateTime.Now.Month == 3 && DateTime.Now.day > 20)
{
TextBox3.Text = true;
}
else if (DateTime.Now.Month == 4 && DateTime.Now.day > 20)
{
TextBox4.Text = true;
}
None of the statements are true. The first condition satisfies where DateTime.Now.Month == 4 but the second won't as dateTime.Now.Day is not greater than 20. It will go through each if block but nothing happens at this time. No matter if we set the readonly to true by default or false by default.
if (DateTime.Now.Month == 1 && DateTime.Now.Day > 20)
{
TextBox1.ReadOnly = true;
}
else if (DateTime.Now.Month == 2 && DateTime.Now.day > 20)
{
TextBox2.Text = true;
}
else if (DateTime.Now.Month == 3 && DateTime.Now.day > 20)
{
TextBox3.Text = true;
}
else if (DateTime.Now.Month == 4 && DateTime.Now.day > 20)
{
TextBox4.Text = true;
}
None of the statements are true. The first condition satisfies where DateTime.Now.Month == 4 but the second won't as dateTime.Now.Day is not greater than 20. It will go through each if block but nothing happens at this time. No matter if we set the readonly to true by default or false by default.
to make sure that you cover all possible cases
if(dateTime.Now.Month == 1 && DateTime.Now.Day > 20)
TextBox1.ReadOnly = false;
else if(dateTime.Now.Month == 2 && DateTime.Now.Day > 20)
TextBox1.ReadOnly = false;
...
else
TextBox1.ReadOnly = true;
if(dateTime.Now.Month == 1 && DateTime.Now.Day > 20)
TextBox1.ReadOnly = false;
else if(dateTime.Now.Month == 2 && DateTime.Now.Day > 20)
TextBox1.ReadOnly = false;
...
else
TextBox1.ReadOnly = true;
ASKER