C# TextBox事件使用实例向你介绍了一个避免Button按钮起作用反而对Enter回车键有响应的实现,其目的就是做到Enter键来实现Tab效果的问题,那么我们来看看具体的实现效果、实现方法以及实现的实例。
C# TextBox事件的效果预期一般情况下:
◆当一个页面有TextBox以及Button的时候
◆当光标停留在TextBox上 此时按Enter键 回车
◆就会发现光标将焦点停留在Button上并且触发了Button的按钮事件
这里C# TextBox事件实现如下效果
一个包含[姓名TextBox1]和[备注TextBox2]以及[确定Button按钮]的页面
1.屏蔽按钮对回车键的响应 而只响应鼠标的点击
2.以及实现Enter键达到Tab键的转换
◆即TextBox1非空时 按回车键 光标转到TextBox2
◆当TextBox2非空时 按回车键 光标转到Button
◆当光标停留在Button上时 按回车键 光标转到TextBox1
C# TextBox事件代码示例如下:
﹤%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default2.aspx.cs" Inherits="Default2" %﹥
﹤html﹥
﹤head id="Head1" runat="server"﹥
﹤title﹥屏蔽Button按钮对Enter键的响应﹤/title﹥
﹤script language="javascript" type="text/javascript"﹥
//TextBox1的 onkeydown事件
//若TextBox1非空 则光标停留在TextBox2
function TextBox1onKeyDown()
{
if(event.keyCode==13)
{
if(document.all.TextBox1.value=="")
{
document.all.TextBox1.focus();
event.keyCode = 0;
event.returnValue = false;
}
else
{
//event.keyCode=9;//Enter键-﹥Tab键
document.all.TextBox2.focus();
event.keyCode = 0;
event.returnValue = false;
}
}
}
//TextBox2的 onkeydown事件
//若TextBox2非空 则光标停留在Button1
function TextBox2onKeyDown()
{
if(event.keyCode==13)
{
if(document.all.TextBox2.value=="")
{
document.all.TextBox2.focus();
event.keyCode = 0;
event.returnValue = false;
}
else
{
//event.keyCode=9;//Enter键-﹥Tab键
document.all.Button1.focus();
event.keyCode = 0;
event.returnValue = false;
}
}
}
//Button1的 onkeydown事件
//如果是回车键
//则光标停留在TextBox1
//不触发按钮事件
function Button1onKeyDown()
{
if(event.keyCode==13)
{
document.all.TextBox1.focus();
event.keyCode = 0;
event.returnValue = false;
}
}
//Button1的 onClick事件
function btnOnClick()
{
alert(event.keyCode);
alert('onclick');
}
﹤/script﹥
﹤/head﹥
﹤body﹥
﹤form id="form1" runat="server"﹥
﹤asp:TextBox ID="TextBox1" runat="server"﹥﹤/asp:TextBox﹥
﹤asp:TextBox ID="TextBox2" runat="server"﹥﹤/asp:TextBox﹥
﹤asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/﹥
﹤/form﹥
﹤/body﹥
﹤/html﹥
C# TextBox事件实现后台代码:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.TextBox1.Attributes.Add("onkeydown", "TextBox1onKeyDown();");
this.TextBox2.Attributes.Add("onkeydown", "TextBox2onKeyDown();");
this.Button1.Attributes.Add("onkeydown", "Button1onKeyDown();");
this.Button1.Attributes.Add("onclick", "btnOnClick();");
this.TextBox1.Focus();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("onclickServer");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
C# TextBox事件的相关内容和实现实例就向你介绍到这里,希望那个对你了解和学习C# TextBox事件有所帮助。
【编辑推荐】