1、輸入框得到焦點,清空輸入框
很多頁面例如搜尋方塊等,需要當滑鼠焦點在輸入框中時,清空輸入框中內容,這個非常容易實現,請看下面代碼:
<asp:textbox id="txb_KeyWord" MaxLength="20" Runat="server" Width="100px" EnableViewState="False" onFocus="this.value='';">請輸入關鍵字</asp:textbox>
僅僅只需要在該控制項代碼中加入一個屬性就可以,為:onFocus=this.value='';注意是兩個單引號,在前面完整的代碼中this.value='';又包括在雙引號中。
2、斷行符號鍵提交表單
ASP.NET頁面本身沒有辦法自己實現,這裡使用用戶端的javascript指令碼檢查使用者的每一次按鍵,如果為斷行符號,那麼提交表單,注意程式中兩個顏色不同的控制項名稱必須與你的ASPX頁面中的控制項的名稱完全一致,其中Form1為頁面表單的ID,btn_Select是需要斷行符號觸發的伺服器按鈕的名稱。
<script language="javascript">
function KeyDownHandler()
{
// process only the Enter key
if (event.keyCode == 13)
{
// cancel the default submit
event.returnValue=false;
event.cancel = true;
// submit the form by programmatically clicking the specified button
Form1.btn_Select.click();
}
}
</script>
下面是幾篇相關文章
Setting the default Button for a TextBox in ASP.NET
Submitting default buttons when the user presses the Enter key - finally!