用于gridview中的文本框的KeyPress事件

时间:2011-06-14 09:55:57

标签: asp.net

我有一个gridview           “>                                                                         <% - onkeypress =“searchKeyPress(event);” - %GT;                                                                                                                                                                                                                                                                                          

如果我在此焦点按回车键必须转到第二行同一列的文本框....

希望你明白我的问题

谢谢

2 个答案:

答案 0 :(得分:0)

使用

    if(event.keyCode == 13)
            moveToNextElement(event.target);

    function moveToNextElement(target) {
            //you have the current location target as reference
    }

答案 1 :(得分:0)

我假设您在gridview的每一行都有一个文本框,并且您希望将下一个文本框的焦点放在下面一行的同一列中,然后在特定文本框中输入。

    protected void GridView1_OnDataBound(object source, EventArgs e)
    {
        for (int index = 0; index <= GridView1.Rows.Count - 1; index++) {
        dynamic CurrentTextBox = (TextBox)GridView1.Rows(index).FindControl("yourTextBoxId");
        //Set Focus to Next TextBox 
        if ((index != GridView1.Rows.Count - 1)) {
            dynamic NextTextBox = (TextBox)GridView1.Rows(index + 1).FindControl("yourTextBoxId");
            CurrentTextBox.Attributes.Add("onkeypress", "setFocus('" + NextTextBox.ClientID + "');");
        }
        }
    }

在您的aspx页面中编写以下javascript函数。

    function setFocus(Target)
    {
        if(window.event.keyCode=="13")
        document.getElementById(Target).focus();  
    }