本文发表在 rolia.net 枫下论坛FYI:
1. http://www.metabuilders.com/Tools/DefaultButtons.aspx
2. Add this sub to your page:
Public Sub DefaultButton(ByRef Page As System.Web.UI.Page, ByRef objTextControl As TextBox, ByRef objDefaultButton As Button)
' Sets default buttons.
' Created by Janus Kamp Hansen - http://www.kamp-hansen.dk
Dim sScript As New System.Text.StringBuilder()
sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)
sScript.Append("function fnTrapKD(btn){" & vbCrLf)
sScript.Append(" if (document.all){" & vbCrLf)
sScript.Append(" if (event.keyCode == 13)" & vbCrLf)
sScript.Append(" { " & vbCrLf)
sScript.Append(" event.returnValue=false;" & vbCrLf)
sScript.Append(" event.cancel = true;" & vbCrLf)
sScript.Append(" btn.click();" & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append("}" & vbCrLf)
sScript.Append("</SCRIPT>" & vbCrLf)
objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ClientID & ")")
Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString)
End Sub
Then call the sub in your page load with:
' Causes [Enter] key to "press" cmdButton when txtBox has focus
' The 'Me' should be what page you are dealing with. You can use 'Me'
' unless you are dealing with includes, .ascx files, etc.
DefaultButton(Me, txtBox, cmdButton)更多精彩文章及讨论,请光临枫下论坛 rolia.net
1. http://www.metabuilders.com/Tools/DefaultButtons.aspx
2. Add this sub to your page:
Public Sub DefaultButton(ByRef Page As System.Web.UI.Page, ByRef objTextControl As TextBox, ByRef objDefaultButton As Button)
' Sets default buttons.
' Created by Janus Kamp Hansen - http://www.kamp-hansen.dk
Dim sScript As New System.Text.StringBuilder()
sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)
sScript.Append("function fnTrapKD(btn){" & vbCrLf)
sScript.Append(" if (document.all){" & vbCrLf)
sScript.Append(" if (event.keyCode == 13)" & vbCrLf)
sScript.Append(" { " & vbCrLf)
sScript.Append(" event.returnValue=false;" & vbCrLf)
sScript.Append(" event.cancel = true;" & vbCrLf)
sScript.Append(" btn.click();" & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append("}" & vbCrLf)
sScript.Append("</SCRIPT>" & vbCrLf)
objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ClientID & ")")
Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString)
End Sub
Then call the sub in your page load with:
' Causes [Enter] key to "press" cmdButton when txtBox has focus
' The 'Me' should be what page you are dealing with. You can use 'Me'
' unless you are dealing with includes, .ascx files, etc.
DefaultButton(Me, txtBox, cmdButton)更多精彩文章及讨论,请光临枫下论坛 rolia.net