Gridview Enter key ..

1 posts, 0 answers
  1. PRABAKAR
    PRABAKAR avatar
    1 posts
    Member since:
    Apr 2012

    Posted 02 Apr 2012 Link to this post

    Hi..

    Gridview enterkey is not working ..plz resolve this.

    Design Code:

    <head runat="server">
        <script type="text/javascript">

            function controlEnter(obj, event) {

                var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

                if (keyCode == 13) {

                    document.getElementById(obj).focus();

                    return false;
                }

                else {
                    return true;
                }
            }



        </script>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h3>
                Gridview with Enter Key sample</h3>
        </div>
        <div>
            <asp:GridView ID="Grid_EnterKey" runat="server" AutoGenerateColumns="false"
               OnRowDataBound="Grid_EnterKey_RowDataBound"
               >
                <Columns>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label ID="lbl_emp_id" runat="server" Visible="true" Text='<%# Bind("ID") %>'></asp:Label>
                            <%--<asp:TextBox ID="txt0" ReadOnly="false" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>--%>
                        </ItemTemplate>
                        <%--<EditItemTemplate>
                            <asp:TextBox ID="txt00" ReadOnly="false" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
                        </EditItemTemplate>--%>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="FIRST NAME">
                        <ItemTemplate>
                            <%--<asp:Label ID="lbl_emp_fname" Visible="false" runat="server" Text='<%# Bind("FIRST_NAME") %>'></asp:Label>--%>
                            <asp:TextBox ID="txt1" ReadOnly="false" runat="server" Text='<%# Bind("FIRST_NAME") %>'></asp:TextBox>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txt01" ReadOnly="false" BorderStyle="Dotted" runat="server" Text='<%# Bind("FIRST_NAME") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MAIL ID">
                        <ItemTemplate>
                            <asp:Label ID="lbl_mail" runat="server" Visible="true" Text='<%# Bind("MAIL_ID") %>'></asp:Label>
                            <%--<asp:TextBox ID="txt2" ReadOnly="false" runat="server" Text='<%# Bind("MAIL_ID") %>'></asp:TextBox>--%>
                        </ItemTemplate>
                        <%-- <EditItemTemplate>
                            <asp:TextBox ID="txt02" ReadOnly="false" runat="server" Text='<%# Bind("MAIL_ID") %>'></asp:TextBox>
                        </EditItemTemplate>--%>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MOBILE NO">
                        <ItemTemplate>
                            <%--<asp:Label ID="lbl_emp_mobileno" runat="server" Visible="true" Text='<%# Bind("EMP_MOBILE") %>'></asp:Label>--%>
                            <asp:TextBox ID="txt3" ReadOnly="false" runat="server" Text='<%# Bind("EMP_MOBILE") %>'></asp:TextBox>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txt03" ReadOnly="false" runat="server" Text='<%# Bind("EMP_MOBILE") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="SALARY">
                        <ItemTemplate>
                            <asp:TextBox ID="txt4" runat="server" Text='<%# Bind("EMP_SALARY") %>'></asp:TextBox>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txt04" runat="server" Text='<%# Bind("EMP_SALARY") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            &nbsp;
        </div>
        </form>
    </body>

    **************************************************************************************************************************

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;

    public partial class WebForms_Enter_key_GV_Sample3_Sample3_GV_enterkey : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridBind();


            for (int i = 0; i < Grid_EnterKey.Rows.Count; i++)
            {
                string firstTextbox = "txt1";
                string NextTxtBx = "txt1";

                string FourthColumnTextbx = "txt3";
                string FifthColummnTextbx = "txt4";

                TextBox Txtbx1 = (TextBox)Grid_EnterKey.Rows[i].FindControl(firstTextbox);
                TextBox Txtbx2 = new TextBox();
                TextBox Txtbx3 = (TextBox)Grid_EnterKey.Rows[i].FindControl(FourthColumnTextbx);
                TextBox Txtbx4 = (TextBox)Grid_EnterKey.Rows[i].FindControl(FifthColummnTextbx);

                if (Grid_EnterKey.Rows.Count - 1 != i)
                {
                    Txtbx2 = (TextBox)Grid_EnterKey.Rows[i + 1].FindControl(NextTxtBx);
                }
                else
                {
                    Txtbx2 = (TextBox)Grid_EnterKey.Rows[0].FindControl(NextTxtBx);
                }

                if (Txtbx1 != null && Txtbx2 != null && Txtbx3 != null && Txtbx4 != null)
                {

                    Txtbx1.Attributes.Add("onkeypress", "return controlEnter('" + Txtbx3.ClientID + "', event)");
                    Txtbx3.Attributes.Add("onkeypress", "return controlEnter('" + Txtbx4.ClientID + "',event)");
                    Txtbx4.Attributes.Add("onkeypress", "return controlEnter('" + Txtbx2.ClientID + "',event)");
                }
            }

            TextBox Txtbxfirst = (TextBox)Grid_EnterKey.Rows[0].FindControl("txt1");
            Txtbxfirst.Focus();
        }

        protected void Grid_EnterKey_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataRowView drv = e.Row.DataItem as DataRowView;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int iEmpID = Convert.ToInt32(drv["ID"]);
                int currentrow = Convert.ToInt16(e.Row.RowIndex);

                for (int i = 1; i <= Grid_EnterKey.Rows.Count; i++)
                {
                    Grid_EnterKey.Attributes.Add("onkeypress", "return controlEnter('" + e.Row.ClientID + "', event)");

                }
            }
        }

        protected DataSet GridBind()
        {

            string strSQLconn = @"Data Source=PRO1; Initial Catalog=AllTestExample_queries; UId=sa;password=mcl@123; Integrated Security=true;";
            SqlConnection SQLconn = new SqlConnection(strSQLconn);
            SQLconn.Open();

            SqlCommand cmd = new SqlCommand();
            string strQuery = @"SELECT [EMP_id] AS ID,[EMP_fname] AS FIRST_NAME,[EMP_email] AS MAIL_ID,[EMP_mobileno] AS EMP_MOBILE,[EMP_Salaery] AS EMP_SALARY FROM [DataKeys_exm]";
            cmd.Connection = SQLconn;
            cmd.CommandText = Convert.ToString(strQuery);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            Grid_EnterKey.DataSource = ds.Tables[0];
            Grid_EnterKey.DataBind();

            return ds;
        }

    **************************************************************************************************************************

    Thanks ,
    PRABAKARAN G
Back to Top