How to work with IsPostBack.

In this lesson I will go in depth and show how you can go around asking your code to perform a particular task through PostBack actions and totally neglect  !IsPostBack block of code found in Page_load event.

Let  us create an application that will allow 2 forms of user inputs :  First textbox  takes in the user name as a string , while second textbox  takes in the user date of birth, the retreive button should output the result from both first textbox and second textbox.


     protected void Page_Load(object sender, EventArgs e)
        {
       
           if (!Page.IsPostBack)
           {
                nameTextBox.Text = "Your name pls";   // default  input execute only when page load
                birthdayTextBox.Text = "m/d/yyyy";      // default input execute only when page load

            }

        }

        protected void okButton_Click(object sender, EventArgs e)
        {
            resultLabel.Text = "";
            // name textbox takes a default value
            // Default value should be clear off once a postback occur as to output user input
            //Birthday textbox accept user input check if today is someones birthday or not.
            DateTime now = DateTime.Now;
            string dow =now.Day.ToString();
            int dow1 = int.Parse(dow);
            string mon = now.Month.ToString();
            int mon1 = int.Parse(mon);
            string yearnow = now.Year.ToString();

            DateTime birthday = DateTime.Parse(birthdayTextBox.Text);
            string bow = birthday.Day.ToString();
            int bow1 = int.Parse(bow);
            string Month = birthday.Month.ToString();
            int Month1 = int.Parse(Month);
            string year = birthday.Year.ToString();
            // string old = now.Subtract(birthday).ToString();
            int convertyearnow = int.Parse(yearnow);
            int convertyear = int.Parse(year);
            int old = convertyearnow - convertyear;
            string yourage = old.ToString();
            string dayold = (old > 7) ? "days" : "day"; // checks if the person is just newly born
            string suffix;
            int[] suffix2 = new int[3] { 11, 12, 13 };// check  the days order of positions
            {
            if (suffix2.Contains(old))
            {
                suffix = "th";
            }
            else if (old % 10 == 1)
            {
                suffix = "st";
            }
            else if (old % 10 == 2)
            {
                suffix = "nd";
            }
            else if (old % 10 == 3)
            {
                suffix = "rd";
            }
            else
            {
                suffix = "th";
            }
   
        }
     
             if ( mon1 == Month1 && dow1 == bow1 && convertyear < convertyearnow)
            {
           resultLabel.Text = string.Format("Welcome {0} ,
Today {1},{2} is your birthday, Congratulations on your {3}{4} birthday.",
nameTextBox.Text, mon, dow,  yourage,suffix);
               
            }
     
            else if (dow1 == bow1 && mon1 == Month1 && convertyear == convertyearnow)
            {
                resultLabel.Text = string.Format("Welcome {0}, Today {1},{2},{3} is your birthday and  you are just {4} {5}", nameTextBox.Text,  mon, dow, yearnow, yourage, dayold);
            }
            else if ( convertyear > convertyearnow)
            {
                resultLabel.Text = string.Format
("Oops your birthdate is greater than current,Today is {0}.                
<br> Did you forget your calendar", now.ToShortDateString());
            }
       
            else
            {
                resultLabel.Text = string.Format("Welcome {0} ,
 Today {1},{2},{3} is not your birthday ", nameTextBox.Text,  mon, dow, yearnow);
            }
       
       
            // resultLabel1.Text = suffix;

        }
    }
}





1 .Application : Default load view











2. Application:  Put date that is not same with the current date















3. Application: Put date that is greater than current date















4. Application: if input date is correct with date now, it then wishes you happy birthday

















Note:
Throughout this app our code loads nameTextBox.Text = "Your name pls";and birthdayTextBox.Text = "m/d/yyyy";  only at the default load view and once a button is click after new user inputs has been typed in, PostBack occur, This grab and submit new inputs from users.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

New Post

TSQL-Working With Date And Time

Here you will learn how to work with the date and time in SQL Server. select  GETDATE() as dateNow This returns the current date and time. s...