By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

submit + enter button - same issue but different problem

P: n/a
I have TWO submit buttons of type IMAGE on my asp form. This renders as
<input type="image">.

I need to be able to eble the ENTER button for both buttons. Yes, I know
that for the input type image, the submit happens automatically to the
server. Heres my problem:

I have two event handlers for both these buttons as stated below:

BUTTON 1:
-------------
private void formApplySubmit_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
if (Page.IsValid)
{
formApplySubmit.Visible = false;
formGoMainSubmit.Visible = true;
}
}

BUTTON 2:
-------------
private void formGoMainSubmit_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
if (Page.IsValid)
{
Server.Transfer(NEXT_PAGE);
}
}
SO you see whats happening? One button is hidden and then the other comes in
focus. I need to enable the enter key for both these buttons AND fire these
same events directly depending on which button is visible. Id rather not use
third party controls and I have loads of form elements on my page which are
not all text boxes. They are radio buttons, drop downs etc etc which have
their own events firing sometimes.

Thanks,
Girish


Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Nov 18 '05 #2

P: n/a
Would this work for netscape as well? dosent seem to be working.

thanks,
girish
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:6y*************@cpmsftngxa06.phx.gbl...
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

Nov 18 '05 #3

P: n/a

I got this far with netscape - but it does not like document.all

function Document_onKeyDown(e)
{
var nKey = -1;
var btn = document.getElementById("hdDefaultButton").value;

if (e && e.which)
nKey = e.which; // NS4 & NS6
else if (window.event && window.event.keyCode)
nKey = window.event.keyCode; // IE

if (nKey == 13)
{
alert("enter pressed");
eval(btn).submit();
return true;
}

alert("not pressed");
return false;
}
"Girish" <gb****@tietronixinc.com> wrote in message
news:uY**************@TK2MSFTNGP12.phx.gbl...
Would this work for netscape as well? dosent seem to be working.

thanks,
girish
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:6y*************@cpmsftngxa06.phx.gbl...
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


Nov 18 '05 #4

P: n/a
figured it out. Thanks for your help.
Girish

<script language="javascript">
function Document_onKeyDown(e)
{
var nKey = -1;
var btn = document.getElementById("hdDefaultButton").value;

if (e && e.which)
nKey = e.which; // NS4 & NS6
else if (window.event && window.event.keyCode)
nKey = window.event.keyCode; // IE

if (nKey == 13)
{
document.getElementById(btn).click();
return true;
}

return false;
}

</script>
"Girish" <gb****@tietronixinc.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

I got this far with netscape - but it does not like document.all

function Document_onKeyDown(e)
{
var nKey = -1;
var btn = document.getElementById("hdDefaultButton").value;

if (e && e.which)
nKey = e.which; // NS4 & NS6
else if (window.event && window.event.keyCode)
nKey = window.event.keyCode; // IE

if (nKey == 13)
{
alert("enter pressed");
eval(btn).submit();
return true;
}

alert("not pressed");
return false;
}
"Girish" <gb****@tietronixinc.com> wrote in message
news:uY**************@TK2MSFTNGP12.phx.gbl...
Would this work for netscape as well? dosent seem to be working.

thanks,
girish
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:6y*************@cpmsftngxa06.phx.gbl...
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



Nov 18 '05 #5

P: n/a
Hi Girish,

Thanks for your response. Actually I haven't considered that you'll apply
the code on netscape browser. As you've found the "document.all" is not
supported in netscape, however the "document.getElementById()" has the same
function and is supported by both IE and netscape. Anyway, I'm glad that
you've figured it out finally and thanks again for posting in community.
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

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.