casta

          讓控件獲取焦點及設置輸入法全角半角或無效

           

          讓控件獲取焦點及設置輸入法全角半角或無效

          做開發的過程中,常遇到一些頁面要求光標的默認位置固定在某控件上
          看起來好像很難,其實實現方法很簡單.
          JavaScript里面有現成的設置焦點的方法
          可以寫一個簡單的函數命名為setFocus,然后在Page_Load里調用就可以了
          比如需要設置焦點的是mytextbox控件
          那么只要Page_Load寫上Call setFocus(Me.mytextbox)就可以了
          下面是VB.NET代碼實例.C#的代碼也差不多...


             Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                 Call setFocus(Me.txt_code)
                 End Sub

           

             Private Sub setFocus(ByVal ctrl As Control)
                 Dim script As String
                 script = "<script language='JavaScript'>" & _
                          "        document.getElementById('" & ctrl.ClientID & "').focus();" & _
                          "</script>"
                 RegisterStartupScript("setfocus", script)
             End Sub


          下面是輸入法設置,更簡單:

                Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                 If Not Page.IsPostBack Then
                     'IME半角設定
                     setIME(Me.Page, Me.txt_code, 1)
                     setIME(Me.Page, Me.txt_date, 1)
                 End If
                 End Sub

           


             Public Sub setIME(ByRef pgTemp As Page, _
                               ByRef ctrTemp As WebControl, _
                               ByVal intMode As Integer)
                 Select Case intMode
                     Case 0 'IME無効
                         ctrTemp.Attributes.Add("style", "ime-mode:disabled")
                     Case 1 'IME半角
                         ctrTemp.Attributes.Add("style", "ime-mode:inactive")
                     Case 2 'IME全角
                         ctrTemp.Attributes.Add("style", "ime-mode:active")
                     Case 3 'IME自動
                         ctrTemp.Attributes.Add("style", "ime-mode:auto")
                     Case 4 'IME無効且右對齊
                         ctrTemp.Attributes.Add("style", "ime-mode:disabled;text-align:right")
                     Case 5 'IME半角且右對齊
                         ctrTemp.Attributes.Add("style", "ime-mode:inactive;text-align:right")
                 End Select
             End Sub

          posted on 2008-09-22 13:59 casta 閱讀(753) 評論(0)  編輯  收藏 所屬分類: html/jsp


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 普洱| 麻阳| 密山市| 什邡市| 山东省| 合川市| 胶南市| 炉霍县| 靖江市| 麻栗坡县| 阜宁县| 乌恰县| 东乡| 濉溪县| 伽师县| 桃园县| 宁河县| 星座| 孟村| 桓仁| 慈利县| 嘉荫县| 中卫市| 平利县| 桃江县| 大同市| 凤山县| 临汾市| 永德县| 九台市| 陕西省| 赤峰市| 中牟县| 错那县| 道真| 页游| 满洲里市| 呼玛县| 广昌县| 郧西县| 松潘县|