TextBox
VB.Net TextBox 輸入時按下Enter
Private Sub TextBox11_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
MsgBox("輸入按下Enter")
End If
End Sub
ASP.NET加入浮水印
<asp:TextBox ...placeholder="請輸入學號"></asp:TextBox>
在ASP.NET控制項 TextBox中加入按下Enter 觸發postback。
參考網址:https://baechang.pixnet.net/blog/post/57376317
在 protected void Page_Load(object sender, EventArgs e) 中為Textbox加入Keydown事件,TextBox1.Attributes.Add("onkeydown", JScript事件),Enter的KeyCode是13,以下為程式碼:
1: if(event.which || event.keyCode)
2: {
3: if ((event.which == 13) || (event.keyCode == 13))
4: {
5: __doPostBack('Search1$LinkButton1','');
6: //參照render後的Html原始碼得知要參考的事件控制項觸發事件,加入到Textbox keydown事件中
7: return false;
8: }
9: }
10: else
11: {
12: return true
13: };
14:
15:
16:
17: utton1','');return false;}} else {return true}; ");
TextBox1.Attributes.Add("onkeydown", JScript事件),語法如下:
1: TextBox1.Attributes.Add("onkeydown",
2: "if(event.which || event.keyCode){
3: if ((event.which == 13) || (event.keyCode == 13))
4: {__doPostBack('Search1$LinkButton1','');return false;}} else {return true};
5: ");
在textbox文字框輸入關鍵字後按下Enter執行查詢
參考網址:https://dotblogs.com.tw/shadow/2017/09/26/205959
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>測試網頁</title>
</head>
<body>
<form name="myForm" >
<input type="text" name="keyword" placeholder="請輸入..." />
<input type="button" id="btnSearch" value="按我" />
</form>
<!--引用jQuery-->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function () {
//在關鍵字textbox按下Enter,執行查詢
$("input[name=keyword]").keyup(function (event) {
if (event.keyCode === 13) {
$("#btnSearch").click();
}
});
$("#btnSearch").click(function () {
console.log("向後端API執行查詢");
});
});
</script>
</body>
</html>
<script type="text/javascript">
$(function(){
$("#btnSearch").click(function () {
console.log("向後端API執行查詢");
});
$("form[name=myForm]").submit(function (event) {
$("#btnSearch").click();
//取消表單提交動作,避免畫面刷新
event.preventDefault();
});
});
</script>