Sei sulla pagina 1di 29

BIE 33103

DOTNET PROGRAMMING

SEM 1I 2016/2017

Programming ASP.Net Web Pages


Data types and variables
Declare two variables: an integer (int) to hold a number
and a String (string) to hold a piece of text:
//Declare a variable of type int to hold medium
sized whole numbers.
int distanceInMiles;
distanceInMiles =437;
//Declare a variable to hold some text like a
first name.
string firstName;
firstName = Danish;

Or we can write like this:


int distanceInMiles =437;
string firstName = Danish;
Common Type System (CTS)
namespace
.NET C# DESCRIPTION
System.Int16 short Capable of storing whole numbers
between -32,768. Defaults to 0 .
System.Int32 int Capable of storing whole numbers
between -2,147,648 and 2,147,648.
Defaults to 0 .
System.Single float Store large numbers with decimals
between -3.402823E+38 and
3.402823E+38. Defaults to 0.0 .
System.Boolean bool Used to hold a simple boolean value: true
or false. Defaults to false.
System.Char char Holds a single character. Default to null.
System.String string Can hold text with length of up to 2
billion characters. Defaults to null.
Converting and Casting Data Types
1. Convert into a String.
For example,
the Text returned from a TextBox is a String, and so
is the SelectedValue of a DropDownList.
To get a string representation of an Object, you can call its
ToString()method.
Label1.text =
System.Datetime.Now.ToString();
Converting and Casting
Data Types
2. Convert into a Convert class.
Function to convert a number of data types into another
type.
For example,
converting a string containing a value that looks like a
boolean into a true Boolean type:
bool myBoolean1 =
Convert.ToBoolean(True);//Results in true
bool myBoolean2 =
Convert.ToBoolean(False); //Results in
false
Arrays
Defines an array:
string[] roles = new string[2];
To enter the role names into array:
roles[0]= Administrators;
roles[1]= ContentManagers;
Throw an error:
roles[2]= Members;
Index was outside the bounds of the array.
Statements: Operators
1. Assignment Operators
2. Arithmetic Operators
3. Comparison Operators
4. Concatenation Operators
5. Logical Operators
Statements: Operators
1. Assignment Operators
Eg. int age = 42;
Used to assign a value to a variable
Sources :
A constant value
Value of another variable
The result of an expression or a function
Statements: Operators
2. Arithmetic Operators
Combine the arithmetic and assignment operators,
enabling to take the value of a variable, perform some
arithmetic operation on it, and assign the result back to
the variable.
Example:
C# also enables to increase a variables value by using the
++ operator.
Example:
Statements: Operators
3. Comparison Operators
Statements: Operators
4. Concatenation Operators
To concatenate two strings, use the + in C#.
To combine the concatenation and assignment operator
, use += and &=
Example:
Statements: Operators
5. Logical Operators
To combine the results of multiple individual expression,
To make sure that multiple conditions are true or false.
Statements: Operators
5. Logical Operators
Example:

Guess what the answer?


Making Decisions
1. if, if else and elseif constructs
if statement simple
Contains two relevant parts :
1. Condition being tested
2. Code executed when condition evaluates to true
E.g:
if (User.IsInRole(Administrators) = true)
if (User.IsInRole(Administrators))
{ {
DeleteButton.Visible
DeleteButton.Visible = true;
= true;
}
}
Making Decisions
1. if, if else and elseif constructs
if else statement
E.g:
if (User.IsInRole(Administrators))
{
DeleteButton.Visible = true;
}
else
{
DeleteButton.Visible = false;
}
Making Decisions
elseif statement more than 2 option
E.g: use 3 different roles :
Administrators create and delete content.
Content managers only create new content.
standard members cannot do anything.

To show or hide the relevant buttons, use the following


code:
Making Decisions
if (User.IsInRole(Administrators))
{
CreateNewArticleButton.Visible = true;
DeleteArticleButton.Visible = true;
}
else if (User.IsInRole(ContentManagers))
{
CreateNewArticleButton.Visible = true;
DeleteArticleButton.Visible = false;
}
else if (User.IsInRole(Members))
{
CreateNewArticleButton.Visible = false;
DeleteArticleButton.Visible = false;
}
Making Decisions
switch constructs
E.g. to calculate the discount percentage:
DateTime today = DateTime.Now;
double discountPercentage = 0;
switch (today.DayOfWeek)
{ case DayOfWeek.Monday;
discountPercentage = 40; break;
case DayOfWeek.Tuesday;
discountPercentage = 30; break;
case DayOfWeek.Wednesday;
discountPercentage = 20; break;
case DayOfWeek.Thursday;
discountPercentage = 10; break;
default:
discountPercentage = 0; break;
}
Making Decisions
2. Loops
for loop- simply repeat its code a predefined number of
time
Define exact number of iteration when set up the loop
Syntax:
for (startCondition; endCondition;step definition)
Example:
for (int loopCount=1; loopCount<=10; loopCount++)
{
Label1.Text += loopCount.ToString() + <br />;
}
Making Decisions
2. Loops
foreach loop- simply iterate over all the items in a
collection
Example:
foreach (string role in roles)
{
Label1.Text += role + <br />;
}
Making Decisions
2. Loops
while loop- able to loop while a certain condition is true
Example:
bool success = false;
int loopCount = 0;
while (!success && loopCount < 3)
{
success = SendEmailMessage();
loopCount = loopCount + 1;
}
Simple Calculator
Create a new Web Form called
CalculatorDemo.aspx in the Demos folder.
Write the script code to get an output in Design View as
shown in Figure Q1.

Figure Q1
CalculatorDemo.aspx
<title> Calculator</title>
</head>
Example
<body>
<form id="form1" runat="server">
Value 1:
<asp:TextBox ID="valueBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="operatorDropDownList" runat="server">
<asp:ListItem value="+"> </asp:ListItem>
<asp:ListItem value="-"> </asp:ListItem>
<asp:ListItem value="*"> </asp:ListItem>
<asp:ListItem value="/"> </asp:ListItem>
</asp:DropDownList>
Value 2:
<asp:TextBox ID="valueBox2" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID ="CalculateButton" runat="server"
OnClick="calculateButton_Click" Text="Calculate" />
Result:
<asp:TextBox ID="resultTextBox" runat="server"></asp:TextBox>
</form>
</body>
</html>
The code behind
protected void calculateButton_Click(object resultTextBox.Text=result.ToString();
sender, EventArgs e)
}
{
else
if (valueBox1.Text.Length > 0 &&
{
valueBox2.Text.Length > 0)
resultTextBox.Text=string.Empty;
{
}
double result = 0;
}
double value1 =
Convert.ToDouble(valueBox1.Text);
double value2 =
Convert.ToDouble(valueBox2.Text);
switch (operatorDropDownList.SelectedValue)
{
case "+":
result = value1 + value2;
Programming in the
break;
case "-":
code - .aspx.cs
result = value1 - value2;
break;
case "*":
result = value1 * value2;
break; 1 2
case "/":
result = value1 / value2;
break; The Result
}
The Result

Potrebbero piacerti anche