1111 creating html programatically. 2222 objectives you will be able to invoke c# code on the server...

25
1 Creating HTML Programatically

Upload: grace-chandler

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

333 Example We will use C# code to output the current date and time each time a page is requested by a browser. Later we will extend the example to show dynamically created controls. Start up Visual Studio and create a new C# ASP.NET empty web site. Date_Time_Demo

TRANSCRIPT

Page 1: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

1111

Creating HTML Programatically

Page 2: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

2 2 2 2

Objectives

You will be able to Invoke C# code on the server from an ASP.NET

page. Write C# code to create HTML output for an

ASP.NET page dynamically.

Page 3: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

3 3 3

Example We will use C# code to output the current

date and time each time a page is requested by a browser. Later we will extend the example to show

dynamically created controls.

Start up Visual Studio and create a new C# ASP.NET empty web site. Date_Time_Demo

Page 4: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

New Web Site

4

Page 5: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

5

New Empty Web Site

Page 6: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

6

Add New Item

Page 7: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

7

Default.aspx

Page 8: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

8 8 8

Boilerplate Created by Visual Studio

Page 9: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

9 9 9

Customize the Boilerplate

Set the title to a meaningful name:Date Time Demo

Add a top level heading inside the <div> tags:

<h1>Creating HTML Programatically</h1>

Page 10: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

10 10 10

Customized Boilerplate

Page 11: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

11 11 11

Try it!

Title

Heading

Page 12: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

12 12 12

Server Code

We can add tags to the ASP.NET source to be processed on the server as the ASP markup is translated into pure HTML.

<% ... %>

Between the <% %> tags include normal C# code.

Typically function calls

Output goes into the HTML stream at that point. We saw an example last week where we put the

application start time onto a page as plain text.

Page 13: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

13 13 13

Invoking Server Code

= says to insert the text produced by the code into the HTML, replacing the <% %>.

Page 14: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

14 14 14

Server Code Output in Chrome

View source on the browser.

Page 15: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

15

What the Browser Received

Page 16: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

16 16 16

Response.Write()

The = inside <% %> is shorthand for Response.Write( );

Response is an object that embodies the response that will be sent back to the browser in response to the current request.

Its Write() method inserts text into the HTML output at the current position.

Page 17: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

17 17 17

Server Code Shown Directly

Page 18: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

18 18 18

The Design View

Server code output is not shown at design time.

Page 19: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

19

Page in Chrome

Page 20: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

20

A Code Behind Function

Note: This is not an event handler.

Page 21: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

21

Invoking Code Behind Function

Page 22: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

22

Page in Chrome

Page 23: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

23

Page_Load What would happen if we did the Response.Write

in the Page_Load event handler?

Page 24: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

24

HTML Output from Page_Load

Page 25: 1111 Creating HTML Programatically. 2222 Objectives You will be able to Invoke C# code on the server from an ASP.NET page. Write C# code to create HTML

25

Summary We can invoke a function in the code behind file

from any place in a .aspx file to output any HTML using Response.Write( ).

<% Some_Function(); %> The HTML can be determined at run time from

user inputs, database contents, etc. The dynamically created HTML goes into the

page at the point where we invoke the Response.Write( ) function.

End of Presentation