5、Strongly-typed access to cross-page postback controls。(使用强类型方式访问跨页面提交控件?)
这一条比其他任何一点都更加involved(包含?不像,应该是不常用的意思吧),但是十分有用。一个页面提交信息到另一个页面,在这里ASP.NET2.0介绍了跨页面提交的概念。按钮提交数据到一个页面,把按钮的PostBackUrl属性设置为目标页面的名字,就是通过这种方式(实现跨页面提交)。
(不好意思啊,英超比赛开始了,不得不停下来看球了,剩下的内容以原文贴出,明天再译吧~~)
Normally,the posted data can be accessed by doing something likePreviousPage.FindControl("ControlID"). However, this requires a castif you need to access properties of the target control in the previouspage (which you normally need to do). If you add a public propertyinto the code-behind page that initiates the postback operation, youcan access the property in a strongly-typed manner by adding thePreviousPageType directive into the target page of the postback. Thatmay sound a little confusing if you haven‘t done it so let me explain alittle more.
If you have a page called Default.aspx that exposes a publicproperty that returns a Textbox that is defined in the page, the pagethat data is posted to (lets call it SearchResults.aspx) can accessthat property in a strongly-typed manner (no FindControl() call isnecessary) by adding the PreviousPageType directive into the top of thepage:
By adding this directive, the code in SearchResults.aspx can accessthe TextBox defined in Default.aspx in a strongly-typed manner. Thefollowing example assumes the property defined in Default.aspx is namedSearchTextBox.
This code obviously only works if the previous page isDefault.aspx. PreviousPageType also has a TypeName property as wellwhere you could define a base type that one or more pages derive fromto 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 providesstrongly-typed access to controls. If you have public propertiesdefined in a Master Page that you‘d like to access in a strongly-typedmanner you can add the MasterType directive into a page as shown next(note that the MasterType directive also allows a TypeName to bedefined 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 withmaster pages including sharing master pages across IIS virtualdirectories at a previous blog post I wrote.
7. Validation groups: You may have a page that hasmultiple controls and multiple buttons. When one of the buttons isclicked you want specific validator controls to be evaluated ratherthan all of the validators defined on the page. With ASP.NET 1.1 therewasn‘t a great way to handle this without resorting to some hack code. ASP.NET 2.0 adds a ValidationGroup property to all validator controlsand buttons (Button, LinkButton, etc.) that easily solves theproblem. If you have a TextBox at the top of a page that has aRequiredFieldValidator next to it and a Button control, you can firethat one validator when the button is clicked by setting theValidationGroup property on the button and on theRequiredFieldValidator to the same value. Any other validators not inthe defined ValidationGroup will be ignored when the button is clicked.Here‘s an example:
8. Finding control/variable names while typing code: This tip is a bit more related to VS.NET than to ASP.NET directly, butit‘s definitely helpful for those of you who remember the first fewcharacters of control variable name (or any variable for that matter)but can‘t remember the complete name. It also gives me the chance tomention two great downloads from Microsoft. First the tip though. After typing the first few characters of a control/variable name, hitCTRL + SPACEBAR and VS.NET will bring up a short list of matchingitems. Definitely a lot easier than searching for the control/variabledefinition. Thanks to Darryl for the tip. For those who areinterested, Microsoft made all of the VS.NET keyboard shortcutsavailable 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 bementioned and I‘ll try to keep this post updated. Have agreat (simple) ASP.NET 2.0 tip or trick? Post the details in thecomments 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 puttogether that show how to accomplish different tasks from working withAJAX, to ASP.NET to Web Services and WCF at the following URL:
http://weblogs.asp.net/dwahlin/archive/tags/Video/default.aspx
联系客服