C#webform中如何獲得輸入文字框的焦點?

來源:互聯網
上載者:User

用javascript實現吧,在onload="init();"  
  function   init()  
  {  
      document.Form1.txt_userid.focus();  
  }  
  注意txt_userid是HTM控制項,然後再把當設成server就行了

 

using   System;  
  using   System.Text;  
  using   System.Web;  
  using   System.Web.UI;  
   
  namespace   Application  
  {  
  ///   <summary>  
  ///   擷取焦點類  
  ///   </summary>  
  public   class   Focus  
  {  
  public   Focus()  
  {  
   
  }  
   
  //設定成一個靜態方法,這樣在使用的時候不用建立一個執行個體對象,直接採用Focus.SetFocus(this,   "Button2")的形式進行調用。  
  public     static   void   SetFocus(System.Web.UI.Page   page,   String   m_focusedControl)  
  {  
   
  //如果控制項名稱為空白,則返回  
  if(m_focusedControl   ==   "")  
  return;  
   
  //添加指令碼以聲明函數  
  StringBuilder   sb   =   new   StringBuilder("");  
  sb.Append("<script   language=javascript>");  
  sb.Append("function   ");  
  sb.Append("setFocusFunctionName");  
  sb.Append("(ctl)     {");  
  sb.Append("if(document.forms[0][ctl]   !=   null)   ");//如果不為空白,則設定焦點,這裡調用的Javascript裡面的方法  
  sb.Append("   document.forms[0][ctl].focus();"   );  
  sb.Append("}");  
         
  //添加指令碼以調用函數  
  sb.Append("setFocusFunctionName");  
  sb.Append("('");  
  sb.Append(m_focusedControl);  
  sb.Append("');");  
  sb.Append("</");  
  sb.Append("script>");  
         
  if   (!page.IsStartupScriptRegistered("SetFocusScriptName"))    
  page.RegisterStartupScript("SetFocusScriptName",   sb.ToString());//將這段javascript代碼寫到頁面中去  
  }  
   
  ///   <summary>    
  ///   設定在頁面斷行符號時觸發事件的控制項    
  ///   </summary>    
  ///   <param   name="Ctrl">將觸發事件的控制項對象</param>    
  public   static   void   SetEnterControl(System.Web.UI.Control   Ctrl)    
  {    
  Page   mPage   =   Ctrl.Page;    
  string   mScript;    
  mScript   =   @"<script   language=""javascript"">    
                function   document.onkeydown()    
                {    
                var   e   =   event.srcElement;    
                var   k   =   event.keyCode;    
                if   (k   ==   13   &&   e.type   !=   ""textarea"")    
                {    
                document.all."   +   Ctrl.ClientID   +   @".click();    
                event.cancelBubble   =   true;    
                event.returnValue   =   false;    
                }    
                }    
                </script>";    
  if(!mPage.IsClientScriptBlockRegistered("SetEnterControl"))    
  mPage.RegisterClientScriptBlock("SetEnterControl",mScript);    
  }  
  }  
  }   
  
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.