TA的每日心情 | 衰 2019-8-18 09:37 |
|---|
签到天数: 2 天 [LV.1]初来乍到
|
随着微软.NET的流行,ASP.NET越来越为广大开发人员所接受。作为ASP.NET的开发人员,我们不仅需要掌握其基 本的原理,更要多多实践,从实践中获取真正的开发本领。在我们的实际开发中,往往基本的原理满足不了开发需
0 S' k/ [, c2 n. [# \5 ]求,我们更多的要积累一些开发技巧,本文就向大家介绍一些实用技巧,希望对大家的开发有所裨益。
1 t2 G% t0 s: A6 G 1. ~ 的用法
( J* V. Z$ j( S1 i" M) R 一般的情况下,我们是使用./../ 这样的相对路径来确定和规划我们的资源(比如图片、资源文件),但这种方* D5 A8 i. S9 g r5 @, Q; e( r# Z
式下在我们部署应用的时候,可能会出错,另外对于.ascx的控件中如果包含了一个图片,而这个控件被我们在不
/ A% k2 R3 i" p6 u3 ~) S9 |同层次的两个目录的aspx文件分别引用时,问题就会出现了。
' L4 C7 \3 V7 ]0 M ~/image/about.bmp
% l0 @5 v0 X% F6 ?是一种非常好的方法,它以Web应用程序的根目录为起始点,这样使得比你使用./image/about.bmp这样的方式要更# D+ w7 J5 l# e1 a6 b
加灵活和方便。有一点不好,是这种方式是在ASP.NET运行时动态解析的,所以在IDE设计模式中,你可能不能预览
' `/ q7 k' r- b+ p它。1 _% h0 M4 D% C- T8 I
2. 在刷新和提交页面后,保存你的页面滚动条的位置
* P) j. }' \& C! D 经常有这样的情况,我们需要用户提交一个表单,但是表单中有超过500+个?控件或文本框要填写,也就是说
7 i; y% q8 ^8 w8 G: C( l+ H, T8 v% l用户需要拉动IE的滚动条才能够填得完,那么假如用户正在可见IE范围的2/3处,选择了一个组合框的值,很不幸# `' `; e; B4 d; ]
组合框是服务器端的,那么也就意味着页面会提交一次,而当用户再看见刷新过的页面时,页面确定在3/1的地方" [3 S$ c3 e$ p, ]
也就是显示在页面最开始的地方,用户只有拖动鼠标,然后接着刚刚的地方再填写剩下的250个控件,很不幸,370
% Q# q, g6 w- ~* }9 ]$ Y* ~个控件又需要他选择一下? 5 F5 }# |# D: I/ G" b1 @" a
用下面的方法可以很快地确定和记住你提交前的位置。
- a) P' ~* y8 _+ } 网上的Old Dog Learns New Tricks也有一个类似的例子Maintain Scroll Position in any Page Element,% ]" F7 F& b/ r
不过他使用了Web Behavior这意味着你需要使用一个.htc文件/ h! |, |, `. K3 I. a$ {0 I; s7 }1 d3 u
Private Sub RetainScrollPosition()5 p8 t4 J7 T$ _& z' c/ u
Dim saveScrollPosition As New StringBuilder
1 S+ z R7 D$ b7 V; [9 z* \9 |7 QDim setScrollPosition As New StringBuilder
3 {2 V! T* E* K3 Q& B$ ORegisterHiddenField("__SCROLLPOS", "0")
/ V O; v- O* t/ YsaveScrollPosition.Append("<script language='javascript'>")
& _+ F. R" n# h6 f [3 w; B/ n: @saveScrollPosition.Append("function saveScrollPosition() {")9 R6 Z+ L, s+ j( @* o3 r
saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = document.body.scrollTop;")
: A5 C7 @& y. N, O) L: s' J7 J% {saveScrollPosition.Append("}")! p; ^$ K2 w* F, a/ f- z% b
saveScrollPosition.Append("document.body.onscroll=saveScrollPosition;")
; E& W. K% k+ _saveScrollPosition.Append("</script>")
. w! N" w3 s" w$ pRegisterStartupScript("saveScroll", saveScrollPosition.ToString())
/ p/ W# Y( k$ ^9 p( RIf (Page.IsPostBack = True) Then
" ~: c0 I6 L4 d% V6 ?% {setScrollPosition.Append("<script language='javascript'>")
8 B. V$ z3 i [; QsetScrollPosition.Append("function setScrollPosition() {")
& X- K: {2 E4 h2 l0 P' @- S" r4 wsetScrollPosition.Append(" document.body.scrollTop = " & Request("__SCROLLPOS") & ";")9 h: T7 [( T+ f( x
setScrollPosition.Append("}")5 t& t. Q) [0 B- b: ~
setScrollPosition.Append("document.body.onload=setScrollPosition;")
/ n' C2 Q+ Z' H# C5 qsetScrollPosition.Append("</script>")' F2 z* K1 [! m8 e6 M. S
RegisterStartupScript("setScroll", setScrollPosition.ToString())5 l1 G( [( \ w. f8 e0 {& h+ q
End If
3 K4 N9 O# H* @- _! @, Z$ r8 hEnd Sub
, t2 b2 c9 w+ O; m, DPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ! B% i+ g( H' A- m: N$ K
MyBase.Load
! Q% ?. @) i+ G# W& O, f6 cRetainScrollPosition()
/ @% I: b% e( i: s9 eEnd Sub 6 }* N, P# L) |7 t R' H
7 q5 y" G2 M3 ` e! w+ |# K3. DataList使用不同风格的模板+ F {) Q7 Y( k% V' ]
这招也非常实用,你可以制作两个不同的模板或表现形式,分别以.ascx控件的形式保存,运行时根据某个条5 ?$ P; _" ?5 a+ Q9 L4 i
件动态的选择使用其中的一个模板,另外ScottGu认为ItemDataBound方法也可以定制你显示的表现,比如加亮某个, a9 X: ?' `% X- ]
元素或是加一个促销广告图等等。
3 t* c& i" T9 [ g6 n4 ~Dim theme As String
) }! j* @" M6 [4 Ftheme = DropDownList1.SelectedValue
# Z# F7 s1 D R! P( @( ^DataList1.ItemTemplate = Page.LoadTemplate(theme & ".ascx") ---Cool
. Q% q2 u0 G; v( O' F+ R! UDataList1.DataSource = DS/ e7 D4 e2 c* B: C' l! Q6 m& r
DataList1.DataBind() 3 V5 {; T8 R6 F5 I
4. 设置服务器端控件的焦点
( s4 B' d2 g$ N: k* F% lPrivate Sub SetFocus(ByVal controlToFocus As Control): D1 l1 u( \, Z* Z2 \& X
Dim scriptFunction As New StringBuilder( [# ~( o5 e6 f
Dim scriptClientId As String: l% s0 ], F5 Z4 j( U5 k3 M
scriptClientId = controlToFocus.ClientID& I( U0 c* o3 w8 X( A
scriptFunction.Append("<script language='javascript'>")* a! `+ p! k" x: p3 R& E/ h
scriptFunction.Append("document.getElementById('" & scriptClientId & "').focus();")/ j* j# Z4 p& c& k) y
scriptFunction.Append("</script>")
- N' J b# E( p2 ^1 S! nRegisterStartupScript("focus", scriptFunction.ToString())# q ]0 K3 R5 A- T4 E
End Sub5 w+ w% t+ Q0 a1 X
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles / p# A8 n! {2 e5 v: T
MyBase.Load- t; [) c4 o1 L1 m8 U7 _
If (Page.IsPostBack = False) Then
" D9 W b) F. f: O, V( u' e7 ASetFocus(TextBox1)4 n' N- N' J- T+ e* P
End If
( j1 |* b' ?) [* J- J; l+ v* o( rEnd Sub
; A! ~2 o8 `9 u1 a 5. 滚动DataGrid
0 v* Z1 R5 l% e! `6 }& D1 A 这招就更简单了,有时候你的页面只有一个固定的地方,但是需要显示非常多的数据,亦或是也不定,但是只7 H7 p1 Y2 N- @/ f3 O* B. g. \( M
有固定的一个地方给你显示它了。这时你就可以用下面这招,自动出滚动条,而且适用许多控件。很简单将你的控
: c- D( h9 f4 R6 Z1 z% \* R件放在一个DIV中将overflow属性设置成auto' y7 {0 A) V: o5 |* w
<div style=“height:400px;width:200px;overflow:auto”>
0 C" o* S8 b1 h. c8 f; a<asp:datagrid id=“MyGrid” runat=“server”/>
4 K7 a# P" k% A' V, n$ T</div> ( n; B# e6 P5 r" j, P
6. 动态创建控件
/ W# q. B3 [; X" a1 F: D1 k 利用PlaceHolder控件,这东西在ASP.NET 2.0 Mutil-View和Master Page中运用的就更加多了。
% f4 {9 }) S* @) F5 pSub Page_Load()
6 J6 X+ q; ]" O6 }! h4 w# [Dim i as Integer) U. A2 k) y% l2 J
For i=0 to 4
, j) Y) y9 k& M& TDim myUserControl as Control3 M7 a0 q& ?; l" Q
myUserControl = Page.LoadControl(“foo.ascx”)$ N' h) c( P. E( A" _, s7 n
PlaceHolder1.Controls.Add(myUserControl)
! R/ [6 m% U- w5 _PlaceHolder1.Controls.Add(New LiteralControl(“<br>”))' X `& T# |" |9 x n
Next i
2 ~2 A2 g' a0 M4 ~0 B$ S! mEnd Sub
M% n* G- ]# V, M B 7. 客户端代码的使用+ O; i( N$ y, O
1). 可以使用客户端的事件代码,但两者不能同名,服务器端代码的名是你可以控制的。对于非ASP.NET的标9 i/ E& r6 E ?9 y* T
准控件的自定义控件必须实现IAttributeAccessor接口或从WebControl派生并且可用expando属性# ]( Y4 s4 }2 w& J
asp:ImageButton id=“foo” . O- i4 m. M W$ z* r
ImageUrl=“start.jpg”
' ?! O h5 k7 x+ N1 C* }8 K* n2 BonMouseOver=“rollover(this);” # M/ G/ }' G' m$ i/ o9 \
onMouseOut=“rollout(this)”
$ y' b, {. i7 r+ Erolloversrc=“myrollover.jpg”8 j6 f0 v( @' C2 Z/ P/ v
rolloutsrc=“myrollout.jpg”! P( U) Q8 t* Z1 @' y
runat=“server”/>$ D( T" q5 G% g
<input type=Button onClick=“return clientHandler()” + r3 r2 [3 w! R7 I. V1 T7 R
onServerClick=“Button1_Click” … /> & w I$ e" d( r; X, I5 n( I- l! d
2). 使用可以在Postback之前执行客户端代码,当然也可以取消这次Postback,另外也可以访问客户端该页所
" f% _& F& H' e# D1 j q) Z有的客户端控件。
$ {1 J2 m+ F5 y$ p; IPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
& K; k2 q' _3 `( L: ~MyBase.Load9 v+ c7 K4 {' J5 T
RegisterOnSubmitStatement("foo", "return confirm('Are you sure you want to submit the order?');")
* V9 H# Z. D: P3 t3 zEnd Sub
1 x8 Z; F0 c3 ^& F+ ? 3). 还有更复杂的我认为不实用,大家可以自己去看,主要是运用RegisterStartupScript和JavaScript的技术 |
|