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="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
<script language="javas cript">
function doEnter()
{
var btn = document.getEle mentById("hdDef aultButton").va lue;
if ((event.which && event.which == 13) || (event.keyCode &&
event.keyCode == 13))
{
document.all(bt n).click();
return false;
}
else
{
return true;
}
}
</script>
</HEAD>
<body onkeydown="doEn ter()">
<form id="Form1" method="post" runat="server">
<input type="hidden" runat="server" id="hdDefaultBu tton"
name="hdDefault Button" value="formAppl ySubmit">
<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="formApplySu bmit" runat="server"
Text="formApply Submit"></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="formGoMainS ubmit" runat="server"
Text="formGoMai nSubmit" Visible="False" ></asp:Button>
</td>
</tr>
</table>
</form>
</body>
</HTML>
-------------------------------------code behind page
class----------------------
public class MultiDefaultBut ton : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on formApplySubmit ;
protected System.Web.UI.W ebControls.Butt on formGoMainSubmi t;
protected System.Web.UI.W ebControls.Text Box TextBox1;
protected System.Web.UI.W ebControls.Text Box TextBox2;
protected System.Web.UI.W ebControls.Text Box TextBox3;
protected System.Web.UI.W ebControls.Text Box TextBox4;
protected System.Web.UI.H tmlControls.Htm lInputHidden hdDefaultButton ;
private void Page_Load(objec t sender, System.EventArg s e)
{
TextBox1.Attrib utes.Add("onkey down","doEnter( )");
TextBox2.Attrib utes.Add("onkey down","doEnter( )");
TextBox3.Attrib utes.Add("onkey down","doEnter( )");
TextBox4.Attrib utes.Add("onkey down","doEnter( )");
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.formApplyS ubmit.Click += new
System.EventHan dler(this.formA pplySubmit_Clic k);
this.formGoMain Submit.Click += new
System.EventHan dler(this.formG oMainSubmit_Cli ck);
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void formApplySubmit _Click(object sender, System.EventArg s e)
{
formApplySubmit .Visible = false;
formGoMainSubmi t.Visible = true;
this.hdDefaultB utton.Value = "formGoMainSubm it";
Response.Write( "<br>Button formApplySubmit is clicked at: " +
DateTime.Now.To LongTimeString( ));
}
private void formGoMainSubmi t_Click(object sender, System.EventArg s e)
{
formApplySubmit .Visible = true;
formGoMainSubmi t.Visible = false;
this.hdDefaultB utton.Value = "formApplySubmi t";
Response.Write( "<br>Button formGoMainSubmi t is clicked at: " +
DateTime.Now.To LongTimeString( ));
}
}
----------------------------------------------------------------------------
----------------------
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