submit + enter button - same issue but different problem

G

Girish

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
 
S

Steven Cheng[MSFT]

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
 
G

Girish

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;
}
 
G

Girish

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>
 
S

Steven Cheng[MSFT]

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
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top