1. 程式人生 > >如何把Button按紐繫結到Enter鍵

如何把Button按紐繫結到Enter鍵

問題:.net下的login控制元件當頁面再用了一個按鈕控制元件時,回車鍵替代不了點選登入按鈕,

解決的方法:

在頁面的.cs檔案中加入程式碼

 protected void Page_Load(object sender, EventArgs e)
        {
            TextBox password = (TextBox)this.logUser.FindControl("password");
            ImageButton LoginImageButton=(ImageButton)this.logUser.FindControl("LoginImageButton");
            if (password != null)
            {
                password.Attributes.Add("回車", "if(event.keyCode==13)document.getElementById('" + LoginImageButton.ClientID + "').focus();");
            }
        }
其中 password,LoginImageButton控制元件都是在login控制元件裡面,所以要先找到這兩個控制元件,

註釋:password 密碼(TextBox)  LoginImageButton  登入(ImageButton )