+ All Categories
Home > Documents > Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS,...

Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS,...

Date post: 04-Feb-2018
Category:
Upload: duongmien
View: 216 times
Download: 0 times
Share this document with a friend
17
-started in 1998. ASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) Web Pages Single Pages Model MVC Model View Controller Web Forms Event Driven Model Simplest ASP.NET model. Similar to PHP and classic ASP. Built-in templates and helpers for database, video, graphics, social media and more. MVC separates web applications into 3 different components: Models for data Views for display Controllers for input The traditional ASP.NET event driven development model: Web pages with added server controls, server events, and server code. ASP.NET supports three different development models: Web Pages, MVC (Model View Controller), and Web Forms: -Web pages and web forms a pretty straight forward. -MVC explanation: Models for dana(actually represent databases or capacity which actually holds the dana). Views for display(Representing the output), Controlers for input( Inputs done by the user and expect some outcome when input is done eventually) Classic ASP - Active Server Pages Active Server Pages (ASP), also known as Classic ASP, was introduced in 1998 as Microsoft's first server side scripting engine. ASP is a technology that enables scripts in web pages to be executed by an Internet server. ASP pages have the file extension .asp, and are normally written in VBScript. If you want to learn Classic ASP, visit our Classic ASP Tutorial . ASP.NET (extremely important)
Transcript
Page 1: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

-started in 1998.

ASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB)

Web PagesSingle Pages Model

MVCModel View Controller

Web FormsEvent Driven Model

Simplest ASP.NET model.

Similar to PHP and classic ASP.

Built-in templates and helpers for database, video, graphics, social

media and more.

MVC separates web applications into 3

different components:

Models for dataViews for display

Controllers for input

The traditional ASP.NET event driven development model:

Web pages with added server controls, server events, and

server code.

ASP.NET supports three different development models:Web Pages, MVC (Model View Controller), and Web Forms:

-Web pages and web forms a pretty straight forward.

-MVC explanation: Models for dana(actually represent databases or capacity which actually holds the dana). Views for display(Representing the output), Controlers for input( Inputs done by the user and expect some outcome when input is done eventually)

Classic ASP - Active Server PagesActive Server Pages (ASP), also known as Classic ASP, was introduced in 1998 as Microsoft's first server side scripting engine.

ASP is a technology that enables scripts in web pages to be executed by an Internet server.

ASP pages have the file extension .asp, and are normally written in VBScript.

If you want to learn Classic ASP, visit our Classic ASP Tutorial.

ASP.NET (extremely important)ASP.NET is a new ASP generation. It is not compatible with Classic ASP, but ASP.NET may include Classic ASP.

ASP.NET pages are compiled, which makes them faster than Classic ASP.

Page 2: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

ASP.NET has better language support, a large set of user controls, XML-based components, and integrated user authentication.

ASP.NET pages have the extension .aspx, and are normally written in VB (Visual Basic) or C# (C sharp).

User controls in ASP.NET can be written in different languages, including C++ and Java.

When a browser requests an ASP.NET file, the ASP.NET engine reads the file, compiles and executes the scripts in the file, and returns the result to the browser as plain HTML.

ASP.NET Razor (@variable/function)Razor is a new and simple markup syntax for embedding server code into ASP.NET web pages(embedding server code into html pages), much like Classic ASP.

Razor has the power of traditional ASP.NET, but is easier to use and easier to learn.

What is Razor? (In depth) (output always plain html) Razor is a markup syntax for adding server-based code to web pages Razor has the power of traditional ASP.NET markup, but is easier to learn, and

easier to use Razor is a server side markup syntax much like ASP and PHP Razor supports C# and Visual Basic programming languages

Main Razor Syntax Rules for C# (extremely important) Razor code blocks are enclosed in @{ ... } Inline expressions (variables and functions) start with @ Code statements end with semicolon Variables are declared with the var keyword Strings are enclosed with quotation marks C# code is case sensitive C# files have the extension .cshtml

Using a Layout Page

Page 3: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

In the previous section, you saw that including the same content in many web pages is easy.

Another approach to creating a consistent look is to use a layout page. A layout page contains the structure, but not the content, of a web page. When a web page (content page) is linked to a layout page, it will be displayed according to the layout page (template).

The layout page is just like a normal web page, except from a call to the @RenderBody() method where the content page will be included.

Each content page must start with a Layout directive.

Layout Page:<html><body><p>This is header text</p>@RenderBody()<p>&copy; 2012 W3Schools. All rights reserved.</p></body></html>

Any Web Page:@{Layout="Layout.cshtml";}

<h1>Welcome to W3Schools</h1>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laborisnisi ut aliquip ex ea commodo consequat.</p>

D.R.Y. - Don't Repeat YourselfWith two ASP.NET tools, Content Blocks and Layout Pages, you can give your web applications a consistent look.

These tools also save you a lot of work, since you don't have to repeat the same information on all pages. Centralizing markup, style, and code makes web applications much more manageable and easier to maintain.

Hiding Sensitive Information

Page 4: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

With ASP.NET, the common way to hide sensitive information (database passwords, email passwords, etc.) is to keep the information in a separate file named "_AppStart".

_AppStart.cshtml@{WebMail.SmtpServer = "mailserver.example.com";WebMail.EnableSsl = true;WebMail.UserName = "[email protected]";WebMail.Password = "your-password";WebMail.From = "[email protected]";}

Folders, paths, urls, virtual and physical names:

FOLDERS

The "Account" folder contains logon and security files The "App_Data" folder contains databases and data files The "Images" folder contains images The "Scripts" folder contains browser scripts The "Shared" folder contains common files (like layout and style files)

VIRTUAL AND PHYSICAL

The physical structure for the "Images" folder at the website above might look like this on a computer:

C:\Johnny\Documents\MyWebSites\Demo\Images 

From the example above:

The virtual name of a web picture might be "Images/pic31.jpg". //virtual means on the net

But the physical name is "C:\Johnny\Documents\MyWebSites\Demo\Images\pic31.jpg"// on the hard

URLs and PathsURLs are used to access files from the web: http://www.w3schools.com/html/html5_intro.asp

The URL corresponds to a physical file on a server: C:\MyWebSites\w3schools\html\html5_intro.asp 

A virtual path is shorthand to represent physical paths. If you use virtual paths, you can move your pages to a different domain (or server) without having to update the paths.

Page 5: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

URLs and PATHS

URLs are used to access files from the web: http://www.w3schools.com/html/html5_intro.asp

The URL corresponds to a physical file on a server: C:\MyWebSites\w3schools\html\html5_intro.asp 

A virtual path is shorthand to represent physical paths. If you use virtual paths, you can move your pages to a different domain (or server) without having to update the paths.

URL: http://www.w3schools.com/html/html5_intro.aspServer name: w3schoolsVirtual path: /html/html5_intro.aspPhysical path: C:\MyWebSites\w3schools\html\html5_intro.asp

The root on a disk drive is written like C:\, but the root on a web site is  / (forward slash).

The virtual path of a web folder is (almost)

never the same as the physical folder.

In your code you will, reference both the physical path and the virtual path, depending on what you are coding.

ASP.NET has 3 tools for working with folder paths: the ~ operator, the Server.MapPath method, and the Href method.

The ~ OperatorTo specify the virtual root in programming code, use the ~ operator.

If you use the ~ operator, instead of a path, you can move your website to a different folder or location without changing any code:

var myImagesFolder = "~/images";var myStyleSheet = "~/styles/StyleSheet.css";

The Server.MapPath Method (instead of href)The Server.MapPath method converts a virtual path (/default.cshtml) to a physical path that the server can understand (C:\Johnny\MyWebSited\Demo\default.cshtml).

You will use this method when you need to open data files located on the server (data files can only be accessed with a full physical path):

Page 6: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

var pathName = "~/dataFile.txt";var fileName = Server.MapPath(pathName); -- the mehtod being used instead of ~

The Href MethodThe Href method converts a path used in the code to a path that the browser can understand (the browser cannot understand the ~ operator).

You use the Href method to create paths to resources like image files, and CSS files.

You will often use this method in HTML <a>, <img>, and <link> elements:

@{var myStyleSheet = "~/Shared/Site.css";}

<!-- This creates a link to the CSS file. --><link rel="stylesheet" type="text/css" href="@Href(myStyleSheet)" /> -- href method being used here @Href()

<!-- Same as : --><link rel="stylesheet" type="text/css" href="/Shared/Site.css" />

The Href method is a method of the WebPage Object.

Kudvenkat C#,ASP.NET

Page 7: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

-When the html page loads we are talking about the normal GET_REQUEST It is not a post back request because we are not posting back anything to the server.

-web browsers can only understand html (no php, java, asp.net…)

-when the html is sent to the client (user surfing the net) everything is destroyed in asp.net(global variables, objects ect…)

-when the client is actually looking at a html page , on the other hand the webserver doesn’t no anything about the webform that we coded.

-for example if there is a button and the user (client) presses the button a postback occurs all the variables that are written inside of the code are created , the code compiles and the objects, global variables ect… are immediately destroyed.

ViewState,Session

- To use any of the above just type ViewState[“variable_name”] Session[“Variable name”]

How does the server rember values from fields, objects if the mentioned are destroyed? VIEWSTATE

Viewstate preserves data between requests.

-in the viewstate you can store global variables, objects before they get destroyed, a viewstate is acting like a memory.

The viewstate is actually a hidden field; it is stored in the page so if you navigate away to another page this viewstate won t be available overthere which makes sense.

Example 1:

int clickscount = 1; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { TextBox1.Text = "0"; } }

protected void Button1_Click(object sender, EventArgs e) { if (ViewState["Clicks"] != null) // at the beggining viewstate is equal to null { clickscount=(int)ViewState["Clicks"]+1; } TextBox1.Text = clickscount.ToString();

Page 8: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

ViewState["Clicks"] = clickscount; }

Example 2:

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { TextBox1.Text = "0"; } }

protected void Button1_Click(object sender, EventArgs e) { int Clickscount = Convert.ToInt32(TextBox1.Text) + 1; TextBox1.Text = Clickscount.ToString(); }

Conclusion:

-All the objects (WEBFORMS:buttons, textbox,listbox, datagridview…) use Viewstate internaly!!! In other words they preserve the data once written inisde of them

-Remember html controls DO NOT PRESERVE STATE!!! AS ASP.NET CONTROLOS DO THAT using viewstate internally! This is the main difference between aspo.net controls and html controls.

- If you want to differ the html control and asp.net control just go to the .aspx file and you will see that each asp.net control has the atribute called runat and the value

runat:“server“;

html controls do not have such attributes, if you want to convert you html control to asp.net control just type runat:“server“;

example:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

<input id="Button2" type="button" value="button" />

EVENTS LIFE CYCLE

In web applications , events occur at 3 levels:

1.At the application level(example: application start)

2.At the page level(example:pageload)3.At the control level(example:button click)

Page 9: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

Web applications work on a stateless protocol(http). Every time a request is made for a webform, the folowing sequence of events occur:

1.Web application creates an instance of the requested webform.

2. Processes the events of the webform.3.Generated the html, and sends the html back to

the requested client.4.The webform gets destroyed and removed from the

memory

SESSION

Page 10: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

What is a session: a session is a unique instance of the browser. A single user can have multiple sessions, by visiting your application, with multiple instances of the browser running with a different session-id on his machine.

When a browser is opened a unique browser ID is sent to the server, the webserver then knows that this is one user. If a browser window is opened and you open another instance of the SAME browser, the same session will be involved that’s because session uses cookies internally.

So to short things out:

If you open a instance of the browser the session event will fire and that will be one session If you now open another browser instance using the same browser you will get no

incrementation because the cookie from the browser is still alive and it will actually no the same user activated another browser.

If you close both browser windows and open a new one the session event will fire again and increment

If you open another browser (let’s say opera instead of chrome) you will again have a unique session and the session will increment.

You can also use also set cookieless=”true” in the web.config file

APPLICATION

When using application variable data, the data is available across all instances of all browsers. For example if you have a number which equals 5 and you open the same page which has the same code with another browser then you will be able to see this same data on that browser.

Page 11: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

SERVER EVENTS:

Postback event:

Example of the posback event:

The button clik represents the postaback event because once clicked the information travels

To the server for proccessing.

Cached event:

Important because if you write down a text in the textbox and if you adjust this text to be displayed

IMMEDIATLY you won t' get it if you do not set the property autopostback of the object (in this case textbox) to true! Otherwise if it is false it will be hidden in a viewstate (hidden field) and THEY WILL NOT BE PROCCESSED UNTIL A POSTBACK DOESNT OCCUR!

Validation event

Page 12: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

They happen on the browser , not on the server and they are not classified as server events.if you set the property of a object to „Control to validate“ then it will automatically run before the server event happen.

Mapping virtual path to physical path using Server.MapPath method

Server.MapPath()- returns the physical path for a given virtual path(i don't really think that we will need that since you can link your links with the web storage and specify the virtual path but nevermind)

Example:

Copy and paste the following code in PageInElectronicsFolder.aspx.csResponse.Write(". returns " + Server.MapPath(".") + "<br/>");Response.Write(".. returns " + Server.MapPath("..") + "<br/>");Response.Write("~ returns " + Server.MapPath("~") + "<br/>");

Running this page would produce the following output.. returns C:\SampleWeb\SampleWeb\Categories\Electronics.. returns C:\SampleWeb\SampleWeb\Categories~ returns C:\SampleWeb\SampleWeb

Page 13: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

From the output, it should be clear thatServer.MapPath(".") returns the current physical directory of the page that you are runningServer.MapPath("..") returns the parent pysical directory of the page that you are runningServer.MapPath("~") returns the physical path of the root directory of the application 

Important:

Tilde(~) symbol resolves to the root application directory, no matter how deep you are in the folder hierarchy

Dataset and SqlAdapter class ( usefull classes )

Dataset is a collection of datatables we use dataset to store many datatables in a single collection. In other words once the instance of the dataset is created you just have to put ht e parameter (a table parameter) into the data set object variable

Example:

DataTable table1 = new DataTable("patients");

table1.Columns.Add("name");

table1.Columns.Add("id");

table1.Rows.Add("sam", 1);

table1.Rows.Add("mark", 2);

DataSet set = new DataSet("office");

set.Tables.Add(table1);

Sqladapter

The sqladapter interacts with the data table type. It can fill a datatable with a table from your sql (only by using the select… query or stored procedure,you dont have to loop through the table and save the dana into a arrays, lists, ect… )

Example:

private DataSet GetData(string SPName, SqlParameter SPParameter){    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;    SqlConnection con = new SqlConnection(CS);    SqlDataAdapter da = new SqlDataAdapter(SPName, con);    da.SelectCommand.CommandType = CommandType.StoredProcedure;    if (SPParameter != null)    {        da.SelectCommand.Parameters.Add(SPParameter);    }    DataSet DS = new DataSet(); // the dataset is now empty but it will be filled with the sqladatper table.    da.Fill(DS);    return DS;}

Page 14: Web viewASP.NET is a development framework for building web pages and web sites with:HTML, CSS, JavaScript and server scripting(C#,VB) ... (Visual Basic) or C# (C sharp)

Example2:

void FillData()

{

// 1

// Open connection

using (SqlConnection c = new SqlConnection(

Properties.Settings.Default.DataConnectionString))

{

c.Open();

// 2

// Create new DataAdapter

using (SqlDataAdapter a = new SqlDataAdapter(

"SELECT * FROM EmployeeIDs", c))

{

// 3

// Use DataAdapter to fill DataTable

DataTable t = new DataTable();

a.Fill(t);

// 4

// Render data onto the screen

// dataGridView1.DataSource = t; // <-- From your designer

}

}

}


Recommended