1111 creating html programatically. 2222 objectives you will be able to invoke c# code on the server...
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_DemoTRANSCRIPT
1111
Creating HTML Programatically
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.
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
New Web Site
4
5
New Empty Web Site
6
Add New Item
7
Default.aspx
8 8 8
Boilerplate Created by Visual Studio
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>
10 10 10
Customized Boilerplate
11 11 11
Try it!
Title
Heading
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.
13 13 13
Invoking Server Code
= says to insert the text produced by the code into the HTML, replacing the <% %>.
14 14 14
Server Code Output in Chrome
View source on the browser.
15
What the Browser Received
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.
17 17 17
Server Code Shown Directly
18 18 18
The Design View
Server code output is not shown at design time.
19
Page in Chrome
20
A Code Behind Function
Note: This is not an event handler.
21
Invoking Code Behind Function
22
Page in Chrome
23
Page_Load What would happen if we did the Response.Write
in the Page_Load event handler?
24
HTML Output from Page_Load
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