Turn: simple ASP. NET 2.0 tips and tricks that you may (or may not) have heard about (some simple ones that you may already know or do not know

Source: Internet
Author: User
Tags access properties
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 coshould 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 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 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 shocould 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 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 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. Stronugly-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 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 the button shoshould PostBack data. 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 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 to (Lets call it searchresults. aspx) can access that property in a stronugly-typed manner (no findcontrol () call is necessary) by adding the previouspagetype direve 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 stronugly-typed manner. the 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 coshould 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. 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 using 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. 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. 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. 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 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.