Page_Load:
if (!Page.IsPostBack)
{
TextBox1.Attributes["onkeydown"]="if (event.keyCode==13) { document.all.Button1.click; return false;}"
}
if (!Page.IsPostBack)
{
TextBox1.Attributes["onkeydown"]="if (event.keyCode==13) { document.all.Button1.click; return false;}"
}
TextBox1.Attributes("onkeydown")="if (event.keyCode==13) { document.all.Button1.click; return false;}"
End if
'在此处放置初始化页的用户代码
TextBox1.Attributes.Add("onkeydown", "if(event.keyCode==13) document.all." + Button1.ClientID + ".click()")
Button1.Attributes.Add("onclick", "alert('你点了')")
End Sub
if Not IsPostBack then
TextBox1.Attributes("onkeydown")="if (event.keyCode==13) { document.all.Button1.click; return false;}"
End if这段代码去掉后也是一样啊,要按两下回车才默认执行搜索引擎后面的那个按钮Click动作!
TextBox1.Attributes("onkeydown")="if (event.keyCode==13) { document.all.Button1.click; return false;}"
End if
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
上面的代码放在Page_Load里面,不要放在按钮事件
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function clickbutton()
{
if(event.keyCode==13)
{
window.document.all.Button1.click();
}
}
</script> </HEAD>
<body MS_POSITIONING="GridLayout" onkeydown="if(event.keyCode==13) return false;">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" onkeydown="clickbutton()" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>
BindDateGrid()
End Sub Keyword是TextBox控件
Search是Button控件上面的代码我全部试过了,都没有效果,很怪!不管要不要上面的代码,都要按两下回车,也就是要第二次提交页面的时候才执行默认Button里的Click动作,第一次好像是刷新,页面提交时Button控件没有被按下去的动作发生,第二次才可以看见按扭被按下去的动作,才可以执行按钮的onClick程序。
if Not IsPostBack Then
KeyWord.Attributes("onkeydown")="if (event.keyCode==13) { document.all.Search.click(); return false;}"
End if
BindDateGrid()
End Sub