Home » Web Development » ASP.NET »
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#.
ASP.Net If Statement
- 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:
.
- 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:
.
- 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.
2 Comments
Post A CommentTutorialsRoom 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
Tutorials By Category
Top Rated Tutorials
- How to Design Blog Calendar Icon in Photoshop
Rating: 5.0/5 Votes: 1 - 2012 - Cool 3d Text,typography Photoshop Tutorial
Rating: 5.0/5 Votes: 1 - How to Design Wide Screen Laptop in Photoshop
Rating: 5.0/5 Votes: 1 - Reflection - Customize Your Glasses Reflection
Rating: 5.0/5 Votes: 1 - How to Make Aqua Navigation Bar for Your Website in Photoshop
Rating: 5.0/5 Votes: 1
Latest Exclusive Tutorials
Copyright © 2007-2012 Hazem Osman. All rights reserved.
Terms & Conditions








![[del.icio.us]](http://www.tutorialsroom.com/includes/bookmarkify/delicious.png)
![[Digg]](http://www.tutorialsroom.com/includes/bookmarkify/digg.png)
![[Facebook]](http://www.tutorialsroom.com/includes/bookmarkify/facebook.png)
![[Furl]](http://www.tutorialsroom.com/includes/bookmarkify/furl.png)
![[Google]](http://www.tutorialsroom.com/includes/bookmarkify/google.png)
![[MySpace]](http://www.tutorialsroom.com/includes/bookmarkify/myspace.png)
![[Spurl]](http://www.tutorialsroom.com/includes/bookmarkify/spurl.png)
![[Squidoo]](http://www.tutorialsroom.com/includes/bookmarkify/squidoo.png)
![[StumbleUpon]](http://www.tutorialsroom.com/includes/bookmarkify/stumbleupon.png)
![[Technorati]](http://www.tutorialsroom.com/includes/bookmarkify/technorati.png)
![[Yahoo!]](http://www.tutorialsroom.com/includes/bookmarkify/yahoo.png)
![[Email]](http://www.tutorialsroom.com/includes/bookmarkify/email.png)
what about this: if(textbox1.text = "") { Response.Write("Please enter name!"); } What is the solution cause it turn error on the whole parenthesis?