TutorialsRoom.com, Where knowledge lands
RSS Feed

Subscribe: RSS or e-mail

Home » Web Development » ASP.NET »

ASP.Net If Statement

bitmap_vector (1K)
If statement is one of the most important statements every top level programming language must have. It is used to make decisions based on a logical test. Learn about if statements in ASP.Net and C#.
  • Start a new project by choosing File > New > Project or simply pressing Ctrl+Shift+N in Visual Studio, choose Visual C# Web from the Project Types tree, Give your project a name and click Ok.
  • In the Default.aspx file, choose the design view by pressing Shift+F7, drag and drop two Label controls, a TextBox control and a Button control.
  • Now we will set properties for the controls using the Properties window located at the right of the screen under the Solution Explorer window by default. If the properties window is closed you can easily open it again by pressing F4. We will be using the Properties Window a lot so check the following image for what each icon is used for:
    Asp.Net Properties Window.
  • Select the first Label, search for the Text property of the label control and type: "Please enter an integer:", select the TextBox control, search for the ID property and type txtInput, the ID of the ASP.Net control is what we will use to reference the control in our code. Also, name the Button btnExecute and the other label lblResult and delete the text of the lblResult. Also, type Execute in the btnExecute Text property.
  • Layout your controls on the page so they look like following:
    if Statement.
  • Now, we want the user to enter a number in the TextBox, press the Execute button and the lblResult should tell if the number is positive or not so double click the btnExecute control and let's start typing our first if statement code.
  • Start by typing if and press the Tab key on your keyboard twice, Visual Studio will thankfully do us some help! It will complete the if statement template for us and your code will look like this:
        if (true)
        {
    
        } 
        
  • We should replace the word true with a logical condition which ASP.Net will check if it's true and executes the code between the braces only then. So type this:
        if (int.Parse(txtInput.Text) > 0)
        {
            lblResult.Text = "You've entered a positive number!";
        }
        
  • Let's explian this code, we want the lblResult to display "You've entered a positive number!" only if the user has entered a positive number (bigger than 0) but the txtInput.Text value is a string not integer so first we have to convert it to integer using int.Parse() function which converts a string to integer then check to see if this value is bigger than 0. This is called Boolean expression which will either be True or False and is exactly what the if statement wants!
  • Test your code by pressing F5 to start, typing a positive number and pressing the Execute button. This should work just fine, now enter -5 and press the Execute button again, oops, it's still displaying "You've entered a positive number!" Why is that happening?
  • Because we didn't tell our ASP.Net program what to do if we don't enter a positive number! So the lblResult still has the previous text, it didn't execute the code but also didn't reset it. To fix this logical error, we will use the else keyword of the if statement which tells ASP.Net what to do if the logical condition "is not" true.
        if (int.Parse(txtInput.Text) > 0)
        {
            lblResult.Text = "You've entered a positive number!";
        }
        else
        {
            lblResult.Text = "You've entered a negative number!";
        }
        
  • Test your code again and it should work greate for positive and negative numbers but wait, what if you enter 0? We should enhance our code a little more by encluding els if() before the else keyword which enables us to test for another logical expression, your code should now look like this:
        if (int.Parse(txtInput.Text) > 0)
        {
            lblResult.Text = "You've entered a positive number!";
        }
        else if (int.Parse(txtInput.Text) == 0)
        {
            lblResult.Text = "You've entered zero!";
        }
        else
        {
            lblResult.Text = "You've entered a negative number!";
        } 
        
  • Now, your code should work just fine. Notice the "==" which is used for comparison in C# so if you type x == 6 this will test for the value of x and return a boolean value (either true or false) while x = 6 will try to assign the value 6 to the variable x, and you will get "Cannot implicitly convert type 'int' to 'bool'" error because the if statement needs a boolean value.
Rating: 2.6/5 (44 votes)
2 Comments
Post A Comment
Clester Resonable Said,
Mon, 02 November 2009 14:14pm (GMT)

what about this: if(textbox1.text = "") { Response.Write("Please enter name!"); } What is the solution cause it turn error on the whole parenthesis?

TutorialsRoom Said,
Tue, 03 November 2009 05:38am (GMT)

change if(textbox1.text = "") to if(textbox1.text == "") because in c# you use "==" for comparison not "=". What you are doing now is assigning empty string to textbox1.text

Post Your Comment: (English Only Please)


(Required)


(Optional, will not be shown)


(Including http:// - Optional)



Type the sum of 5 + 3 (Required)

Copyright © 2007-2012 Hazem Osman. All rights reserved. Terms & Conditions