Title: Session 4: HTML and Web Server Controls
1Session 4HTML and Web Server Controls
2Outline
- Creating Web Forms
- Using Server Controls
- HTML Server Controls
- Web Server Controls
- Writing ASP Code
- Inline Code
- Code behind pages
- Event Procedures
- Using Page Events
3What Is a Web Form?
- .aspx extension
- Page attributes
- _at_ Page directive
- Body attributes
- Form attributes
- lt_at_ Page Language"vb" gt
- lthtmlgt
- ltbodygt
- ltform id"Form1" method"post" runat"server"gt
- lt/formgt
- lt/bodygt
- lt/htmlgt
4What is a Server Control?
ltaspButton id"Button1" runat"server"
Text"Submit"/gt
- Runat"server"
- Events happen on the server
- View state saved
- Have built-in functionality
- Common object model
- All have Id and Text attributes
- Create browser-specific HTML
5Types of Server Controls
- HTML server controls
- Web server controls
- Intrinsic controls
- Validation controls
- Rich controls
- List-bound controls
- Internet Explorer Web controls
6HTML Server Controls
- Based on HTML elements
- Exist within the System.Web.UI.HtmlControls
namespace
ltinput type"text" id"txtName" runat"server"
/gt
7Web Server Controls
- Exist within the System.Web.UI.WebControls
namespace - Control syntax
- HTML that is generated by the control
ltaspTextBox id"TextBox1" runat"server"gtText_to_
Display lt/aspTextBoxgt
ltinput name"TextBox1" type"text"
value"Text_to_Display" Id"TextBox1"/gt
8Selecting the Appropriate Control
Use Web Server Controls if
Use HTML Server Controls if
You prefer a Visual Basic-like programming model
You prefer an HTML-like object model
You are writing a page that might be used by a
variety of browsers
You are working with existing HTML pages and want
to quickly add ASP.NET Web page functionality
You need specific functionality such as a
calendar or ad rotator
The control will interact with client and server
script
Bandwidth is not a problem
Bandwidth is limited
9Writing ASP.NET Code Inline Code
- Code and content in the same file
- Different sections in the file for code and HTML
ltSCRIPT Language"vb" runat"server"gt Sub
btn_Click(s As Object, e As EventArgs) Handles
btn.Click ... End Sub lt/SCRIPTgt ltHTMLgt
ltaspButton id"btn" runat"server"/gt lt/HTMLgt
10Writing ASP.NET Code Code-Behind Pages
- Separation of code from content
- Developers and UI designers can work independently
Separate files
Single file
code
lttagsgt
code
lttagsgt
Form1.aspx.vb or Form1.aspx.cs
Form1.aspx
Form1.aspx
11Understanding How Code-Behind Pages Work
- Create separate files for user interface and
interface logic - Use _at_ Page directive to link the two files
- Pre-compile or JIT-compile
Page1.aspx.cs public class WebForm1
private void cmd1_Click()
Page1.aspx lt _at_ Page Language"c"Inherits"Proje
ct.WebForm1" Codebehind"Page1.aspx.cs" Src
"Page1.aspx.cs" gt