The seven tricks of the application that don't take the usual road design

Source: Internet
Author: User
Tags end net return string tostring client root directory| Program | The seven great tricks of designing applications without unusual design

With Microsoft. Net of the popular, ASP. NET is increasingly accepted by developers. As developers, we not only need to master its basic principles, but also more practice, from the practice of acquiring real development skills. In our actual development, often the basic principle can not meet the development needs, we have more to accumulate some development skills, this article to introduce some practical skills, hope for the development of everyone to benefit.

1. The usage of the ~

In general, we are using./. /Such a relative path to identify and plan our resources (such as pictures, resource files), but in this way when we deploy the application, there may be errors, The problem arises if you include a picture in a control of. ascx, and the control is referenced separately by us at different levels of the two-directory aspx file.

~/image/about.bmp is a great way to start with a Web application's root directory, which makes it more flexible and convenient than you use./image/about.bmp. The downside is that this approach is dynamically parsed at runtime, so you may not be able to preview it in IDE design mode.

2. After refreshing and submitting the page, save the location of your page scroll bar

Often there are cases where we need users to submit a form, but are there more than 500+ in the form? control or text box to fill in, that is, users need to pull the IE scroll bar to fill out, then if the user is visible ie range of 2/3, select a combo box value, Unfortunately, the combo box is server-side, then it means that the page will be submitted once, and when the user again see the refreshed page, the page is determined in 3/1 of the place is displayed at the beginning of the page, the user only drag the mouse, and then just fill in the remaining 250 controls, unfortunately, 370 controls do you want him to choose?

Use the following method to quickly identify and remember where you were before submitting.

The old Dog learns New tricks also has a similar example maintain Scroll in the any Page Element, but he uses the Web Position which means you need to use an. 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 use different styles of templates

This trick is also very practical, you can make two different templates or expressions, Saved in the form of an. ascx control, the runtime uses one of the templates dynamically according to a condition, and scottgu that the ItemDataBound method can customize the performance you display, such as highlighting an element or adding a promotional ad map, and so on.

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. Scrolling DataGrid

This is easier, sometimes your page only has a fixed place, but you need to display a lot of data, or also uncertain, but only a fixed place for you to show it. At this point you can use the following strokes, automatic scroll bar, and the application of many controls. It's easy to put your control in a div and set the overflow property to auto

<div style= "Height:400px;width:200px;overflow:auto"
<asp:datagrid id= "Mygrid" runat= "Server"/>

6. Dynamically creating controls

With the placeholder control, this stuff is used even more in 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 (New LiteralControl ("<br>"))
Next I
End Sub

7. Use of client code

1. You can use the client's event code, but the two cannot have the same name, the name of the server-side code you can control. Custom controls for standard controls that are not must implement iattributeaccessor interfaces or derive from WebControl and can be expando properties

Asp:imagebutton id= "foo"
Imageurl= "Start.jpg"
Onmouseover= "rollover (this);"
Onmouseout= "Rollout (this)"
Rolloversrc= "Myrollover.jpg"
Rolloutsrc= "Myrollout.jpg"
runat= "Server"/>

<input Type=button onclick= "return ClientHandler ()"
onserverclick= "Button1_Click" .../>

2. Use to execute client code before postback, and of course you can cancel this postback, and 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 your sure you want to submit the order? ')
End Sub

3. There are more complex I think is not practical, we can go to see, mainly using RegisterStartupScript and JavaScript technology

The above article introduced some commonly used and the more practical skill, hoped can be beneficial to everybody's actual 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: 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.