Hi Girish,
Thanks for posting in the community!
From your description, you have two buttons on a certain ASP.NET web page.
And when one button is visible
the other is not visible. However, you'd like to set both the two buttons
as default "enter" key button depend on
which one is visible, yes?
As for this question, I think you can use a <input type=hidden ..> html
element to store the button's id (which is currently visible). Since we can
use javascript to invoke the default button, when user press "enter" key,
we dynamically invoke the certain button via the <input type=hidden ..>'s
value. Below is a sample page which applying the above means, please refer
to it if you feel anything unclear:
--------------------------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 doEnter()
{
var btn = document.getElementById("hdDefaultButton").value;
if ((event.which && event.which == 13) || (event.keyCode &&
event.keyCode == 13))
{
document.all(btn).click();
return false;
}
else
{
return true;
}
}
</script>
</HEAD>
<body onkeydown="doEnter()">
<form id="Form1" method="post" runat="server">
<input type="hidden" runat="server" id="hdDefaultButton"
name="hdDefaultButton" value="formApplySubmit">
<table width="500" align="center">
<tr>
<td>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button id="formApplySubmit" runat="server"
Text="formApplySubmit"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox4" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button id="formGoMainSubmit" runat="server"
Text="formGoMainSubmit" Visible="False"></asp:Button>
</td>
</tr>
</table>
</form>
</body>
</HTML>
-------------------------------------code behind page
class----------------------
public class MultiDefaultButton : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button formApplySubmit;
protected System.Web.UI.WebControls.Button formGoMainSubmit;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.HtmlControls.HtmlInputHidden hdDefaultButton;
private void Page_Load(object sender, System.EventArgs e)
{
TextBox1.Attributes.Add("onkeydown","doEnter()");
TextBox2.Attributes.Add("onkeydown","doEnter()");
TextBox3.Attributes.Add("onkeydown","doEnter()");
TextBox4.Attributes.Add("onkeydown","doEnter()");
}
#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.formApplySubmit.Click += new
System.EventHandler(this.formApplySubmit_Click);
this.formGoMainSubmit.Click += new
System.EventHandler(this.formGoMainSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void formApplySubmit_Click(object sender, System.EventArgs e)
{
formApplySubmit.Visible = false;
formGoMainSubmit.Visible = true;
this.hdDefaultButton.Value = "formGoMainSubmit";
Response.Write("<br>Button formApplySubmit is clicked at: " +
DateTime.Now.ToLongTimeString());
}
private void formGoMainSubmit_Click(object sender, System.EventArgs e)
{
formApplySubmit.Visible = true;
formGoMainSubmit.Visible = false;
this.hdDefaultButton.Value = "formApplySubmit";
Response.Write("<br>Button formGoMainSubmit 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