在Gridview中有一列模板列,里面放的是TextBox,TextBox绑定了数据库中一个相应的字段
请问怎么实现在按回车键的时候,光标自动由上一个TextBox移动到下一个TextBox

解决方案 »

  1.   

    我写的伪代码,我对JS不熟悉
    if(eventKey==13)
    找到下一个文本框//可以利用gridview是table的优点.得到下一个tr,然后定位到txtbox然后txtbox.focus();
      

  2.   

    if(eventKey==13) 
    {
    txtbox=找到下一个文本框;
    //可以利用gridview是table的优点.得到当前txt的tr父元素,再得到tr的兄弟tr,然后定位到txtbox
    txtbox.focus();
    }
      

  3.   

    按回车键的时候,光标自动由上一个控件移动到下一个控件function keyDown()
    {
        var keycode=event.keyCode;
        var keyChar=String.fromCharCode(keycode);
        if(keycode==13)
        event.keyCode=9;
    }
    document.onkeydown=keyDown 
      

  4.   

    举个例子:
    <input id="text1" type="text" onkeypress="Do()" />
    <input id="text2" type="text" />
    <script>
       function Do()
    {
       if(event.keyCode==13)
    {
        document.getElementById("text2").focus();
    }
    }
    </script>
      

  5.   

    可我的TextBox是放在GridView中的
      

  6.   

    if(event.srcElement.tagName!='sumit'&&event.srcElement.tagName!="texterea")
    {
    if(event.keyCode==13)
    {
    event.keyCode=9
    }
    }
      

  7.   

     if(event.keyCode==13) 

        document.getElementById("text2").focus(); 

    用js得焦点
      

  8.   

    document.getElementById("text2").focus();
    只要text2是textbox的ID就行
      

  9.   

    event.keyCode=9 能够回答这个问题吗?假设TextBox右边有一个CheckBox,这不就将焦点转移到完全错误的地方了吗?楼主的表述不严格,制造了这种混乱的回答。
      

  10.   

    什么叫做“光标自动由上一个TextBox移动到下一个TextBox”呢?是必须是同一列中的下一个TextBox,还是假设同一行中右边还有另外一个TextBox就可以获得焦点?但是不论如何,写“event.keyCode=9”都是糊弄这个需求的,因为它只是让焦点转移到下一个具有输入焦点的html对象,而并不保证移到楼主要求的TextBox上。
      

  11.   

    假设只是移动到同一列的下一个TextBox这种需求,怎么设计呢?其实很简单!假设模板列中的TextBox的ID是txt,可以在GridView控件的事件中处理:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        var thisTextbox = e.Row.FindControl("txt") as TextBox;
        if (thisTextbox != null)
        {
            if (_lastRow != null)
            {
                ((TextBox)_lastRow.FindControl("txt")).Attributes["onkeydown"] = "if(event.keyCode==13){document.getElementById('" +
                    thisTextbox.ClientID + "').focus();return false;};";
            }
            _lastRow = e.Row;
        }
    }private GridViewRow _lastRow = null;
    只要设计并用好出 _lastRow 就可以了。