ASP. NET application development

Source: Internet
Author: User
With the popularity of Microsoft. NET, ASP. NET is increasingly accepted by developers. As a developer of ASP. NET, we not only need to master its basics

This principle requires more practice and practical development skills. In our actual development, the basic principle is often unable to meet the development requirements.

Please, we need to accumulate more development skills. This article will introduce some practical skills to you, hoping to benefit your development.

1 .~ Usage

In general, we use relative paths such as ../to determine and plan our resources (such as comparison files and resource files ).

When we deploy the application, an error may occur. In addition, if the. ascx control contains an image, this control is not

When the aspx files of the two directories at the same level are referenced separately, the problem occurs.

~ /Image/about.bmp

Is a very good method. It takes the root directory of the Web application as the starting point, so that it is more effective than using./image/about.bmp.

Added flexibility and convenience. This method is used for dynamic parsing during ASP. NET runtime. Therefore, you may not be able to preview it in IDE design mode.

It.

2. After the page is refreshed and submitted, save the position of the page scroll bar.

This is often the case where you need to submit more than 500 forms? Enter the control or text box, that is

The user needs to pull the IE scroll bar to fill it out. If the user is visible at 2/3 of the IE range and selects a value for the combo box, unfortunately

The combo box is on the server side, which means that the page will be submitted once. When the user sees the refreshed page again, the page will be located at 3/1.

That is to say, at the beginning of the page, the user only needs to drag the mouse, and then fill in the remaining 250 controls in the last place. Unfortunately, 370

Controls need to be selected again?

The following method can be used to quickly determine and remember the location before your submission.

The old dog learns new tricks on the Internet also has a similar example maintain scroll position in any page element,

But he uses Web behavior, which means you need to use a. HTC file.

Private sub retainscrollposition ()

Dim savescrollposition as new stringbuilder
Dim setscrollposition as new stringbuilder

Registerhiddenfield ("_ scrollpos", "0 ")
Savescrollposition. append ("<script language = 'javascript '> ")
Savescrollposition. append ("function savescrollposition (){")
Savescrollposition. append ("document. Forms [0]. _ scrollpos. value = Document. Body. scrolltop ;")
Savescrollposition. append ("}")
Savescrollposition. append ("document. Body. onscroll = savescrollposition ;")
Savescrollposition. append ("</SCRIPT> ")

Registerstartupscript ("savescroll", savescrollposition. tostring ())

If (page. ispostback = true) then

Setscrollposition. append ("<script language = 'javascript '> ")
Setscrollposition. append ("function setscrollposition (){")
Setscrollposition. append ("document. Body. scrolltop =" & request ("_ scrollpos ")&";")
Setscrollposition. append ("}")
Setscrollposition. append ("document. Body. onload = setscrollposition ;")
Setscrollposition. append ("</SCRIPT> ")
Registerstartupscript ("setscroll", setscrollposition. tostring ())

End if

End sub

Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles

Mybase. Load
Retainscrollposition ()
End sub

3. datalist uses templates of different styles

This method is also very practical. You can create two different templates or representations to save them in the form of a. ascx control.

In addition, scottgu believes that the itemdatabound method can also customize your display performance, such as adding

Element or add a promotion advertisement diagram.

Dim theme as string
Theme = dropdownlist1.selectedvalue

Datalist1.itemtemplate = page. loadtemplate (Theme & ". ascx") --- cool
Datalist1.datasource = DS
Datalist1.databind ()

4. Set the focus of the server-side control

Private sub setfocus (byval controltofocus as control)
Dim scriptfunction as new stringbuilder
Dim scriptclientid as string

Scriptclientid = controltofocus. clientid
Scriptfunction. append ("<script language = 'javascript '> ")
Scriptfunction. append ("document. getelementbyid ('" & scriptclientid & "'). Focus ();")
Scriptfunction. append ("</SCRIPT> ")
Registerstartupscript ("Focus", scriptfunction. tostring ())
End sub

Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles

Mybase. Load
If (page. ispostback = false) then
Setfocus (textbox1)
End if
End sub

5. Scroll the DataGrid

This is simpler. Sometimes your page only has a fixed place, but you need to display a lot of data, or you may not, but only

There is a fixed place to show it to you. In this case, you can use the following method to automatically export the scroll bar and use many controls. Easy to control

Set the overflow attribute to auto in a div.

<Div style = "height: 400px; width: 200px; overflow: Auto">
<Asp: DataGrid id = "mygrid" runat = "server"/>
</Div>

6. dynamically create controls

The placeholder control is used in ASP. NET 2.0 mutil-view and master page.

Sub page_load ()
Dim I as integer
For I = 0 to 4
Dim myusercontrol as control
Myusercontrol = page. loadcontrol ("foo. ascx ")
Placeholder1.controls. Add (myusercontrol)
Placeholder1.controls. Add (New literalcontrol ("<br> "))
Next I
End sub

7. Use of client code

1) You can use the Event code of the client, but the two cannot have the same name. You can control the name of the server code. For non-ASP. NET labels

The custom control of the quasi-control must implement the iattributeaccessor interface or derive from webcontrol and be available with the expando attribute.

ASP: imagebutton id = "foo"
Imageurl1_1_start.jpg"
Onmouseover = "rolover (this );"
Onmouseout = "rolout (this )"
Roloversrc##myrolover.jpg"
Roloutsrc?“myrolout.jpg"
Runat = "server"/>

<Input type = button onclick = "Return clienthandler ()"
Onserverclick = "button#click "... />

2). You can execute the client code before the PostBack operation, or cancel the PostBack operation. You can also access this page of the client.

Some client controls.

Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles

Mybase. Load
Registeronsubmitstatement ("foo", "Return confirm ('Are you sure you want to submit the order? ');")
End sub

3) There are more complex things I think are not practical. You can check them by yourself, mainly using registerstartupscript and JavaScript techniques.

Techniques

The above article introduces some common and practical skills in ASP. NET, hoping to benefit your practical development!

 

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.