2

I have a log in page..Where in it contains username and password..Whenever i enter the values for username and Password and press TAB and then Press Enter it will go to next page..But my requirement is after entering the values and press Enter without Pressing Tab button it should go to next page..I am doing this code in asp.net with c#..

Username --------------- Password --------------- Lo-gin

Below specified is the aspx code

        style="top: 198px; left: 561px; position: absolute; height: 22px; width: 128px"></asp:TextBox>
         <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Please enter the username" ControlToValidate="txtuser" Display="None" ValidationGroup="Validation" SetFocusOnError="true"></asp:RequiredFieldValidator>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox 
        ID="txtpass" runat="server" 



        style="top: 239px; left: 561px; position: absolute; height: 22px; width: 128px" 
        TextMode="Password"></asp:TextBox>
         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter the password" ControlToValidate="txtpass" Display="None" ValidationGroup="Validation" SetFocusOnError="true"></asp:RequiredFieldValidator>
    <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
</p>
<p>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        style="top: 289px; left: 512px; position: absolute; height: 30px; width: 76px" 
        Text="Login" ValidationGroup="Validation" BackColor="#993300" ForeColor="White" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>

with C#

        if (e.keyPress == 13)//Error..
        {

            if (Username == "admin")
            {

                Response.Redirect("~/Admin/admininfo.aspx");
            }
            else
            {
                Response.Redirect("~/Faculty/facultyinfo.aspx");


            }
        }
            }

Need some help on this issue...The code should be written in c#..I am using VS 2010..

3 Answers3

1
   if (e.KeyChar == 13)
    {
      if((username.text!=null) && (password.text!=null))
      {
        Response.Redirect("Homepage");                
      }                  
    }

//put the above code in page load..
//if page load dint work then textBox2_KeyPress

Sasidharan
  • 3,676
  • 3
  • 19
  • 37
  • Error 20 'System.EventArgs' does not contain a definition for 'KeyChar' and no extension method 'KeyChar' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?) D:\Shreyas Tg\New project\FMS-12-09-2013\login.aspx.cs 22 18 D:\...\FMS-12-09-2013\ –  Sep 12 '13 at 05:13
  • Else you use Panel.DefaultButton Property... http://stackoverflow.com/questions/7835453/how-to-check-if-enterkey-is-pressed-in-a-textbox-in-asp-net – Sasidharan Sep 12 '13 at 05:18
  • Are you using KeyPressEventArgs? – Daniel Abou Chleih Sep 12 '13 at 05:19
  • @DanielAbouChleih R u saying about namespaces..? –  Sep 12 '13 at 05:22
  • No in your method textBox2_KeyPress or did you piut your code into page load? – Daniel Abou Chleih Sep 12 '13 at 05:23
  • So what to do for this problem..any alternate methods..? –  Sep 12 '13 at 05:32
  • Simple one line code is needed just when i press Enter Button it should go to next page.. –  Sep 12 '13 at 05:35
  • as i said use panel and paste your user name password controls inside there..then for panel make dfault button property as enter key – Sasidharan Sep 12 '13 at 05:36
1

If there is only one button, that should be the default button in an asp.net form. So on pressing enter it should submit the form. Also if there is multiple buttons in a form, the first button will be considered as default button.

The another option is put all your elements in an asp.net panel and use the defaulltButton property like given below

<asp:Panel DefaultButton="idoftheBUtton"></asp:Panel>

Also in the code behind you can specify the default button for that form, like given below

this.Form.DefaultButton=idoftheButton
Varun Paul
  • 183
  • 8
0

In your page_load event
Page.RegisterHiddenField("__EVENTTARGET", "Button1")

The following reference liks are provide the different method to achieve this task.

How to set the default button in content page using asp.net?
how to set a default 'enter' on a certain button
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton.aspx

Community
  • 1
  • 1
Bala
  • 618
  • 5
  • 21