Sunday, May 27, 2007

ASP.NET 2.0 - Tips & Tricks V1.0

ASP.NET 2.0 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. 

 1.  Maintain the position of the scrollbar on postbacks:  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:  Using the DefaultFocus property of the HtmlForm control you can easily do this.

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

3. Set the default button that is triggered when the user hits the enter key:  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">

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. 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">
asp:FormView ID="formVw" runat="server">
<asp:TextBox ID="txtName" runat="server" 
Text='<%# Eval("FirstName") + " " + Eval("LastName") %>' />

This little trick can also be used on the server-side when calling FindControl(). Here's an example:

TextBox tb = this.FindControl("form1$formVw$txtName"as TextBox;
(tb != null)
//Access TextBox contro! l

5. Strongly-typed access to cross-page postback controls:  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 (say SearchResults.aspx) can access that property in a strongly-typed manner 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;

6. Strongly-typed access to Master Pages 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:

<%@ 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 "text updated using MasterType directive with VirtualPath attribute.";

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. Here's an example:

<form id="form1" runat="server">
    Search Text: <asp:TextBox ID="txtSearch" runat="server" /> 
asp:RequiredFieldValidator ID="valSearch" runat="Server" 
="txtSearch" ValidationGroup="SearchGroup" /> 

8. Finding control/variable names while typing code:  This tip is a bit more related to VS.NET than to ASP.NET directly. 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. For those who are interested, Microsoft made all of the VS.NET keyboard shortcuts available in a nice downloadable and printable guide-



Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258


DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

No comments:

Post a Comment