Sie sind auf Seite 1von 26

ASP.

NET

ASP.NET is otherwise known as ACTIVE SERVER PAGE .NET. It is basically a technology


through which user can develop server based program or web based program, using the language
C#.NET or VB.NET. It is basically the advanced version of ASP. ASP.NET applications are basically
required to develop platform independent as well as secured application.

NOTE:- ASP application is capable to run in any O.S provided the O.S must have a browser.To run
the application in a network(collection of machines) minimum one machine has to be a windows
server.

ASP.NET application requires 2 different type of coding


 HTML code
 CODE BEHIND

HTML code is basically appeared in a client browser whereas CODE BEHIND is actually the
program resided inside the server.

2 types of operations are being done in between client and server i.e. REQUEST and
RESPONSE which are done through HTML & JSCRIPT.

NOTE: Any language compatible with HTML to make the HTML dynamic is known as a scripting
language. 2 different types of scripting languages are available
1. VB SCRIPT  Browser dependent
2. JSCRIPT  Browser independent

GRAPHICAL REPRESENTATION OF WEB APPLICATION NAMED “BANKSOFT” AND HOW IT IS MADE


GRAPHICAL REPRESENTATION OF WEB SERVER

Whenever a program request is made from the client,the request is made to the web server
and a web server has a primary component called IIS(internet information service)

IIS is basically a technology or a mechanism available in the web server responsible to


execute and send the output to the client using other web server components.IIS has a primary sub
component called ISAPI(internet server application program interface)responsible to execute the
program.The IIS will do the following job in between the request is made and the output is given
back.

 It creates the web process and collects all the resources and connects to the databse if
required.
 It send the program to the framework for compilation purpose and making the exe.
 Once the exe is created IIS will provide it to the ISAPI for execution.
 Once the exe is executed ,the output is again converted to HTML/Jscript and given back to
client.
 Once all the above jobs are done the process is de-allocated by IIS.

NOTE:-The time span between the web process start and web process end is known as session.

STEPS TO OPEN ASP.NET

To start ASP.NET application, from the NEW PROJECT window choose the language as C#
and application as ASP.NET. Then choose webform item and click on ok
Now a new ASP.NET project will start having a default page. The ASP.NET IDE consist of
following components i.e. toolbox, property window, solution explorer etc
To create a new page Right click over the project name from the solution explorer and ADD
NEWITEM and choose WEB FORM. Each webform comprising of 3 views i.e.

1. Design view  It is responsible for design to design the page either by putting the controls
over the page or by writing code behinds.
2. Source view  using this view user can see the equivalent HTML code of a web form and
controls over it.
3. Split view  it’s a combination of design view and source view.

To develop a web application ASP.net provides 2 types of controls.


1. Server controls or web controls
2. HTML controls

Server controls are those controls which provides events and basically meant for dynamic
effects. This controls are basically executed in the server side.
Whereas HTML controls are otherwise known as static controls neither they provides
events nor they execute in the server.

NOTE:- In other words server controls are responsible for dynamic web page development and
HTML controls are responsible for static web page development.
NOTE:- Asp.net server controls are identified by asp keyword in HTML view whereas HTML controls
are identified by input keyword in HTML view.

HTML view has primarily 3 different blocks


<HTML>
<HEAD>

</HEAD>
<BODY>

</BODY>
</HTML>

Head …End Head is responsible to hold all the information regarding a page.
Body….End Body hold all the HTML codes for several controls available in the page.

NOTE:- Asp.net basically creates 2 major files.


1) Webform1.aspx :- which holds the interface and served to a client depending on the
request.
2) Webform1.aspx.vb/cs :- This is the code behind file available inside the server which
contains the programs of an interface.

NOTE:- Asp.net provides a major class which is responsible to create individual webforms and i.e.
System. Web. UI. Page
Namespace class
CONTROLS IN ASP.NET

1. LABEL :- It is a server control which is responsible to hold static data. In other words label is
responsible to provide an identification mark to an object.The basic difference between a
label and a textbox is , label holds static data whereas textbox holds dynamic data. The
property of a label can be changed during runtime using an event. Important properties of a
label are

Property Description

AccessKey It is used to set keyboard shortcut for the label.

TabIndex The tab order of the control.

BackColor It is used to set background color of the label.

BorderColor It is used to set border color of the label.

BorderWidth It is used to set width of border of the label.

Font It is used to set font for the label text.

ForeColor It is used to set color of the label text.

Text It is used to set text to be shown for the label.

ToolTip It displays the text when mouse is over the label.

Visible To set visibility of control on the form.

Height It is used to set height of the control.

Width It is used to set width of the control.

Eg. protected void Button1_Click(object sender, EventArgs e)


{
Label1.backcolor=color.red;
}

NOTE:- To access the Color structure user have to use System.Drawing namespace.
2. BUTTON: Button is otherwise known as a process control which accepts the input in form of
event. ASP.NET provides 3 different types of buttons

a. GENERAL BUTTON REPRESENTED THROUGH TEXT

Property Description

AccessKey It is used to set keyboard shortcut for the control.

TabIndex The tab order of the control.

BackColor It is used to set background color of the control.

BorderColor It is used to set border color of the control.

BorderWidth It is used to set width of border of the control.

Font It is used to set font for the control text.

ForeColor It is used to set color of the control text.

Text It is used to set text to be shown for the control.

ToolTip It displays the text when mouse is over the control.

Visible To set visibility of control on the form.

Height It is used to set height of the control.

Width It is used to set width of the control.

b. LINK BUTTON REPRESENTED LIKE HYPERLINK


c. IMAGE BUTTON REPRESENTED THROUGH IMAGE

Create one-one button over the form and change their required properties such as border
color, border style, border width, font etc.

NOTE :- For image button gather all the images ( JPEG/ GIF ) and store them inside a folder within
the same project. To add an image in image buttonRIGHT CLICK OVER PROJECT FROM SOLUTION
EXPLORERADDADD EXISTING ITEMTHEN SELECT THE JPG FILE
protected void Button1_Click(object sender, EventArgs e)
{
Label3.Text = "you clicked on button";
}

protected void LinkButton1_Click(object sender, EventArgs e)


{
Label1.Text = "WELCOME ALL";
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Button1.BackColor = Color.Red;
}

3. TEXT BOX:- Text box is otherwise known as input control which accepts the input at
runtime. Its important properties are listed below.

Property Description

AccessKey It is used to set keyboard shortcut for the control.

TabIndex The tab order of the control.

BackColor It is used to set background color of the control.

BorderColor It is used to set border color of the control.


BorderWidth It is used to set width of border of the control.

Font It is used to set font for the control text.

ForeColor It is used to set color of the control text.

Text It is used to set text to be shown for the control.

ToolTip It displays the text when mouse is over the control.

Visible To set visibility of control on the form.

Height It is used to set height of the control.

Width It is used to set width of the control.

MaxLength It is used to set maximum number of characters that can be entered.

Readonly It is used to make control readonly.

protected void Button1_Click(object sender, EventArgs e)


{
TextBox3.Text = TextBox1.Text;
}

4. CHECK BOX AND RADIO BUTTONS: Check box and Radio button are standard controls. The
basic difference between them is checkbox allows the user to choose multiple values from a
list of value whereas radio button allow the user to choose single value from a list of values.
NOTE:- Other than the button, to send the request to the server, user has to make any control’s
AutoPostBack property to True. If it is false then it won’t send the request to the server and the
controls won’t work.

EXAMPLE OF CHECKBOX

protected void Page_Load(object sender, EventArgs e)


{
TextBox3.Text = "NONE";
}

protected void Button1_Click(object sender, EventArgs e)


{
var msg="";
if (CheckBox1.Checked)
msg+ = CheckBox1.Text+" ";
if (CheckBox2.Checked)
msg += CheckBox2.Text + " ";
if (CheckBox3.Checked)
msg += CheckBox3.Text + " ";
TextBox3.Text = msg;

EXAMPLE OF RADIOBUTTON

protected void Button1_Click(object sender, EventArgs e)


{
if (RadioButton1.Checked)
TextBox3.Text = "your gender is " + RadioButton1.Text;
else if(RadioButton2 .Checked )
TextBox3.Text = "your gender is " + RadioButton2.Text;

NOTE:- Change minimum 3 properties e.g. GroupName , AutoPostBack , Text. Other than the
button, to send the request to the server, user has to make any control’s AutoPostBack property to
True. If it is false then it won’t send the request to the server and the controls won’t work.

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)


{
if (CheckBox1.Checked==true )
{
TextBox3.TextMode = TextBoxMode.SingleLine;
CheckBox1.Text = "hide password";
}
if (CheckBox1.Checked == false)
{
TextBox3.TextMode = TextBoxMode.Password;
CheckBox1.Text = "show password";
}
}

protected void CheckBox2_CheckedChanged(object sender, EventArgs e)


{
if (CheckBox2.Checked == true)
TextBox3.BackColor = Color.Red;
if (CheckBox1.Checked == false)
TextBox3.BackColor = Color.Chocolate;
}
protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox3.Checked == true)
TextBox3.ForeColor = Color.Blue;
if (CheckBox3.Checked == false)
TextBox3.ForeColor = Color.Black;
Q.Design the form as given below.

5. DROP DOWN LIST BOX(COMBO BOX):- Combo box is nothing but the extended version of
radio button which allow the user to choose 1 value from a list of values. Combo box always
holds the values in a hidden format. When it is clicked all the values are displayed and user
is allowed to choose one value out of that. User can add values in a combo box in 2 different
manners i.e.
a. During design time
b. During runtime

CODE BEHIND (TO ADD VALUES TYPED IN THE TEXTBOX AND ON CLICKING THE BUTTON)

protected void Button1_Click(object sender, EventArgs e)


{
DropDownList1.Items.Add(TextBox3.Text);
TextBox3.Text = "";
}

CODE BEHIND (TO ADD VALUES AND CHANGING THE COLOR OF THE TEXT BOX DYNAMICALLY)

protected void Page_Load(object sender, EventArgs e)


{
if (this.IsPostBack == false)
{
DropDownList1.Items.Add("red");
DropDownList1.Items.Add("blue");
DropDownList1.Items.Add("yellow");
DropDownList1.Items.Add("blue");
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox3.BackColor = Color.FromName(DropDownList1.SelectedItem.ToString());
}

NOTE:- Here if (this.IsPostBack == false) is basically a condition which always treat as if the page is
loaded for the first time. For control AutoPostBack and for form IsPostBack.

protected void Button1_Click(object sender, EventArgs e)


{
if (DropDownList1.SelectedValue == "")
{
TextBox3.Text = "PLEASE SELECT A COUNTRY";
}
else
{
TextBox3.Text = "your choice is" + DropDownList1.SelectedValue;
}

NOTE:- SelectedItem selects the index and SelectedValue selects the value of the index.

6. LIST BOX:-List box is a standard control which is treated as the extended version of check
box. Which allow the user to choose multiple values from a list of values. List box is identical
with combo box as far as events and properties are concerned. User can add the values in list
box either in design time or in runtime.

Example 1: Printing array using foreach loop

Class loop

Public static void main(String[ ] args)


{

Char[ ] str={‘h’,’e’,’l’,’l’,’o’};

foreach(char ch in str)

Console.WriteLine(ch);

protected void Button1_Click(object sender, EventArgs e)


{
ListBox2.Items.Clear();
foreach (ListItem lst in ListBox1.Items)
{
if (lst.Selected == true)
ListBox2.Items.Add(lst.Text);
}
}

7. RADIO BUTTON LIST : It is an extended version of a radio button. Advantages are it


doesn’t require any group name and coding will be less. Create a radio button list over the form
and change the following properties i.e. Items, RepeatDirection (Horizontal or Vertical).

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)


{
ListBox1.BackColor = Color.FromName(RadioButtonList1.SelectedItem.ToString());
}
8. CALENDAR : This control is responsible to select a date from a calendar.

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)


{
if (CheckBox1.Checked == true)
Calendar1.Visible = true;
if (CheckBox1.Checked == false)
Calendar1.Visible = false;
}

protected void Calendar1_SelectionChanged(object sender, EventArgs e)


{
TextBox3.Text = Calendar1.SelectedDate.ToString("dd-MM-yy");
Calendar1.Visible = false;
CheckBox1.Checked = false;
}

9. CHECK BOX LIST :- Check box list is an extended version of check box. Advantage of check
box list is it supports less coding.
Code behind

protected void Page_Load(object sender, EventArgs e)


{
if (this.IsPostBack == false)
{
CheckBoxList1.Items.Add("bbb");
}
}

protected void Button1_Click(object sender, EventArgs e)


{
ListBox1.Items.Clear();
foreach (ListItem lst in CheckBoxList1.Items)
{
if (lst.Selected == true)
{
ListBox1.Items.Add(lst.Value);
}
}
}

WORKING WITH ‘TABLE’ MENU

Table menu is required to design a form in a customized manner with proper alignment. To
design a table click over the table menu in menu bar and click on insert table.

Provide the number of rows, columns, cell spacing, cell padding, border color etc. and click
on OK. Choose individual cell and create controls within the cells. Design the form in the following
manner.
WORKING WITH VALIDATORS

Validators are otherwise known as frontend constraints responsible to restrict data in


frontend layer. Validators are categorized in 2 different manners
 Client side
 Server side

Client side validators doesn’t require any program whereas server side ( custom ) validators
have their own event.

WORKING WITH CLIENT SIDE VALIDATORS

1. REQUIRE FIELD VALIDATOR: This validator acts like NOT NULL in database. This validator
will show error when the control remains blank. Create the control over the form and change the
following properties
a. Error message
b. Fore color
c. Control to validate

2. COMPARE VALIDATOR: This validator is required to compare 2 values and validate. Drag
the control over the form and change the following properties. a. Error message
b. Control to validate
c. Control to compare
d. Type
e. Operator
f. Tool tip text

3. RANGE VALIDATOR: This validator is responsible to check a range of values and validate
it. Drag the control over the form and change the following properties.
a. Forecolor
b. Error message
c. Control to validate
d. Type
e. Max value
f. Min value

4. REGULAR EXPRESSION VALIDATOR: This validator is responsible to check the format such
as mail id, internet URL, pin code etc. Create the control over the form and change the following
properties.
a. Error message
b. Control to validate
c. Validation expression
d. Tool tip text

WORKING WITH SERVER SIDE (CUSTOM) VALIDATORS

5. CUSTOM VALIDATOR: This is a server validator responsible to check error


programmatically. This validator has one event called ServerValidate. Create the custom validator
over the form and change the following properties.
a. Control to validate
b. Fore color
c. Error message

Following example is to check whether an user id is greater than 5 characters or not.

Here ** Password Mismatched is the only server side validators whereas other are client side
validators.
Double click over the validator and write the following code.

Protected void CustomValidator1_ServerValidate(object source,ServerValidateEventArgs args)


{
if(args.Value.Length<5)
{
args.IsValid=false;
}
}
6. VALIDATION SUMMARY: This validator is responsible to display all errors at a single
glance. This validator is not confined to a single control rather than attached to whole page. Create
the validator over the page and change the following properties.
a. Header text
b. Show message box
c. Show summary
d. Fore color

WORKING WITH PLACE HOLDERS

ASP.NET not only allow the user to create control during design time but also allow the user to
create control during runtime. Place Holder is nothing but a container which contains dynamic controls
created during runtime.

Label l1 = new Label();


l1.Text = "user id";
l1.ForeColor = Color.Red;
Label l2 = new Label();
l2.Text = "user id";
l2.ForeColor = Color.Red;
TextBox t1 = new TextBox();
t1.ForeColor = Color.Blue;
TextBox t2 = new TextBox();
t2.ForeColor = Color.Blue;
t2.TextMode = TextBoxMode.Password;
Button b1 = new Button();
b1.Text = "submit";
PlaceHolder1.Controls.Add(l1);
PlaceHolder2.Controls.Add(t1);
PlaceHolder3.Controls.Add(l2);
PlaceHolder4.Controls.Add(t2);
PlaceHolder5.Controls.Add(b1);
WORKING WITH ‘TABLE’ CONTROL

Table control is a control required to represent the data in a 2-dimensional manner. Table
control represents data in a read only mode. Create the table control over the form and change the
following properties i.e. cell padding, cell spacing, font, caption, etc. click on ROWS and add the
number of rows .Add number of cells according to the requirement and provide data if required.

CODE BEHIND: To represent the data of a particular cell.

protected void Button1_Click(object sender, EventArgs e)


{
TextBox1.Text = Table1.Rows[1].Cells[0].Text;
}

WORKING WITH WEB SERVICE

Web service is basically a technology or mechanism responsible to make the .Net programs
platform independent. Once a program is created using web service technology it can be operated
on any O.S.

NOTE:- Whenever a program is created using web service it supports the technology of XML. Any
program created through web service has to be a function.

STEPS TO CREATE WEB SERVICE

STEP1
Open Visual Studio  File New  Web Site

Step2
Create “ASP.Net Empty Web Site” and save at the desired location.

STEP3

Right-click on Project folder in Solution Explorer on the right side  Add New Item

Step4
Select your language (C# in my case)  Web Service (ASMX)  Add.

NOTE:- Each function in web service starts with a keyword [WebMethod]. [WebMethod] attribute is
used to specify that this method is exposed to the user/Client to access. If you remove this
attribute, the client will not be able to see the method details, hence they can't implement it.

Example:-

[WebMethod]
public int sum(int a,int b)
{
return a+b;
}

Step5

Now execute the web service to check whether it is working or not.

STEPS TO USE WEB SERVICE IN CLIENT APPLICATION

Step 1 :- Open a fresh web application and design the page as shown below.

Step 2 :- Now go to solution explorer  right click on project i.e web application  click on add
service reference  a window will open. Now click on ADVANCED  click on ADD WEB REFERENCE
 then on the url provide the
 click on add reference  now the local host will be added in the solution explorer.

Step 3 :- Now in the button click event write the following coding.

protected void Button1_Click(object sender, EventArgs e)


{
localhost.WebService obj = new localhost.WebService();
int a,b,c;
a = int.Parse(TextBox1.Text);
b = int.Parse(TextBox2.Text);
c=obj .sum(a,b);
TextBox3.Text = Convert.ToString(c);

HOW WEB SERVER WORKS

Whenever a web method is invoked from a client , first the request is converted to XML
using a language called WSDL(web service description language).

Then the request is made to the server and the program is executed. Once the program is
executed again the output is converted to XML and given back to the client.

XML data transmitted between the client and the server using a protocol called
SOAP(simple object access protocol).

WORKING WITH ADROTATOR


Adrotator is an important control of Asp.net which allows to display different advertisement
each time the page is refreshed. It not only display the ad rather it helps to hyperlink to its
concerned site.

NOTE:- Adrotator supports a file called XML file , which is responsible to hold the advertisement.
Once the XML file is created tag it to the adrotator.

STEPS TO CREATE AN ADROTATOR

1. Go to solution explorer and right click on the project and add a new folder called images and
add the desired images in it.
2. From the solution explorer window , right click over the project name add  add new
item from the left window select data then select XML file  click on open
3. Inside the XML file write the following coding.

<Advertisements>
<Ad>
<ImageUrl>Images/Desert.jpg</ImageUrl>
<NavigateUrl>http://google.com</NavigateUrl>
<AlternateText>please visit google</AlternateText>
<Impression>50</Impression>
</Ad>

<Ad>
<ImageUrl>Images/Jellyfish.jpg</ImageUrl>
<NavigateUrl>http://yahoo.com</NavigateUrl>
<AlternateText>please visit yahoo</AlternateText>
<Impression>10</Impression>
</Ad>

<Ad>
<ImageUrl>Images/Hydrangeas.jpg</ImageUrl>
<NavigateUrl>http://rediff.com</NavigateUrl>
<AlternateText>please visit rediff</AlternateText>
<Impression>20</Impression>
</Ad>
</Advertisements>

4. Once the XML file is created save the file.

NOTE:- The advertisement contents is stored in a parent tag called advertisement.

Advertisement tag is having several ad tags which holds individual advertisements. Ad tag is
having the following sub tags.
a. ImageUrl which display the advertisement in adrotator
b. NavigateUrl  which specifies the hyperlink to a specific page
c. AlternateText  which acts as a tooltip text
d. Impression  it shows the priority of an advertisement

5. Now open the webform and drag the adrotator and go to its property. Provide the XML file
name in advertise file property.

PAGE NAVIGATION IN Asp.Net

Page navigation is a concept or a technology which is responsible to navigate through different


pages for information sharing. To navigate through different pages Asp.net provides different
methods and those are

1. Hyperlink :- This is a method through which user can navigate to another page interactively.
It doesn’t have its own events.

Drag the hyperlink control over the page  go to its property and change its following
properties.
a. Text
b. NavigateUrl
c. Tooltip text
d. Backcolor

NOTE :- Through hyperlink control we can navigate to any page of different web server.

2. Response.Redirect :- Redirect method is a method which is identical with hyperlink. But the
advantage of redirect is it can navigate to another page programmatically using events.

NOTE :- The main difference between hyperlink and redirect method is hyperlink can connect to a
html page whereas redirect method can only connect to aspx file.

Drag a link button or an image button over a page. Change its concerned properties. Double
click over the control and write the following coding.

protected void LinkButton1_Click(object sender, EventArgs e)


{
Response.Redirect("WebForm2.aspx");
}

3. Server.Execute :- This method is identical with redirect because both navigate to another
page programmatically.The advantage of execute method is it is responsible to merge the
data of sender page and recipient page and display in one page.

NOTE :- To work with server.execute go to html view of sender page and in the page tag write the
following code
<% @ Page…….Enableviewstate=”False”>
Drag a link button or image button and change the properties and double click over the
control and write the following code.

protected void LinkButton1_Click(object sender, EventArgs e)


{
Server.Execute("WebForm2.aspx");
}

4. Server.Transfer :- Transfer is a method responsible to navigate to another page by holding


some data to destination page.

NOTE :- The major advantage of this type of concept is to reduce the data entry operation.

STEPS

a. In the source page html view write the following code in the page tag.
Enableviewstatemac=”false”
b. Drag a link button or image button  change its properties  double click over the button
and write the following coding.

protected void LinkButton1_Click(object sender, EventArgs e)


{
Server.Transfer("WebForm2.aspx");
}

NOTE:- The above coding is responsible to transfer the control to webform2 by preserving the data
of webform1.
c. Double click over the destination form(webform2) and write the following coding in page
load event.

protected void Page_Load(object sender, EventArgs e)


{
System.Collections.Specialized.NameValueCollection obj = Request.Form;
TextBox1.Text = obj["Textbox1"];
TextBox2.Text = obj[“Textbox2”];
}

Das könnte Ihnen auch gefallen