惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 含羞草

Jenkins的pipeline脚本中获取git代码变更用户名和email - 含羞草 CentOS7安装配置SonarQube - 含羞草 CentOS安装Harbor Gitlab自动触发Jenkins构建项目 - 含羞草 CentOS7安装Jenkins Master - 含羞草 解决win7下打开Excel2007,报“向程序发送命令时出现问题”的错误 - 含羞草 转:老调重弹:const char*, char const* and char *const WebService:System.InvalidOperationException: 无法生成临时类 PowerBuilder:表中字段的数据长度的修改对数据窗口的影响 转:使用SET NOCOUNT优化存储过程 转:如何使用SQL Server 2005 专用管理员连接(DAC)登录到服务器 四遥量 - 含羞草 VS.NET开发环境的WIN FORM中怎样加入ActiveX控件 - 含羞草 C# Winform程序如何获取运行路径 - 含羞草 转:类型"string"的值无法转换为"System.Drawing.Color" - 含羞草 Asp.net : 访问嵌套模版页中的控件 SQL Server 2005 : 服务器不允许远程客户端登录的解决办法 SQL Server 2005 : 清空数据库日志 安装删除服务
转: Simple ASP.NET 2.0 Tips and Tricks that You May (or may not) have Heard About (一些简单的、你可能已经知道或者不知道的ASP.NET 2.0技巧)
含羞草 · 2007-04-30 · via 博客园 - 含羞草

http://weblogs.asp.net/dwahlin/archive/2007/04/17/simple-asp-net-2-0-tips-and-tricks-that-you-may-or-may-not-have-heard-about.aspx
================

ASP.NET 2.0 is an awesome framework for developing Web applications.  If you've worked with it for awhile then that's no secret.  It offers some great new features that you can implement with a minimal amount of code.  I wanted to start a list of some of the most simple (yet cool) things you could do with it that required little or no C#/VB.NET code.  If you have other suggestions add a comment and I'll update the list if the suggestion is a simple task that can be applied easily.

 1.  Maintain the position of the scrollbar on postbacks:  In ASP.NET 1.1 it was a pain to maintain the position of the scrollbar when doing a postback operation.  This was especially true when you had a grid on the page and went to edit a specific row.  Instead of staying on the desired row, the page would reload and you'd be placed back at the top and have to scroll down.  In ASP.NET 2.0 you can simply add the MaintainScrollPostionOnPostBack attribute to the Page directive:

<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeFile="..." Inherits="..." %>

2.  Set the default focus to a control when the page loads:  This is another extremely simple thing that can be done without resorting to writing JavaScript.  If you only have a single textbox (or two) on a page why should the user have to click in the textbox to start typing?  Shouldn't the cursor already be blinking in the textbox so they can type away?  Using the DefaultFocus property of the HtmlForm control you can easily do this.

<form id="frm" DefaultFocus="txtUserName" runat="server">
  ...
</form>

3. Set the default button that is triggered when the user hits the enter key:  This was a major pain point in ASP.NET 1.1 and required some JavaScript to be written to ensure that when the user hit the enter key that the appropriate button on the form triggered a "click" event on the server-side.  Fortunately, you can now use the HtmlForm control's DefaultButton property to set which button should be clicked when the user hits enter.  This property is also available on the Panel control in cases where different buttons should be triggered as a user moves into different Panels on a page.

<form id="frm" DefaultButton="btnSubmit" runat="server">
  ...
</form>

4. Locate nested controls easily: Finding controls within a Page's control hierarchy can be painful but if you know how the controls are nested you can use the lesser known "$" shortcut to find controls without having to write recursive code.  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. The following example shows how to use the DefaultFocus property to set the focus on a textbox that is nested inside of a FormView control.  Notice that the "$" is used to delimit the 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>

This little trick can also be used on the server-side when calling FindControl().  I blogged about this awhile back if you'd like more details.  Here's an example:

TextBox tb = this.FindControl("form1$formVw$txtName"as TextBox;
if 
(tb != null)
{
    
//Access TextBox control
}

5. Strongly-typed access to cross-page postback controls:  This one is a little more involved than the others, but quite useful.  ASP.NET 2.0 introduced the concept of cross-page postbacks where one page could postback information to a page other than itself.  This is done by setting the PostBackUrl property of a button to the name of the page that the button should postback data to.  Normally, the posted data can be accessed by doing something like PreviousPage.FindControl("ControlID").  However, this requires a cast if you need to access properties of the target control in the previous page (which you normally need to do).  If you add a public property into the code-behind page that initiates the postback operation, you can access the property in a strongly-typed manner by adding the PreviousPageType directive into the target page of the postback.  That may sound a little confusing if you haven't done it so let me explain a little more.

If you have a page called Default.aspx that exposes a public property that returns a Textbox that is defined in the page, the page that data is posted to (lets call it SearchResults.aspx) can access that property in a strongly-typed manner (no FindControl() call is necessary) by adding the PreviousPageType directive into the top of the page:

<%@ PreviousPageType VirtualPath="Default.aspx" %>

By adding this directive, the code in SearchResults.aspx can access the TextBox defined in Default.aspx in a strongly-typed manner.  The following example assumes the property defined in Default.aspx is named SearchTextBox.

TextBox tb PreviousPage.SearchTextBox;

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):

<%@ MasterType VirtualPath="MasterPage.master" %>

You can then access properties in the target master page from a content page by writing code like the following:

this.Master.HeaderText "Label updated using MasterType directive with VirtualPath attribute.";

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:

<form id="form1" runat="server">

    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