返回顶部

收藏

强制按钮点击输入

更多
Public Sub SetDefaultButton(ByVal page As Page, ByVal textControl As TextBox, ByVal defaultButton As Button)

    'Sets default buttons.
    'Originally created by Janus Kamp Hansen - <a href="http://www.kamp-hansen.dk">http://www.kamp-hansen.dk
    'Extended by Darrell Norton - <a href="http://dotnetjunkies.com/weblog/darrell.norton/">http://dotnetjunkies.com/weblog/darrell.norton/ 
    '-- added Mozilla support, fixed a few issues, improved performance

    Dim theScript As String

    theScript = "<SCRIPT language='javascript'>" & NewLine
    theScript &= "<!--" & NewLine
    theScript &= "function fnTrapKD(btn, event){" & NewLine
    theScript &= "if (document.all){" & NewLine
    theScript &= "  if (event.keyCode == 13){" & NewLine
    theScript &= "   event.returnValue=false;" & NewLine
    theScript &= "   event.cancel = true;" & NewLine
    theScript &= "   btn.click();" & NewLine
    theScript &= "  }" & NewLine
    theScript &= " }" & NewLine
    theScript &= " else if (document.getElementById){ " & NewLine
    theScript &= "  if (event.which == 13){" & NewLine
    theScript &= " event.returnValue=false;" & NewLine
    theScript &= "   event.cancel = true;" & NewLine
    theScript &= "   btn.click();" & NewLine
    theScript &= "  }" & NewLine
    theScript &= " }" & NewLine
    theScript &= " else if(document.layers){" & NewLine
    theScript &= "  if(event.which == 13){" & NewLine
    theScript &= "   event.returnValue=false;" & NewLine
    theScript &= "   event.cancel = true;" & NewLine
    theScript &= "   btn.click();" & NewLine
    theScript &= "  }" & NewLine
    theScript &= " }" & NewLine
    theScript &= "}" & NewLine
    theScript &= "// -->" & NewLine
    theScript &= " </SCRIPT>" & NewLine

    page.RegisterStartupScript("ForceDefaultToScript", theScript)
    textControl.Attributes.Add("onkeydown", "fnTrapKD(" & defaultButton.ClientID & ",event)")

End Sub
//该片段来自于http://outofmemory.cn

标签:VB,控件

收藏

0人收藏

支持

0

反对

0