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