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

解决方案 »

  1.   


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

  2.   

    为你的表单添加KEYPRESS事件,在事件里加上以下代码就可以了            if((int)e.KeyChar ==13){
                    //BUTTON1为你执行单击事件的那个按钮.
                     button1.PerformClick(); 
                }
      

  3.   


    onkeydown="if(event.keyCode==13){document.getElementById('Button1').click();return false;}
      

  4.   

    to: marvintang 
    如果只 alert(); 测试, ok,但是如果换成后台事件,则只能执行一个。why?
      

  5.   

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

  6.   


    <!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>没特别明白搂住的意思,是这样么
      

  7.   

    function keyword()
    {
       if(event.keyCode==13)
        {document.getElementById('Button1').click();
         event.cancelBubble = true; 
         event.returnvalue=false;
         return false;}
    }
    function keyword()
    {
       if(event.keyCode==13)
        {document.getElementById('Button2').click();
         event.cancelBubble = true; 
         event.returnvalue=false;
         return false;}
    }
      

  8.   


    <%@ Register Src="Control/HotSpot.ascx" TagName="HotSpot" TagPrefix="uc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
            function aa()
            {
                if(event.keyCode==13){document.getElementById('Button2').click();
                return false;}
            }
            function bb()
            {
                if(event.keyCode==13){document.getElementById('Button1').click();
                return false;}
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox2" runat="server" Style="position: static" onkeydown="aa()"></asp:TextBox> 
            <asp:Button  ID="Button2" runat="server" Style="position: static" Text="Button" OnClick="Button2_Click" />
            <asp:TextBox ID="TextBox1" runat="server" Style="position: static" onkeydown="bb()"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Style="position: static" Text="Button" OnClick="Button1_Click" />
           </div>
        </form>
    </body>
    </html>
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write(this.TextBox1.Text);
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Write(this.TextBox2.Text);
        }大家测试下,很奇怪,现在button2 在前,它不管那个都会执行button2 ,如果换button1在前,就都会执行button1.看看怎么解决。
      

  9.   

    你的onkeydown方法对应两个,而且还return false了,当然只执行上面那个了,下面那个也换成aa()
    function aa()
    {
        if(event.keyCode==13){
            document.getElementById('Button2').click();
            document.getElementById('Button1').click();
            return false;
        }
    }
    把bb去掉,而且你要满足什么呢,这样写只要按下回车就会执行两个button,如果需要check,最好用我上面回答那个,后台的改成你自己的方法,我那个是判断空的,还去空格了,你的text没入力就不会执行button的onclick事件
      

  10.   

    标准解决方案:
    <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>