×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

FYI:

本文发表在 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
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 有个傻问题,ASP.net的一个button,想设置它为enter键的default event,怎么做?vb.net。谢谢!
    • 在Page_Load里写 button.select应该行吧
      • 不行。没有button.select,应该和accesskey有关。
        • 有点糊涂, 你是不是说你按enter的时候, 相当于click那个button?偶很菜的。
          • 是这意思,偶也很菜呀
    • You need to create a function and call it from in page_load or wherever you want:
      private void setFocus( System.Web.UI.Control ctrl )
      {
      string s = "<SCRIPT language='javascript'>document.getElementById('" + ctrl.ID + "').focus() </SCRIPT>";
      this.RegisterStartupScript( "focus", s );
      }
      • 也就时说,vb.net不行,一定要用javascript?
        • right in web application
          • 谢谢
      • I think this focus will be lost after you input anything in the textbox.
        • 不紧张,你说对了,我怎么解决?忘赐教:)
          • So far I have no better idea than my another post, maybe somebody else will have one.
        • you're right, if you want a button to become default button after entering something on the page, you have to do something else.
    • Make this button the first button in the form
      • it works. any other idea?
    • FYI:
      本文发表在 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
      • it works.
        private void TextBox1_TextChanged(object sender, System.EventArgs e)
        {
        SetFocusControl("Button3");
        }
        public void SetFocusControl( String ControlName)
        {

        String script= @"<script language="+"\"javascript\"" + ">" + " var control = document.getElementById(\"" + ControlName + "\");" + " if( control != null ){control.focus();}" + "</script>";

        Page.RegisterStartupScript("Focus", script);
        }
      • 高手高手高高手!谢!
        • Yes, they are 高手
    • http://www.schmiddy.net/blog/2003/09/aspnet_default_.html
    • A good article for this, don't forget to check the discussion for the article. (it also mentions the solution Hardy post) http://www.allasp.net/enterkey.aspx
      • 谢谢, 跟你学了很多。:))
        • 是问题问的好,加上老板下午没派活给我,就跟着长了长见识。
          • 希望你老板每天给你留点时间,偶们跟着沾光。:))
      • 是喽