在开发Web应用程序方面,Asp.net是一个令人敬畏的框架。如果你使用过一段时间,那么这就不是什么秘密了。它提供了一些十分强大的新特征,而你只需要些少量的代码就能实现。我曾经列出一个清单,上面是一些你可以只用少量或不用任何c#/VB.net代码就能实现的非常简单(甚至很酷)的功能。如果你有其他建议,可以添加评论,如果你的建议是一件能够容易应用的任务,我将进一步更新我的清单。
1、当页面PostBacks的时候,保持滚动条的位置。
在ASP.NET 1.1中,当进行postback 操作的时候,如果想保持滚动条的位置,那真是一件痛苦的事情,特别是当页面上有一个grid(表格?)而你想编辑某一具体行的时候。页面将会重新加载,滚动条位于页面顶端,而不是你期望的位置,这样你就不得不下拉滚动条。在ASP.net2.0中,你可以简单地在Page directive这里加上MaintainScrollPostionOnPostBack 属性(来实现同样的功能)。
<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeFile="
" Inherits="
" %> 2、当页面加载的时候,控件获得默认焦点。
这是另一件很简单的事情,而不用通过写javascrip脚本。如果你的页面上只有一个(或者两个)文本输入框,用户为什么非要点击文本框之后才能开始输入呢?光标难道就不能自动位于文本框,用户可以马上输入?使用HtmlForm控件的DefaultFocus 属性,你就可以很容易地做到。
<form id="frm" DefaultFocus="txtUserName" runat="server">

</form> 
3、当用户按下Enter键的时候,设置默认触发按钮。
在ASP.NET 1.1中,这又是一件十分痛苦的事情。当用户按下Enter键的时候,你需要写一些javascript代码,来保证页面上适当的按钮触发一个服务器端“Click”事件。幸运的是,每当用户按下Enter键的时候,你现在可以使用HtmlForm的DefaultButton 属性来设置点击哪一个按钮。还有一种情况,每当user(指光标是否更合适?)进入页面上不同面板触发不同的按钮,(这个情况下),就可以设置Panel控件的DefaultButton 属性。
<form id="frm" DefaultButton="btnSubmit" runat="server">

</form> 4、容易地定位nested controls(嵌套控件?排列整齐的控件?表达不出来...呵呵~)。
在一个页面的控件层次中查找某些控件,确实是一件很头痛的事。但是如果你知道控件是如何嵌套(nest)的,你可以使用不怎么常用的快捷方式"$"来查找控件,而不用写递归代码。If you're looking for a great way to recursively find a control (in cases where you don't know the exact control nesting) check out my good buddy Michael Palermo's blog entry.(这一句是广告,不翻了~)。以下代码展示了如何使用DefaultFocus 属性来给嵌套在FormView控件里面的文本框设置焦点。注意,用“$”来划定嵌套方式(nesting):
<form id="form1" runat="server" DefaultFocus="formVw$txtName">
<div>
<asp:FormView ID="formVw" runat="server">
<ItemTemplate>
Name:
<asp:TextBox ID="txtName" runat="server"
Text='<%# Eval("FirstName") + " " + Eval("LastName") %>' />
</ItemTemplate>
</asp:FormView>
</div>
</form>
TextBox tb = this.FindControl("form1$formVw$txtName") as TextBox;
if (tb != null)
{
//Access TextBox control
}
5、Strongly-typed access to cross-page postback controls强类型方式访问跨页面PostBack控件。
这一条比其他任何一点都更加involved(“包含”?不像,应该是“不常用”的意思吧),但是十分有用。一个页面提交信息到另一个页面,在这里ASP.NET 2.0介绍了跨页面提交的概念。按钮提交数据到一个页面,把按钮的PostBackUrl属性设置为目标页面的名字,就是通过这种方式(实现跨页面提交)。
一般情况下,可以通过PreviousPage.FindControl("ControlID")方式来访问提交的数据。然而,这需要看情况(requires a cast),如果你需要访问先前页面中的属性(经常需要这么做)。如果在发起回传操作的页面后台代码中增加一个公有属性,那么你可以通过在本次回传的目标页中增加PreviousPageType directive,以强类型的方式来访问这些公有属性。如果你还没有尝试过,这听起来或许有点混淆,所以允许我多解释一些。
假如有一个页面叫做Default.aspx,同时向外提供一个公有属性,来返回页面中定义的TextBox的值。数据所要提交到的页面(姑且叫做SearchResult.aspx吧)就能够以强类型的方式访问到这些属性,只需要在SearchResult.aspx页面顶端增加PreviousPageType directive:
<%@ PreviousPageType VirtualPath="Default.aspx" %> 
通过添加这个directive,SearchResult.aspx中的代码就可以以强类型的方式访问Default.aspx中定义的TextBox。在以下的示例中,假设Default.aspx中定义的属性名是SearchTextBox.
TextBox tb = PreviousPage.SearchTextBox; 很明显,这行代码只有在上一页(Previous Page)是Default.aspx的情况下才能正常运行。同时PreviousPageType 也有一个TypeName属性,根据这个属性,你可以定义一个基类型,这样你可以让一个或者多个页面获取这个基类型的值来支持多页面。你可以从这里了解更多关于PreviousPageType。
(这段代码不好翻译,免于出错,仅给出部分意思,望达人补充,原文如下:This code obviously only works if the previous page is Default.aspx. PreviousPageType also has a TypeName property as well where you could define a base type that one or more pages derive from to make this technique work with multiple pages. You can learn more about PreviousPageType here.)
以下明天翻译,看球了...
6. Strongly-typed access to Master Pages controls: The PreviousPageType directive isn't the only one that provides strongly-typed access to controls. If you have public properties defined in a Master Page that you'd like to access in a strongly-typed manner you can add the MasterType directive into a page as shown next (note that the MasterType directive also allows a TypeName to be defined as with the PreviousPageType directive):
You can then access properties in the target master page from a content page by writing code like the following:
You can find several other tips and tricks related to working with master pages including sharing master pages across IIS virtual directories at a previous blog post I wrote.
7. Validation groups: You may have a page that has multiple controls and multiple buttons. When one of the buttons is clicked you want specific validator controls to be evaluated rather than all of the validators defined on the page. With ASP.NET 1.1 there wasn't a great way to handle this without resorting to some hack code. ASP.NET 2.0 adds a ValidationGroup property to all validator controls and buttons (Button, LinkButton, etc.) that easily solves the problem. If you have a TextBox at the top of a page that has a RequiredFieldValidator next to it and a Button control, you can fire that one validator when the button is clicked by setting the ValidationGroup property on the button and on the RequiredFieldValidator to the same value. Any other validators not in the defined ValidationGroup will be ignored when the button is clicked. Here's an example:
Search Text: <asp:TextBox ID="txtSearch" runat="server" />
<asp:RequiredFieldValidator ID="valSearch" runat="Server"
ControlToValidate="txtSearch" ValidationGroup="SearchGroup" />
<asp:Button ID="btnSearch" runat="server" Text="Search"
ValidationGroup="SearchGroup" />
....
Other controls with validators and buttons defined here
</form>
8. Finding control/variable names while typing code: This tip is a bit more related to VS.NET than to ASP.NET directly, but it's definitely helpful for those of you who remember the first few characters of control variable name (or any variable for that matter) but can't remember the complete name. It also gives me the chance to mention two great downloads from Microsoft. First the tip though. After typing the first few characters of a control/variable name, hit CTRL + SPACEBAR and VS.NET will bring up a short list of matching items. Definitely a lot easier than searching for the control/variable definition. Thanks to Darryl for the tip. For those who are interested, Microsoft made all of the VS.NET keyboard shortcuts available in a nice downloadable and printable guide. Get the C# version here and the VB.NET version here.
That's all for now. There are a lot of other things that could be mentioned and I'll try to keep this post updated. Have a great (simple) ASP.NET 2.0 tip or trick? Post the details in the comments and I'll add it if the content is appropriate for the list. Make sure to list your name so I can give proper credit.
For those who are interested, you can also view videos I've put together that show how to accomplish different tasks from working with AJAX, to ASP.NET to Web Services and WCF at the following URL:
http://weblogs.asp.net/dwahlin/archive/tags/Video/default.aspx
浙公网安备 33010602011771号