📄 asp2.htm
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>ASP.NET编程中的十大技巧 - 网易学院·教程</title><meta content="ASP.NET" name="keywords" /><meta content="ASP.NET,ASP.NET编程中的十大技巧" name="description" /><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><base target="_blank" /><link href="http://cimg.163.com/gzit/netSchool/Indeximg/school.css" rel="stylesheet" type="text/css" /></head><body><style type=text/css><!--body {text-align: center}td,select,input,.p1{font-size:12px;}a.nav:link,a.nav:visited{color:#000000;text-decoration:none;line-height:150%;}a.nav:hover{color:#ff0000;text-decoration:none;line-height:150%;}.line{color:#afafaf}--></style><table width="750" border="0" cellpadding="2" cellspacing="0"><FORM NAME=loginvip METHOD=post ACTION=http://reg.163.com/CheckUser.jsp> <tr align="center"> <td STYLE="border-bottom:black 1px solid"> <a href="http"://reg.163.com/>通行证</a>:用户名 <input name=username size=10 class=log> 密码 <input name=password type=password size=10 class=log> <input name=login type=submit class=sinput value=登录> </td> <td STYLE="border-bottom:black 1px solid"><a href="http://mail.163.com/"><A class="163s" href="http://www.163.com/">首页</A> - <A class="163s" href="http://news.163.com/">新闻</A> - <A class="163s" href="http://www.126.com/">免费邮箱</A> - <A class="163s" href="http://sms.163.com/">短信</A> - <A class="163s" href="http://popo.163.com/">POPO</A> - <A class="163s" href="http://photo.163.com/">相册</A> - <A class="163s" href="http://so.163.com/">搜索</A> - <A class="163s" href="http://love.163.com/">交友</A> - <A class="163s" href="http://adfarm.mediaplex.com/ad/ck/4080-25838-9511-0">拍卖</A></td></tr></form></table><table width="750" align="center" border="0" cellpadding="2" cellspacing="0" style="margin-bottom:3px;"> <tr> <td width="16%" height="50" align="center"><a href="http://www.163.com"><img src="http://www.163.com/images/neteaselogo.gif" alt="NetEase" border="0"></a><br /></td> <td width="83%" align="center" valign="bottom" class="line"><a href="http://news.163.com" class="nav">新闻</a> | <a href="http://sports.163.com/" class="nav">体育 </a>| <a href="http://tech.163.com/" class="nav" style="color:#F00; font-weight:bold;">科技</a> | <a href="http://stock.163.com/" class="nav">财经 </a>| <a href="http://fund.163.com/" class="nav">基金</a> | <a href="http://gp.stock.163.com/" class="nav">证券</a> | <a href="http://travel.163.com" class="nav">旅游</a> | <a href="http://lady.163.com/" class="nav">女性 </a>| <a href="http://culture.163.com/" class="nav">文化</a> | <a href="http://game.163.com/" class="nav">游戏 </a>| <a href="http://mobile.163.com/" class="nav">手机 </a>| <a href="http://dl.163.com/" class="nav">下载 </a>| <a href="http://gz.163.com/" class="nav">广东 </a>| <a href="http://news.163.com/rank" class="nav">排行 </a>| <a href="http://bulo.163.com/" class="nav" style="color:#F00;">部落</a><br /> <a href="http://news.163.com/war" class="nav">军事 </a>| <a href="http://ent.163.com/" class="nav">娱乐</a> | <a href="http://biz.163.com" class="nav">商业</a> | <a href="http://auto.163.com/" class="nav">汽车</a> | <a href="http://soufun.163.com" class="nav">房产 </a>| <a href="http://digi.163.com/" class="nav">数码</a> | <a href="http://life.163.com/" class="nav">生活 </a>| <a href="http://health.163.com/" class="nav">健康</a> | <a href="http://education.163.com/" class="nav">教育 </a>| <a href="http://abroad.163.com/" class="nav">出国</a> | <a href="http://ent.163.com/ent/level1/movie.html" class="nav">电影 </a>| <a href="http://adfarm.mediaplex.com/ad/ck/4080-25846-9511-18" class="nav">拍卖</a> | <a href="http://sh.163.com/" class="nav">上海 </a>| <a href="http://club.163.com" class="nav" style="color:#F00;">社区</a> | <a href="http://bbs.163.com" class="nav">论坛</a></td> </tr></table><table width="750" border="0" cellspacing="0" cellpadding="0" id="TechGuide">
<tr>
<td width="42" class="importantGuide"><a href="http://tech.163.com/">首页</a></td>
<td width="42" class="importantGuide"><a href="http://tech.163.com/discover">科学</a></td>
<td width="62" class="importantGuide"><a href="http://tech.163.com/school" style="color:#F00;">网易学院</a></td>
<td width="42" class="importantGuide"><a href="http://dl.163.com">下载</a></td>
<td width="42" class="importantGuide"><a href="http://tech.163.com/digi">数码</a></td>
<td width="42" class="importantGuide"><a href="http://tech.163.com/production">产品</a></td>
<td> <a href="/special/D/000915HQ/DigiTv.html">数字电视</a> | <a href="/special/p/000915EA/pinglun.html"> 评论</a> | <A href="http://product.tech.163.com">报价</A> | <a href="/cio">信息化</a> | <a href="/special/b/000915DJ/bingduzhensuo.html">杀毒</a> | <a href="/special/z/000915I8/zhuantizh.html">专题</a> | <a href="/special/n/000915I8/newslook.html">新闻见证人</a> | <a href="/special/t/000915I8/techmen.html">访谈</a> | <a href="http://bbs.tech.163.com/index.htm">论坛</a></td>
</tr>
</table><TABLE cellSpacing=0 cellPadding=0 width=750 align=center border=0> <TBODY> <TR> <TD><iframe src="http://adclient.163.com/html.ng/site=netease&affiliate=tech&cat=article&type=column&location=1" width="750" height="70" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></TD> </TR> <TR> <TD height=3></TD></TR></TBODY></TABLE><table width="750" border="0" cellspacing="0" cellpadding="0" id="nav"> <tr> <td style="width:131px;" rowspan="2"><a href="http://tech.163.com/school/" target="_self"><img src="http://cimg.163.com/gzit/netSchool/Indeximg/LogoB.gif" alt="网易学院·教程"/></a></td> <td class="nav1">·<a href="/special/t/000915AD/tools.html" target="_self">工具软件</a>·<a href="/special/o/000915A6/officetool.html" target="_self">办公软件</a>·<a href="http://tech.163.com/special/O/000915A0/OS.html" target="_self">操作系统</a>·<a href="/special/p/0009158A/photos.html" target="_self" target="_self">图形图像</a>·<a href="/special/w/0009158K/website.html" target="_self">网站建设</a>·<a href="/special/d/0009159F/developer.html" target="_self">编程开发</a>·<a href="/special/m/0009158R/multimedia.html" target="_self">多媒体</a>·<a href="/special/n/000917GE/net.html" target="_self">网络</a>·<a href=/special/d/000917GF/du.html target="_self">病毒</a></td> </tr> <tr> <td class="nav2">·<a href="/school" target="_self">学院首页</a>·<a href="http://tech.163.com/special/s/000915AS/softnews.html" target="_self">软件新闻</a>·<a href="/special/A/00091593/Arts1.html" target="_self">艺术设计</a>·<a href="/special/S/000915AH/S_InterView.html" target="_self">教授视点</a>·<a href="/special/A/000915AH/AllPro1.html" target="_self">教授名录</a>·<a href="/special/s/00091589/schoolroll.html" target="_self">滚动</a>·<a href="/special/S/000915SN/S_Special.html" target="_self">专题</a>·<a href="http://pub.club.163.com/sub/bmlb/faq_00.htm">论坛</a></td> </tr></table><div id="MyPosition"> 你现在的位置是: <a href="http://www.163.com">网易</a> > <a href="/">网易科技</a> > <a href="/school">网易学院·教程</a> </div><div id="TextTitle"> <h1>ASP.NET编程中的十大技巧</h1> http://tech.163.com/school · 2005-03-21 11:08:20 · 来源: 网易社区</div><div id="TextContent"><p> <select onChange="javascript:top.location.href=this.value" id="navgg"> <option value="http://tech.163.com/05/0321/11/1FC5MH7O00091589.html">第1页:ASP.NET编程中的十大技巧</option> <option value="http://tech.163.com/05/0321/11/1FC5MH7O00091589_2.html">第2页:ASP.NET编程中的十大技巧</option> </select> <script>for (var i=0;i<document.getElementById("navgg").options.length;i++){if (document.getElementById("navgg").options[i].value==document.location.href){ document.getElementById("navgg").options[i].selected = true; break;}} </script></p>
<P style="TEXT-INDENT: 2em"> </P>
<P style="TEXT-INDENT: 2em"><STRONG>5、只在必要时使用服务器端控件</STRONG> </P>
<P style="TEXT-INDENT: 2em">ASP.NET中新引入了一种在服务器端运行的被称作Web Server Controls的控件,在代码中,它们经常通过下面的语法被说明: </P>
<P style="TEXT-INDENT: 2em">&lt;asp:TextBox id="txtLastName" size="40" runat="server" /&gt; </P>
<P style="TEXT-INDENT: 2em">它们有时也被称作ASP控件。服务器端控件是由runat属性指示的,它的值总是“server”。 </P>
<P style="TEXT-INDENT: 2em">通过添加runat属性,一般的HTML控件可以被很方便地转换到服务器端运行,下面是一个简单的例子: </P>
<P style="TEXT-INDENT: 2em">&lt;input type="text" id="txtLastName" size="40" runat="server" /&gt; </P>
<P style="TEXT-INDENT: 2em">可以通过id属性中指定的名字,我们可以引用程序中的控件,可以通过编程的方式设置属性和获得值,因此,服务器端处理方式有较大的灵活性。 </P>
<P style="TEXT-INDENT: 2em">这种灵活性是有一定代价的。每种服务器端控件都会消耗服务器上的资源。另外,除非控件、网页或应用程序明确地禁止view state,控件的状态是包含在view state的隐藏域中,并在每次回送中都会被传递,这会引起严重的性能下降。 </P>
<P style="TEXT-INDENT: 2em">在这方面的一个很好的例子是,网页上控件表格的应用,如果不需要在代码中引用表格中的元素,则使用无需进行服务器端处理的HTML表格。我们仍然可以在HTML表格单元中放置服务器控件,并在代码中引用服务器控件。如果需要引用任意的表格元素,例如指定的单元,则整个表格必须是服务器控件。 </P>
<P style="TEXT-INDENT: 2em"><STRONG>6. HyperLink控件、LinkButton控件的差别</STRONG> </P>
<P style="TEXT-INDENT: 2em">对于Web访问者而言,HyperLink、LinkButton控件是一样的,但它们在功能方面仍然有较大的差异。 </P>
<P style="TEXT-INDENT: 2em">当用户点击控件时,HyperLink控件会立即将用户“导航”到目标URL,表件不会回送到服务器上。LinkButton控件则首先将表件发回到服务器,然后将用户导航到目标URL。如果在“到达”目标URL之前需要进行服务器端处理,则使用LinkButton控件;如果无需进行服务器端处理,则可以使用HyperLink控件。 </P>
<P style="TEXT-INDENT: 2em"><STRONG>7、注释代码</STRONG> </P>
<P style="TEXT-INDENT: 2em">这一技巧并不是针对ASP.NET的,但它是一个良好的编程习惯。 </P>
<P style="TEXT-INDENT: 2em">注释不仅仅应当说明代码会执行什么操作,还应当注明原因。例如,不要仅仅在注释中说明是在遍历数组,而是要说明遍历数组是根据某一算法计算一个值,除非算法是相当简单的,否则还应当对算法进行简要的说明。 </P>
<P style="TEXT-INDENT: 2em">.NET工程中的不同的编程语言都有各自不同的注释符号,下面是一个简要的说明: </P>
<P style="TEXT-INDENT: 2em">HTML &lt;!-- 注释 --&gt; </P>
<P style="TEXT-INDENT: 2em">JavaScript // 注释 </P>
<P style="TEXT-INDENT: 2em">VBScript ' 注释 </P>
<P style="TEXT-INDENT: 2em">VB.NET ' 注释 </P>
<P style="TEXT-INDENT: 2em">C# // 注释 </P>
<P style="TEXT-INDENT: 2em">/* 多行内容 </P>
<P style="TEXT-INDENT: 2em">的注释 </P>
<P style="TEXT-INDENT: 2em">*/ </P>
<P style="TEXT-INDENT: 2em">SQL -- 注释 </P>
<P style="TEXT-INDENT: 2em">在服务器控件的开始和结束标记中没有注释符号,但服务器能够忽略掉所有它不能识别的属性,因此我们能够通过使用没有定义的属性来插入注释。下面是一个例子: </P>
<P style="TEXT-INDENT: 2em">&lt;asp:TextBox </P>
<P style="TEXT-INDENT: 2em">id="txtLastName" </P>
<P style="TEXT-INDENT: 2em">size="40" </P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -