设置服务器端控件的焦点 :
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
客户端代码的使用
1). 可以使用客户端的事件代码,但两者不能同名,服务器端代码的名是你可以控制的。对于非ASP.NET的标准控件的自定义控件必须实现IAttributeAcces-sor接口或从WebControl派生并且可用expando属性
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). 使用可以在Postback之前执行客户端代码,当然也可以取消这次Postback,另外也可以访问客户端该页所有的客户端控件。
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). 还有更复杂的我认为不实用,大家可以自己去看,主要是运用Register-StartupScript和JavaScript的技术
posted on 2006-02-21 17:19
萌芽的叶子 阅读(397)
评论(0) 编辑 收藏 引用 所属分类:
asp.net