Simple ASP. NET 2.0 Tips and Tricks that You May (or may not) have Heard About [from dwahlin]

Source: Internet
Author: User
Tags access properties

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
Start a list of some of the most simple (yet cool) things you coshould do with it
That required little or no C #/VB. NET code. If you have other suggestions add
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
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 wocould reload and you 'd be placed back at the top
And have to scroll down. In ASP. NET 2.0 you can simply add
MaintainScrollPostionOnPostBack attribute to the Page direve ve:

<% @ 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
Writing JavaScript. If you only have a single textbox (or two) on a page why
Shocould the user have to click in the textbox to start typing? Shouldn't
Cursor already be blinking in the textbox so they can type away? Using
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
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
Server-side. Fortunately, you can now use the HtmlForm control's DefaultButton
Property to set which button shocould be clicked when the user hits enter. This
Property is also available on the Panel control in cases where different buttons
Shocould 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 "$" cannot cut 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 "$" is used to delimit
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. Stronugly-typed access to cross-page postback
Controls:
This one is a little more involved than the others,
Quite useful. ASP. NET 2.0 introduced the concept of cross-page postbacks where
One page cocould 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
Button shoshould postback data to. Normally, the posted data can be accessed
Doing something like PreviousPage. FindControl ("ControlID"). However, this
Requires a cast if you need to access properties of the target control in
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 stronugly-typed manner by adding the PreviousPageType ctive
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
(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
TextBox defined in Default. aspx in a stronugly-typed manner.
Following example assumes the property defined in Default. aspx is named
SearchTextBox.

TextBox tb = PreviousPage. SearchTextBox;

This code obviusly only works if the previous page is Default. aspx.
PreviousPageType also has a TypeName property as well where you cocould define
Base type that one or more pages derive from to make this technique work
Multiple pages. You can learn more about PreviousPageType here.

6. stronugly-typed access to Master Pages controls:The
PreviousPageType direve isn't the only one that provides stronugly-typed
Access to controls. If you have public properties defined in a Master Page that
You 'd like to access in a stronugly-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 direve ):

<% @ 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 guest ss 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
Control variable name (or any variable for that matter) but can't remember
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
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
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
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 cocould 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.