日期:2014-05-18  浏览次数:20944 次

回车事件
有两个表单,两个按扭。
如果我写完第一个点回车,触发第一个按扭事件
写第二个文本,点回车,触发第二个按扭事件。
请问这样该怎么写啊。

------解决方案--------------------
C# code

 private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if((int)e.KeyChar ==13){
                button1.PerformClick(); 
            }
        }

------解决方案--------------------
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>Untitled</title>
</head>
<Script type = "text/javascript" >
window.document.onkeydown = keyword;

function keyword()
{
    if (event.keyCode==13)
    {
        if(document.form1.txtName1.value.replace(/(^\s*)/g,  "") != "" && document.form1.txtName2.value.replace(/(^\s*)/g,  "") == "")
        {
            enter(1);
        }else if(document.form1.txtName2.value.replace(/(^\s*)/g,  "") != "" && document.form1.txtName1.value.replace(/(^\s*)/g,  "") == "")
        {
            enter(2);
        }else if(document.form1.txtName1.value.replace(/(^\s*)/g,  "") != "" && document.form1.txtName2.value.replace(/(^\s*)/g,  "") != "")
        {
            enter(1);
            enter(2);
        }
        else{
            document.form1.txtName1.value = "";
            document.form1.txtName2.value = "";
            alert("空");
        }
    }
}

function enter(i)
{
    document.getElementById("btnSearch" +i).click();
}

    
</Script>
<body >
<form name = "form1" >
<table>
    <tr>
        <td>
            <input type = "text" name = "txtName1" width = "85px" height = "20px" >
        </td>
        <td>
            <input type = "button" name = "btnSearch1" width = "85px" height = "20px" onclick = "alert('aaa');">
        </td>
        <td>
            <input type = "text" name = "txtName2" width = "85px" height = "20px">
        </td>
        <td>
            <input type = "button" name = "btnSearch2" width = "85px" height = "20px" onclick = "alert('bbb');">
        </td>
    </tr>
</table>
</body>
</html>

------解决方案--------------------
标准解决方案:
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1" >
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" /></asp:Panel>
<br />

<asp:Panel ID="Panel2" runat="server" DefaultButton="Button2" >
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button" /></asp:Panel>