• [置顶] Gridview中textbox列,按回车键或者上下键自动下移


    1.aspx代码,因为textbox的外面又一层table所以用到了很多children[],希望有更好的办法,真实例子粘过去就能运行

     <script src="jquery-1.4.2.min.js" type="text/javascript"></script>

    <script type="text/javascript">
            //window.onbeforeunload = function () { return "qwewqeqweqwe" };
            function keyPressed() {
                var element = null;
                element = event.srcElement;
                if (!element || element == null) element = event.target;
                if (element.type != "text") return;
                var tblGrid = document.getElementById("gdv");
                var TBID = element; //当前ID
                var CurCellID = TBID.parentElement.parentElement.parentElement.parentElement.parentElement;
                var CurCellIndex = CurCellID.cellIndex; //当前是第几列
                var CurRowIndex = CurCellID.parentElement.rowIndex; //当前是第行; 
                var rowcount = tblGrid.rows.length; //总行
                var CellCount = tblGrid.rows[CurRowIndex].cells.length; //当前行的总列数
                var TargateRow = -1; //目标行(默认值为-1)
                var TargateCell = -1; //目标列  (默认值为-1)
                var R, C;
                if (event.keyCode == 40 || event.keyCode == 13 || event.keyCode == 38) {
                    if (tblGrid.rows[CurRowIndex].cells[CurCellIndex].children[0].children[0].children[0].children[0].children[0] != null) {
                        if (tblGrid.rows[CurRowIndex].cells[CurCellIndex].children[0].children[0].children[0].children[0].children[0].id == TBID.id) {

                            //向下光标****************************************************************************
                            if (event.keyCode == 40 || event.keyCode == 13)//Buttom
                            {
                                if (CurRowIndex < rowcount - 1) {
                                    if (tblGrid.rows[CurRowIndex + 1].cells[CurCellIndex].children[0] != null) {
                                        if (tblGrid.rows[CurRowIndex + 1].cells[CurCellIndex].children[0].children[0].children[0].children[0].children[0].type == 'text') {
                                            //downvalue
                                            tblGrid.rows[CurRowIndex + 1].cells[CurCellIndex].children[0].children[0].children[0].children[0].children[0].select();
                                            return false;
                                        }
                                    }
                                }
                                else {
                                    //当在最后一行按向下光标键时,新增一行空白行
                                }
                            }

                            //向上光标(当前行必须大于第一行)******************************************************
                            if (event.keyCode == 38 && CurRowIndex > 1)//up
                            {
                                if (tblGrid.rows[CurRowIndex - 1].cells[CurCellIndex].children[0].children[0].children[0].children[0].children[0] != null) {
                                    if (tblGrid.rows[CurRowIndex - 1].cells[CurCellIndex].children[0].children[0].children[0].children[0].children[0].type == 'text') {
                                        //upvalue
                                        tblGrid.rows[CurRowIndex - 1].cells[CurCellIndex].children[0].children[0].children[0].children[0].children[0].select();
                                        return false;
                                    }
                                }
                            }
                            //END****************************************************************************

                        }
                    }
                }
            }
                   
        </script>

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <div>
                    <asp:GridView ID="gdv" runat="server" onkeydown='return keyPressed()' AllowPaging="True"
                        PageSize="50" AutoGenerateColumns="False" Width="100%" PagerStyle-HorizontalAlign="Center">
                        <PagerSettings Visible="False" />
                        <PagerStyle HorizontalAlign="Center" />
                        <RowStyle HorizontalAlign="Center" />
                        <Columns>
                            <asp:TemplateField HeaderText="序号">
                                <ItemTemplate>
                                    <asp:Label ID="lbl" runat="server" Text="<%# Container.DataItemIndex+1%>"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="Name" HeaderText="测试" />
                            <asp:TemplateField HeaderText="名称">
                                <ItemTemplate>
                                    <table>
                                        <tr>
                                            <td>
                                                <asp:TextBox ID="BarCode" runat="server" Text='<%#Eval("HousesID") %>' Width="200px"
                                                    MaxLength="10"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:Label ID="Label1" runat="server" Text='<%#Eval("HousesID") %>'></asp:Label>
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="编辑">
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="showDivObj(this,'gdv', 5)">编辑</asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
        </form>

    2.cs代码

     List<SeeList> list = new List<SeeList>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SeeList li = new SeeList();
            li.AutoID = 1;
            li.HousesID = 21;
            li.Name = "asd1";
            list.Add(li);
            SeeList li2 = new SeeList();
            li2.AutoID = 1;
            li2.HousesID = 22;
            li2.Name = "asd2";
            list.Add(li2);
            SeeList li3 = new SeeList();
            li3.AutoID = 1;
            li3.HousesID = 23;
            li3.Name = "asd3";
            list.Add(li3);
            SeeList li4 = new SeeList();
            li4.AutoID = 1;
            li4.HousesID = 24;
            li4.Name = "asd4";
            list.Add(li4);
            SeeList li5 = new SeeList();
            li5.AutoID = 1;
            li5.HousesID = 25;
            li5.Name = "asd5";
            list.Add(li5);
            gdv.DataSource = list;
            gdv.DataBind();
        }
        #region 实体
        public class SeeList
        {
            private int m_AutoID;

            public int AutoID
            {
                get { return m_AutoID; }
                set { m_AutoID = value; }
            }

            private int m_HousesID;

            public int HousesID
            {
                get { return m_HousesID; }
                set { m_HousesID = value; }
            }

            private string m_Name;

            public string Name
            {
                get { return m_Name; }
                set { m_Name = value; }
            }
        }
        #endregion

  • 相关阅读:
    linux下的磁盘挂载
    shell中的循环语句while
    hadoop安装和配置
    shell 命令 创建/删除 软连接 ln -s
    azkaban disable 停用部分工作流
    git dev 分支merge到master
    shell 命令 zip unzip
    git代码同步服务器代码需要注意的问题
    shell 命令 if elif else fi 用法
    python 引入本地 module
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3038668.html
Copyright © 2020-2023  润新知