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)
    {

    }
}

Thursday, February 2, 2012

Wizard Example 2

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default13.aspx.cs" Inherits="Default13" %>
<!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></title>
    <script type="text/javascript">
        function showtext() {
         
            var len = document.getElementById('AddPropertyWizard_lst').value.length;
            if (len <= 0) {
                alert('enter something');
                return false;
            }
        }
      
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="sc1" runat="server"></asp:ScriptManager>
    <table border="1">
    <tr>
    <td>
    
    
     <asp:Wizard ID="AddPropertyWizard" runat="server" ActiveStepIndex="0" 
           
           Width="98%" margin-left="10px"
            DisplaySideBar="False">
           
            <StartNavigationTemplate>
                <asp:Button ID="StartNextButton" runat="server" CommandName="MoveNext" OnClick="AddPropertyWizard_NextButtonClick"
                    Text="Next" />
            </StartNavigationTemplate>
            <WizardSteps>
            
                <asp:WizardStep ID="WizardStep1" runat="server">
                <table>
                <tr>
                <td>UserName:</td>
                <td>
                <asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
                </td>
                </tr>
                <tr>
                <td> Password:</td>
               <td>
               <asp:TextBox ID="txtpass" runat="server"></asp:TextBox>
                </td>
                </tr>
                </table>    
                </asp:WizardStep>
                <asp:WizardStep ID="WizardStep2" runat="server">
               <table>
                <tr>
                <td>Address:</td>
                <td>
                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                </td>
                </tr>
                <tr>
                <td>Phone:</td>
                
                <td>
                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
                
                </td>
                </tr>
                </table> 
                </asp:WizardStep>
               <asp:WizardStep ID="WizardStep3" runat="server">
               <table>
                <tr>
                <td> Office:</td>
               
                <td>
               <asp:TextBox ID="txtOffice" runat="server"></asp:TextBox>
                </td>
                </tr>
                
                <tr>
                <td> Map:</td>
                
                <td>
               <asp:TextBox ID="txtMap" runat="server"></asp:TextBox>
                
                </td>
                </tr>
                <tr>
                <td>
                <asp:ListBox ID="lst" runat="server" Width="100px" onKeyDown='showDialog("Down")' onKeyPress='showDialog("Press")'>
        
        </asp:ListBox>
        <asp:Button ID="fill" runat="server" Text="Fill" OnClick="fill_Click" />
                </td>
                </tr>
                </table> 
                </asp:WizardStep>
                </WizardSteps>
                   <FinishNavigationTemplate>
                <asp:Button ID="FinishPreviousButton" runat="server" CausesValidation="False" 
                    CommandName="MovePrevious" Text="Previous" />
                <asp:Button ID="FinishButton" runat="server" CommandName="MoveComplete"  OnClick="AddPropertyWizard_FinishButtonClick"
                    Text="Finish" />
            </FinishNavigationTemplate>
            <HeaderTemplate>
                Wizard Example
            </HeaderTemplate>
            <StepNavigationTemplate>
                <asp:Button ID="StepPreviousButton" runat="server" CausesValidation="False" CommandName="MovePrevious" OnClick="AddPropertyWizard_PreviousButtonClick"
                    Text="Previous" />
                <asp:Button ID="StepNextButton" runat="server" CommandName="MoveNext" Text="Next" OnClick="AddPropertyWizard_NextButtonClick" />
            </StepNavigationTemplate>
                </asp:Wizard>
               
             </td>
    </tr>
    </table>
</div>
<div>
<table>
<tr>
<td>
<asp:Label ID="lbl1" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl3" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl4" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl5" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl6" runat="server"></asp:Label>
</td>
</tr>
</table>
</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 Default13 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ClickButton();
       
    }
    public void ClickButton()
    {
       
        Button btn = AddPropertyWizard.FindControl("StartNavigationTemplateContainerID").FindControl("StartNextButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "return confirm('Are you sure you want to move away from the beginning?')";
        }
        btn = AddPropertyWizard.FindControl("StepNavigationTemplateContainerID").FindControl("StepNextButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "return confirm('Are you positive you want to move next?')";
        }
        btn = AddPropertyWizard.FindControl("StepNavigationTemplateContainerID").FindControl("StepPreviousButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "return confirm('Are you sure you want to go back?')";
        }
        btn = AddPropertyWizard.FindControl("FinishNavigationTemplateContainerID").FindControl("FinishPreviousButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "confirm('Almost there!?')";
              
        }
        btn = AddPropertyWizard.FindControl("FinishNavigationTemplateContainerID").FindControl("FinishButton") as Button;
        if (btn != null)
        {
            btn.OnClientClick = "return showtext()"; //confirm('Almost there!?')
        }
    }
    protected void fill_Click(object sender, EventArgs e)
    {
        lst.Items.Add("One");
        //when listbox2 is filled then make SelectedIndex = 0, otherwise dont set SelectedIndex;
         lst.SelectedIndex = 0;
    }
    protected void AddPropertyWizard_NextButtonClick(object sender, EventArgs e)
    {
        int Index = Convert.ToInt32(AddPropertyWizard.ActiveStepIndex);
        switch (Index)
        {
            case 0:
                AddPropertyWizard.ActiveStepIndex = 1;
                break;
            case 1:
                AddPropertyWizard.ActiveStepIndex = 2;
                break;
            case 2:
                AddPropertyWizard.ActiveStepIndex = 3;
                break;
        }
    }
    protected void AddPropertyWizard_PreviousButtonClick(object sender, EventArgs e)
    {
        int Index = Convert.ToInt32(AddPropertyWizard.ActiveStepIndex);
        switch (Index)
        {
            case 1:
                AddPropertyWizard.ActiveStepIndex = 0;
                break;
            case 2:
                AddPropertyWizard.ActiveStepIndex = 1;
                break;
            case 3:
                AddPropertyWizard.ActiveStepIndex = 2;
                break;
        }
    }
    protected void AddPropertyWizard_FinishButtonClick(object sender, EventArgs e)
    {
        lbl1.Text = "The UserName is :" + txtuser.Text + "\n";
        lbl2.Text = "The Password is:" + txtpass.Text + "\n";
        lbl3.Text = "The Address is:" + txtAddress.Text + "\n";
        lbl4.Text = "The Phone is:" + txtPhone.Text + "\n";
        lbl5.Text = "The Office is:" + txtOffice.Text + "\n";
        lbl6.Text = "The Map is:" + txtMap.Text + "\n";
       // 
    }
}