Niversity: Abdul Majid Niazai
Niversity: Abdul Majid Niazai
ASP.NET
Abdul Majid Niazai
[email protected]
Working With Objects
The "DateTime" object is a typical built-in ASP.NET object, but objects can also be self-
defined, a web page, a text box, a file, a database record, etc.
Objects may have methods they can perform. A database record might have a "Save" method,
an image object might have a "Rotate" method, an email object might have a "Send" method,
and so on.
Objects also have properties that describe their characteristics. A database record might have
a FirstName and a LastName property (among others).
The example below shows how to access some
properties of the DateTime object:
<table border="1">
<tr>
<th width="100px">Name</th>
<td width="100px">Value</td>
</tr>
<tr>
<td>Day</td><td>@DateTime.Now.Day</td>
</tr>
<tr>
<td>Hour</td><td>@DateTime.Now.Hour</td>
</tr>
<tr>
<td>Minute</td><td>@DateTime.Now.Minute</td>
</tr>
<tr>
<td>Second</td><td>@DateTime.Now.Second</td>
</tr>
</td>
</table>
If and Else Conditions
An important feature of dynamic web pages is that you can determine what to do based on
conditions.
The common way to do this is with the if ... else statements:
@{
var txt = "";
if(DateTime.Now.Hour > 12)
{txt = "Good Evening";}
else
{txt = "Good Morning";}
}
<html>
<body>
<p>The message is @txt</p>
</body>
</html>
ASP.NET Razor - C# Variables
A variable can be of a specific type, indicating the kind of data it stores. String variables
store string values ("Welcome to Hewad Univers"), integer variables store number values
(103), date variables store date values, etc.
Variables are declared using the var keyword, or by using the type (if you want to declare
the type), but ASP.NET can usually determine data types automatically.
Converting Data Types
The most common example is to convert string input to another type, such as
an integer or a date.
As a rule, user input comes as strings, even if the user entered a number.
Therefore, numeric input values must be converted to numbers before they
can be used in calculations.
Below is a list of common conversion
methods:
Method Description Example
AsInt() if (myString.IsInt())
Converts a string to an integer.
IsInt() {myInt=myString.AsInt();}
AsDecimal() if (myString.IsDecimal())
Converts a string to a decimal number.
IsDecimal() {myDec=myString.AsDecimal();}
AsBool() myString="True";
Converts a string to a Boolean.
IsBool() myBool=myString.AsBool();
myInt=1234;
ToString() Converts any data type to a string.
myString=myInt.ToString();
End of Chapter 03