问题如下,有页面上有button1,button2,textbox1,textbox2  其中button1和textbox1   是一个功能组合。网页刚打开时,默认按钮是button1,也就是说在页面打开以后,敲回车,实际执行是button1.我想实现的功能不是简单的把默认按钮改成button2,而是:   
      刚打开页时,默认按钮为button1.   
      鼠标点在textbox2以后默认按钮自动改成button2。我自已试过两种解决方法,
第一种,用JS, 
textbox1.Attributes.add("onkeydown","javascript:if (keycode==13) { document.getElementById('button2').click();}") 
这样能实现,但是实现的button2的Click不是服务器端的OnClick方法,而是客户端的OnClientClick事件,也就是说不能直接实现OnClick方法,而是要重新用JS再编一个具有相同功能的OnClientClick脚本。因为网站很多地方要改,这样做太麻烦。第二种,利用Textbox的 OnTextChanged属性来写,加上其AutoPostBack 也能实现功能,但这样的做的问题是,如果用户在第一次在TextBox2内输入了内容并回车,功能实现,但第二次用户把鼠标点到TextBox2内,但并不改变已经输入的内容,这时,回车就会失效。请问各位高手有没有更好的方法在服务器端来实现?谢谢指教。

解决方案 »

  1.   

    就第一种方法多好啊,写JS不算太麻烦嘛。
    然后我记得有个第三方控件可以实现这个功能,不过想不起来了,你在GOOGLE上搜下先
      

  2.   


        <form id="form1" runat="server">
            <div>
                <table width="100%" cellpadding="2" cellspacing="2" border="1">
                    <tr>
                        <td>
                            <asp:TextBox ID="textBox1" runat="server"></asp:TextBox></td>
                        <td>
                            <asp:TextBox ID="textBox2" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="button1" runat="server" Text="button1" OnClick="button1_Click"></asp:Button></td>
                        <td>
                            <asp:Button ID="button2" runat="server" Text="button2" OnClick="button2_Click"></asp:Button></td>
                    </tr>
                </table>
            </div>
        </form>protected void Page_Load(object sender, EventArgs e)
    {
                textBox1.Attributes.Add("onclick", "javascript:document.getElementById('button1').focus();");
                textBox2.Attributes.Add("onclick", "javascript:document.getElementById('button2').focus();");
    }
    protected void button1_Click(object sender, EventArgs e)
    {
       Response.Write("<script>alert('button1');</script>");
    }protected void button2_Click(object sender, EventArgs e)
    {
         Response.Write("<script>alert('button2');</script>");
    }
      

  3.   

    这样能实现,但是实现的button2的Click不是服务器端的OnClick方法,而是客户端的OnClientClick事件,也就是说不能直接实现OnClick方法,而是要重新用JS再编一个具有相同功能的OnClientClick脚本
    这是什么结论 ~  你调用 document.getElementById('button2').click() 
    如果button2 是服务器端button 的话 是会执行 OnClientClick ,可执行完 就是会提交form 去执行服务器的click 事件 ,除非 在 OnClientClick 的时候 你终止了 form 的提交
      

  4.   

    首先谢谢楼上两位的回答,但是:二楼: textBox2.Attributes.Add("onclick", "javascript:document.getElementById('button2').focus();");
    在单击TextBox2时,把button2设为焦点控件,不是默认按钮,这样根本没法在TextBox2里输入内容。三楼:
    “你调用 document.getElementById('button2').click() 
    如果button2 是服务器端button 的话 是会执行 OnClientClick ,可执行完 就是会提交form 去执行服务器的click 事件 ,除非 在 OnClientClick 的时候 你终止了 form 的提交”的确是这样的,但执行完OnClientClick后的 服务器的click 事件是还是默认控件Botton1的。所以如果是用这种方法,我是手动把一个没有OnClick属性并且Visble=false的Botton设为默认控件,这样执行 OnClientClick 就相当于没有任何操作了。请指教看还没有没其它什么方法了?
      

  5.   

    简单说来,就是每个TextBox和Botton为一组,有几组TextBox和Botton, 要实现在TextBox里输入内容后直接回车就实现和点击它对应的Botton一样功能。而默认的是,无论在你哪个TextBox里输入内容后回车都是默认按钮----第一个Botton, 而不是它对应的Botton.请指教
      

  6.   


        <script type="text/javascript">
         document.onkeydown=xx;
         function xx()
         {
             var se = event.srcElement;
             if(event.keyCode==13)
             { 
                 if(se.id=="textBox1")
                 {
                 document.getElementById("button1").focus();
                 document.getElementById("button1").click();
                 }
                 if(se.id=="textBox2")
                 {
                  document.getElementById("button2").focus();
                  document.getElementById("button2").click();
                 }
             }
         }
        </script>
    <form id="form1" runat="server">
            <div>
                <table width="100%" cellpadding="2" cellspacing="2" border="1">
                    <tr>
                        <td>
                            <asp:TextBox ID="textBox1" runat="server"></asp:TextBox></td>
                        <td>
                            <asp:TextBox ID="textBox2" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="button1" runat="server" Text="button1" OnClick="button1_Click"></asp:Button></td>
                        <td>
                            <asp:Button ID="button2" runat="server" Text="button2" OnClick="button2_Click"></asp:Button></td>
                    </tr>
                </table>
            </div>
        </form>
    protected void button1_Click(object sender, EventArgs e)
            {
                Response.Write("<script>alert('button1');</script>");
            }        protected void button2_Click(object sender, EventArgs e)
            {
                Response.Write("<script>alert('button2');</script>");
            }
      

  7.   

     if(se.id=="textBox1")
    要是有一组你就改改..
     if(se.id=="textBox1" || se.id=="textBox11" || se.id=="textBox111")
      

  8.   

    xzq686 的解答真详细,不过绕了半天其实只用给楼主说,把你第一种方法:
    textbox1.Attributes.add("onkeydown","javascript:if (keycode==13) { document.getElementById('button2').click();}") 中的click改成focus用OK了
    事实上,最简单方法就是在HTML页而的相应的textBox里加一个onkeydown属性,并定义就行了。比如:
    <asp:TextBox ID="textBox2" runat="server" onkeydown="if(event.keyCode==13) {document.getElementById('button2').focus();}"></asp:TextBox>
      

  9.   

    谢谢各位的解答,不仅问题解决了,我也懂了原理,应该用JS的focus方法更改默认按钮,而不是Click方法是使用Botton的onClientClick方法。
      

  10.   

    document.getElementById('button2').click();
    改为__doPostBack('button2','')