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 the basic principles of ASP. NET, but also need to practice more and get real development skills from practice. In our actual development, the basic principle is often unable to meet the development requirements. We need to accumulate more development skills. This article will introduce some practical skills to you, I hope it will be helpful for everyone's development.
.~ Usage
In general, we use. /.. /This relative path is used to determine and plan our resources (such as slice and resource files). However, in this way, errors may occur when we deploy the application. if an image is included in the ascx control and the control is referenced by the aspx files in two directories at different levels, the problem will occur.
~ /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 better than you. /image/about.bmp is more flexible and convenient. This method is used for dynamic parsing during ASP. NET runtime, so you may not be able to preview it in IDE design mode.
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? The control or text box must be filled in. That is to say, the user needs to pull the IE scroll bar to complete the filling. If the user is visible at 2/3 of the IE range, the value of a combo box is selected, 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 is determined to be in the place where 3/1 is displayed at the beginning of the page. The user only needs to drag the mouse and then fill in the remaining 250 controls in the place just now. Unfortunately, which of the following controls needs to be selected?
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 it 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 = thebody. scrolltop ;") Savescrollposition. append ("}") Savescrollposition. append ("thebody. 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 ("thebody. scrolltop =" & request ("_ scrollpos ")&";") Setscrollposition. append ("}") Setscrollposition. append ("thebody. 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 is also very practical. You can create two different templates or representations, respectively. the ascx control is saved in the form. A template is selected dynamically based on a condition during the runtime. In addition, scottgu believes that the itemdatabound method can also customize the display performance, for example, highlight an element or add a promotion advertisement diagram.
Dim theme as string Theme = dropdownlist1.selectedvalueDatalist1.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 stringScriptclientid = 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 method is simpler. Sometimes your page has only one fixed place, but you need to display a lot of data, or you may not, but there is only one fixed point to show you. In this case, you can use the following method to automatically export the scroll bar and use many controls. Put your control in a div and set the overflow attribute to auto
<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 custom controls of standard controls other than ASP. NET, you must implement the iattributeaccessor interface or derive from webcontrol and use 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 all client controls on the client page.
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 technologies.
The above article introduces some common and practical skills in ASP. NET, hoping to benefit your practical development!
[Transferred from sohuit]