<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.)
 
G

Guest

I used the code from http://www.metabuilders.com/tools/DefaultButtons.aspx and it kinda of works. I have 3 textbox that when information is entered into anyone of them and the user hits the enter key I want by submit button to be clicked. My issues it that only works on the second time the enter key is hit. Here is the code added to my form:

<script language="javascript"
function doEnter(

var btn = document.getElementById("submit").value

if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)

document.all(btn).click()
return false
}
els

return true



</script></HEAD><body onkeydown="doEnter()" MS_POSITIONING="GridLayout"

And in the on the page_load in the c# code

prospectid.Attributes.Add("onkeydown","doEnter() ")
lname.Attributes.Add("onkeydown","doEnter() ")
address.Attributes.Add("onkeydown","doEnter() ")
 
S

Steven Cheng[MSFT]

Hi,

The problem you mentioned seems to be a known issue with IE, it'll focus on
the first submit button on the page always. One way to workaround it is use
the "<input type=button ..>" button instead the ASP.NET SERVER control, for
example

<INPUT id="btnSubmit" type="button" value="Submit" name="btnSubmit"
runat="server">

Here is a demo page:
-----------------------aspx page------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>defaultbutton</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 doSubmit()
{
if ((event.which && event.which == 13) || (event.keyCode &&
event.keyCode == 13))
{
document.getElementById("btnSubmit").click();
}

}
</script>
</HEAD>
<body onkeydown="doSubmit()">
<form id="Form1" method="post" runat="server" onkeydown="doSubmit()">
<table width="100%" align="center">
<tr>
<td>
<asp:TextBox id="txtOne" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:TextBox id="txtTwo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:TextBox id="txtThree" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<INPUT id="btnSubmit" type="button" value="Submit" name="btnSubmit"
runat="server">
</td>
</tr>
</table>
</form>
</body>
</HTML>
----------------------code behind page class-----------
public class defaultbutton : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtOne;
protected System.Web.UI.WebControls.TextBox txtTwo;
protected System.Web.UI.HtmlControls.HtmlInputButton btnSubmit;
protected System.Web.UI.WebControls.TextBox txtThree;

private void Page_Load(object sender, System.EventArgs e)
{
txtOne.Attributes.Add("onkeydown","doSubmit()");
txtTwo.Attributes.Add("onkeydown","doSubmit()");
txtThree.Attributes.Add("onkeydown","doSubmit()");
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.btnSubmit.ServerClick += new
System.EventHandler(this.btnSubmit_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnSubmit_ServerClick(object sender, System.EventArgs e)
{
Response.Write("<br>btnSubmit is clicked at: " +
DateTime.Now.ToLongTimeString());
}
}
---------------------------------------------------------

Hope this helps.


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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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

Top