Wednesday, March 7, 2012

Autofocus Controls when Posted Back

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default18.aspx.cs" Inherits="Default18" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>

   

    <script type="text/javascript">
        function keypressed(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;

            if (intKey == 13) {
                document.getElementById('<%=text2.ClientID %>').focus();
                return false;
            }

        }

        function keypressed2(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=text3.ClientID %>').focus();
                return false;

            }
        }

        function keypressed3(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=text4.ClientID %>').focus();
                return false;

            }
        }

        function keypressed4(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=text5.ClientID %>').focus();
                return false;

            }
        }

        function keypressed5(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=text6.ClientID %>').focus();
                return false;

            }
        }

        function keypressed6(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=text7.ClientID %>').focus();
                return false;

            }
        }

        function keypressed7(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=text8.ClientID %>').focus();
                return false;

            }
        }

        function keypressed8(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=text9.ClientID %>').focus();
                return false;

            }
        }

        function keypressed9(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=text10.ClientID %>').focus();
                return false;

            }
        }

        function keypressed10(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=DropDownList2.ClientID %>').focus();
                return false;

            }
        }

        function keypressed11(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=DropDownList1.ClientID %>').focus();
                return false;

            }
        }

        function keypressed12(e) {
            var intKey = (window.Event) ? e.keyCode : e.which;
            if (intKey == 13) {

                document.getElementById('<%=Button1.ClientID %>').focus();
                return false;

            }
        }

        function Validation1() {
            if (document.getElementById('<%=DropDownList1.ClientID %>').value == "Select::") {
                alert('select DropDownList');
                document.getElementById('<%=Button1.ClientID %>').focus();
                return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox  onkeypress="return keypressed(event)"  id="text1" runat="server" Width="200px" AutoPostBack="True" TabIndex="1" ></asp:TextBox><br/>
    <asp:TextBox onkeypress="return keypressed2(event)" id="text2" runat="server" Width="200px" TabIndex="2"></asp:TextBox><br/>
    <asp:TextBox onkeypress="return keypressed3(event)" id="text3" runat="server" Width="200px"  TabIndex="3"></asp:TextBox><br/>
    <asp:TextBox onkeypress="return keypressed4(event)" id="text4" runat="server" Width="200px"  TabIndex="4"></asp:TextBox><br/>
    <asp:TextBox onkeypress="return keypressed5(event)" id="text5" runat="server" Width="200px"  TabIndex="5"></asp:TextBox><br/>
    <asp:TextBox onkeypress="return keypressed6(event)" id="text6" runat="server" Width="200px"  TabIndex="6"></asp:TextBox><br/>
    <asp:TextBox onkeypress="return keypressed7(event)" id="text7" runat="server" Width="200px"  TabIndex="7"></asp:TextBox><br/>
    <asp:TextBox onkeypress="return keypressed8(event)" id="text8" runat="server" Width="200px"  TabIndex="8"></asp:TextBox><br/>
    <asp:TextBox onkeypress="return keypressed9(event)" id="text9" runat="server" Width="200px"  TabIndex="9"></asp:TextBox><br/>
    <asp:TextBox onkeypress="return keypressed10(event)" id="text10" runat="server" Width="200px"  TabIndex="10"></asp:TextBox><br/>
     
      <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" onkeypress="return keypressed11(event)"  TabIndex="11">
        <asp:ListItem>Select::</asp:ListItem>
         <asp:ListItem>TN</asp:ListItem>
          <asp:ListItem>AP</asp:ListItem>
        </asp:DropDownList><br/>

     
     <asp:DropDownList ID="DropDownList1" runat="server" onkeypress="return keypressed12(event)"  TabIndex="12">
        <asp:ListItem>Select::</asp:ListItem>
        </asp:DropDownList><br/>


     <asp:Button ID="Button1" runat="server" OnClientClick="return Validation1();" Text="Button" OnClick="Button1_Click" />
<asp:Label id="lblMsg" runat="server" forecolor="Red" text="* Username must be alphanumeric with no special characters"></asp:Label>
    </div>
    </form>
</body>
</html>




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

public partial class Default18 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            WebControl wcICausedPostBack = (WebControl)GetControlThatCausedPostBack(sender as Page);
            int indx = wcICausedPostBack.TabIndex;
            var ctrl = from control in wcICausedPostBack.Parent.Controls.OfType<WebControl>()
                       where control.TabIndex > indx
                       select control;
            ctrl.DefaultIfEmpty(wcICausedPostBack).First().Focus();
        }
    }

    protected Control GetControlThatCausedPostBack(Page page)
    {
        Control control = null;

        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}