Avatar of siddhuoops
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.
C#

Avatar of undefined
Last Comment
gelbert
SOLUTION
Avatar of gelbert
gelbert

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of siddhuoops
siddhuoops

ASKER

But we want the date to be exact after 20th. This means that they can edit the textbox before 20th but cannot edit it once it is passed 20th.
Avatar of gelbert
gelbert

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;
}
Avatar of dorothy2
dorothy2
Flag of United States of America image

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.
Avatar of siddhuoops
siddhuoops

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?
Avatar of dorothy2
dorothy2
Flag of United States of America image

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?
Avatar of siddhuoops
siddhuoops

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.
ASKER CERTIFIED SOLUTION
Avatar of dorothy2
dorothy2
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of siddhuoops
siddhuoops

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
Avatar of dorothy2
dorothy2
Flag of United States of America image

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;
Avatar of siddhuoops
siddhuoops

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.



Avatar of gelbert
gelbert

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;
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo