10.4. Code Snippets

Web development can sometimes be pretty repetitive. No, I am not talking about creating a lengthy online questionnaire that no sane person would ever fill out but rather the markup you have to write. To ease this pain, Microsoft has introduced a number of prewritten HTML and ASP.NET snippets (blocks of code) that will substantially reduce the amount of typing you have to do and minimize typos (and possibly reduce RSI in the web development community). I am a big believer in saving time spent on tedious tasks, so let's take a look into these snippets now.

10.4.1. ASP.NET Code Snippets

Let's look at an example by following these steps:

  1. Create a new ASP.NET project.

  2. In Default.aspx, type textbox, and then quickly press Tab. Visual Studio will insert the following code:

    <asp:TextBox runat="server" />

  3. But wait—it gets better. Give your TextBox an ID of txtTest, and press Enter to add a new line.

  4. Type requiredfieldvalidator, and press Tab.

  5. Visual Studio will then insert the following markup. Note how the ControlToValidate property was set to the nearest text box automatically (if you add another text box to the page, you should still find this is the case) and how the cursor was focused on the ErrorMessage property. VS2010 will attempt to guess any properties it can (in this case because of the text box's proximity) and intelligently set the focus on any other fields that need to be filled in.

    <asp:RequiredFieldValidator ErrorMessage="errormessage" ControlToValidate="txtTest"
    runat="server" />

  6. The focus is currently set to the ErrorMessage property, so change it to You must fill in this field; then press Tab to move the focus to the next field.

NOTE

Refer to Chapter 2 for details about how to create your own snippets.

10.4.2. Using Snippets

Web development snippets are divided into two types: ASP.NET and HTML. Most of the snippets are pretty much self-explanatory; Table 10-1 describes some of the more cryptic ones.

Table 10.1. ASP.NET Code Snippets
Shortcut/Trigger KeyNotes
checkbox 
formrServer-side form
hyperlink 
image 
label 
listbox 
listitem 
listview 
loginname 
loginstatus 
loginview 
multiview 
panel 
placeholder 
radiobutton 
registerCustom control register directive
registerascxUser control register directive
repeater 
requiredfieldvalidator 
runat 
scriptreference 
scripts 
servicereference 
sitemappath 
smScriptManager
smpScriptManagerProxy
sqldatasource 
textbox 
updatepanel 
updateprogress 
validationsummary 
view 

A number of HTML snippets are also available. I would like to draw your attention to the XHTML doctype snippets, because I believe these are particularly useful, as shown in Table 10-2.

Table 10.2. HTML Snippets
ShortcutNotes
a 
br 
class 
div 
form 
hr 
html 
html4fHTML 4.01 Frameset doctype declaration
html4sHTML 4.01 Strict doctype declaration
html4tHTML 4.01 Transitional doctype declaration
img 
input 
link 
metaie8IE compatibility flag: <meta http-equiv="X-UA-Compatible" content="IE=8" />
scriptClient script block
scriptrServer script block
scriptrefClient script reference
select 
span 
style 
table 
ul 
xhtml10fXHTML 1.0 Frameset declaration
xhtml10sXHTML 1.0 Strict document declaration
xhtml10tXHTML 1.0 Transitional document declaration
xhtml11XHTML 1.1 doctype

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.16.120