<enter> key and form submit

D

DotNetGruven

Hi,
I have a web form which has:

- Login area with
- email textbox
- password textbox
- <enter> button to log in

- search area with
- string to search for textbox
- <search> button

How can I make this work so that when a user presses <Enter> in:

1) either email or password textbox, I can handle the log in function
which happens on the <enter> button Click() event
2) search textbox, the <search> button Click() event happens

??

TIA,
george
 
S

Steven Cheng[MSFT]

Hi george,


Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you are wanting to set the "default" button for
different entry fields on an ASP.NET web page so that when "enter" key is
pressed with different entry field on focus, different button will be fired
click event?
If there is anything I misunderstood, please feel free to let me know.


Well, I've reviewed the thread and found that Steve has provided serveral
good tech articles on this issue. I believe you may get detailed
information on those articles. And I've also read this one:
http://www.allasp.net/enterkey.aspx

And made a simple test page on this, it does works well. Here is the test
page code, you may try it out yourself to see whether it helps.

-----------------------------------------------aspx page
-----------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>PostBack</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function doLogin()
{
if ((event.which && event.which == 13) || (event.keyCode &&
event.keyCode == 13))
{
document.all("btnLogin").click();return false;
}
else
{
return true;
}
}

function doSearch()
{
if ((event.which && event.which == 13) || (event.keyCode &&
event.keyCode == 13))
{
document.all("btnSearch").click();return false;
}
else
{
return true;
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><INPUT id="txtLogin" type="text" name="txtPost" runat="server"
onkeydown="doLogin()"></td>
<td><INPUT id="btnLogin" type="button" value="Login" name="btnPost"
runat="server"></td>
</tr>
<tr>
<td><INPUT id="txtSearch" type="text" runat="server"
onkeydown="doSearch()"></td>
<td><INPUT id="btnSearch" type="button" value="Search"
runat="server"></td>
</tr>
</table>
</form>
</body>
</HTML>

----------------------------------------------------------------code-behind
page class -------------------------------------------------------
public class PostBack : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputText txtLogin;
protected System.Web.UI.HtmlControls.HtmlInputButton btnLogin;
protected System.Web.UI.HtmlControls.HtmlInputButton btnSearch;
protected System.Web.UI.HtmlControls.HtmlInputText txtSearch;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnLogin.ServerClick += new
System.EventHandler(this.btnLogin_ServerClick);
this.btnSearch.ServerClick += new
System.EventHandler(this.btnSearch_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnLogin_ServerClick(object sender, System.EventArgs e)
{
Response.Write("<br>Login is fired at: " +
DateTime.Now.ToLongTimeString());
}

private void btnSearch_ServerClick(object sender, System.EventArgs e)
{
Response.Write("<br>Search is fired at: " +
DateTime.Now.ToLongTimeString());
}


}

--------------------------------------------
If you have any questions or need any further assistance, please feel free
to post here.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

Jerry III

The best solution to this is to not use .Net. Just create two forms and
submit them to their respective pages (unless you believe Microsoft and
think that login code should be in the same place as search code).

Jerry
 
D

DotNetGruven

Thanks all!

The info that I needed was the 3rd link provided by Steve Orr and certainly
was helped by the example provided by Steven Cheng. I was using
<asp:Button>s and that was the rub. The 2nd link was hiding from DNS today!
:)

Not sure what you are trying to say Jerry, but I am required to use and
really dig using .NET! Therefore, I tend to believe what Microsoft says.

More importantly, by boss and the product manager has defined the project
that I'm contracting on to have a login form and a search box, which isn't
unusual these days.

What I found problematic is for the <Enter> key to work when the focus is in
a test field. Do people really do this? I've always hit <Tab> until I get
to the button or use the mouse.

Again, thanks for your help guys,
George
 
S

Steven Cheng[MSFT]

Hi George,


Thanks for your response. I'm glad that the information is helpful. Also,
If you need any further help, please feel free to post here. I'll be
willing to help you.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top