I have a c# web app that submits data to a server.
Recently my customer asked me to set up the asp:imagebuttons so that when a user hit the enter key, the form would be submitted. Most of these pages have two buttons submit and cancel (some have more but I'm not going to do those as they are more of a wizard
type form that use panels to hide/show things).
The submit button performs some code behind that processes the data and displays a result. I'm able to add the following code to the .aspx page.
<script language="javascript">
function ProcessKeypress() {
if (event.keyCode == 13) {
// process scan on Enter
__doPostBack('ibtnAdd','click');
}
}
</script>
This works for the most part, but the problem I'm running into is with validation. There is 1 required field on this form, entering text in a text box. Ive added the onkeypress call to the body of the
<script language=javascript> function ProcessKeypress() { if (event.keyCode == 13 && Page_IsValid) { __doPostBack('ibtnAdd','click'); } } </script> <script language=javascript> function ProcessKeypress() { if (event.keyCode == 13 && Page_IsValid) { __doPostBack('ibtnAdd','click');
} } </script>
The problem I'm running into is that the form is getting posted twice. On the first go around, hitting enter fires the proper event. Then when it's done, it fires it again for some ungodly reason that I can't identify.
Does anyone have any thoughts on this?
I've tried using Page.Validate(); and Page.IsValid in the code behind, while it sorta works when the page isn't valid, when it is, it performs the action twice and gives back a "this value exists and was not added" instead of being redirected back to the
callling page.
I can work around that if I have to, but what I dont like is the dbl post back firing. Makes no sense to me.
Generally, the default behavior for the Enter key is to submit the form. That's probably what is happening here... how about eschewing the whole "wire into the postback call" approach, and just kicking the Submit button instead? That is,
Generally, the default behavior for the Enter key is to submit the form. That's probably what is happening here... how about eschewing the whole "wire into the postback call" approach, and just kicking the Submit button instead? That is,
The odd behavior I've encountered is this. Leaving the location name blank and hitting the enter key, triggers validation, works perfect as intended.
Adding a location name and hitting enter, the first post back, fires the ibtnAdd_Click event, as intended. However, when it gets to the redirects, it doesn't redirect. It takes me back to the original page. Hitting enter again will do the exact same thing,
however, the redirect then works.
Clicking the button as originally coded works perfectly each time. Hitting enter, it only gets redirected on the second attempt.
The code looks sloppy as hell at the moment, I've moved some things around so its sort of a mess.
Well if your form is getting posted twice it means when you hit enter, two events are firing ... one from body onkeydown and other from the current selected element?
Well check if you haven't put more than onkeydown (if you put it on body you shouldn't put it on another element) and also check autopostback attributes for fields.
I don't suffer from madness,
I enjoy it every minute of my life
Well,
If you're certain that ibtnAdd_Click is executed (meaning you have set tracks or simply tried with an throw new Exception) a redirect should be made to a page.
About the message received from database you should test it like: message.Trim().ToLower() == "ok" for being certain your method tests it the right way.
I don't suffer from madness,
I enjoy it every minute of my life
Well,
If you're certain that ibtnAdd_Click is executed (meaning you have set tracks or simply tried with an throw new Exception) a redirect should be made to a page.
About the message received from database you should test it like: message.Trim().ToLower() == "ok" for being certain your method tests it the right way.
Yup, positive its executing. Walking through the function with the debugger.
When it hits Response.Redirect(page) it just goes back to the calling page.
I used to have __doPostBack("ibtnAdd", "click"); by itself as the function. That would post back twice. The first time would cycle past the response.redirect after validating that what came back from the stored procedure was valid, resubmit the form,
try to add the data a second time, and then would redirect to the page.
Status.ToString() != "OK" << In the code I'm pushing back a capital OK, so no toupper/tolower is needed.
I've tried to just set up the method ibtnAdd_Click as just a simple redirect, no other code. Same thing happens.
The odd thing is that when I add this code:
[code]
function ProcessKeypress() {
if (event.keyCode == 13 )
{
document.getElementById("ibtnAdd").click();
event.cancelBubble = true;
return false;
}
}
[/code]
When that code is processed, it only posts back 1 time as expected. However, the redirect is still not being executed even tho the debugger is hitting it. I have to hit enter at least 2 times and as many as 5 times, before the redirect fires. Which makes
no sense whatsoever to me.
Frank Phillp...
Member
125 Points
30 Posts
event.keyCode == 13 and validation
Apr 26, 2006 07:22 PM|LINK
Hi all
I have a c# web app that submits data to a server.
Recently my customer asked me to set up the asp:imagebuttons so that when a user hit the enter key, the form would be submitted. Most of these pages have two buttons submit and cancel (some have more but I'm not going to do those as they are more of a wizard type form that use panels to hide/show things).
The submit button performs some code behind that processes the data and displays a result. I'm able to add the following code to the .aspx page.
<script language="javascript">
function ProcessKeypress() {
if (event.keyCode == 13) {
// process scan on Enter
__doPostBack('ibtnAdd','click');
}
}
</script>
This works for the most part, but the problem I'm running into is with validation. There is 1 required field on this form, entering text in a text box. Ive added the onkeypress call to the body of the
<script language=javascript> function ProcessKeypress() { if (event.keyCode == 13 && Page_IsValid) { __doPostBack('ibtnAdd','click'); } } </script> <script language=javascript> function ProcessKeypress() { if (event.keyCode == 13 && Page_IsValid) { __doPostBack('ibtnAdd','click'); } } </script>
<body onkeydown="ProcessKeypress();" leftMargin="0" topMargin="0" ms_positioning="GridLayout" marginwidth="0" marginheight="0">
<form id="Main" method="post" runat="server">
The problem I'm running into is that the form is getting posted twice. On the first go around, hitting enter fires the proper event. Then when it's done, it fires it again for some ungodly reason that I can't identify.
Does anyone have any thoughts on this?
I've tried using Page.Validate(); and Page.IsValid in the code behind, while it sorta works when the page isn't valid, when it is, it performs the action twice and gives back a "this value exists and was not added" instead of being redirected back to the callling page.
I can work around that if I have to, but what I dont like is the dbl post back firing. Makes no sense to me.
Thanks in advance,
Frank
PeterBrunone
All-Star
18495 Points
3683 Posts
MVP
Re: event.keyCode == 13 and validation
Apr 26, 2006 07:44 PM|LINK
Generally, the default behavior for the Enter key is to submit the form. That's probably what is happening here... how about eschewing the whole "wire into the postback call" approach, and just kicking the Submit button instead? That is,
document.getElementById("theSubmitButton").click();
event.cancelBubble = true;
return false;
The later lines are an attempt to keep the default behavior from happening. Let me know how this works out.
Cheers,
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
TAsunder
Participant
1001 Points
215 Posts
Re: event.keyCode == 13 and validation
Apr 26, 2006 09:51 PM|LINK
Frank Phillp...
Member
125 Points
30 Posts
Re: event.keyCode == 13 and validation
Apr 27, 2006 12:47 PM|LINK
Peter,
That works, sort of. Might just be my code but I've noticed a hugely odd behavior.
Here is the code for the aspx.
<script language=javascript> function ProcessKeypress() { if (event.keyCode == 13 && Page_IsValid) { document.getElementById("ibtnAdd").click(); event.cancelBubble = true; return false; } } </script> <script language="javascript">
function ProcessKeypress() {
if (event.keyCode == 13 && Page_IsValid) {
document.getElementById("ibtnAdd").click();
event.cancelBubble = true;
return false;
}
}
</script>
</HEAD>
<body onkeydown="ProcessKeypress();" leftMargin="0" topMargin="0" ms_positioning="GridLayout" marginwidth="0" marginheight="0">
<form id=Main method=post runat="server">
Here is the code for the ibtnAdd onclick event in the .cs
private void ibtnAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
bool Parameters = false;
try
{
// Submit the data and return a bool
Parameters = SubmitLocation();
}
catch (Exception ex)
{
Session["MsgTitle"] = "Error";
Session["Message"] = ex.Message;
Session["ReturnTo"] = "Main.aspx";
Response.Redirect("../DisplayMessage.aspx");
}
if (Parameters == false)
Response.Redirect(ConfigurationSettings.AppSettings["HTTPRoot"] + "AutoScan/" + Request.QueryString["ReturnTo"].ToString() + "?scantype=" + Request["ScanType"].ToUpper().ToString());
else
Response.Redirect(ConfigurationSettings.AppSettings["HTTPRoot"] + "AutoScan/" + Request.QueryString["ReturnTo"].ToString() + "?TapeNum=" + Request["TapeNum"].ToString() + "&VDBID=" + Request["VDBID"].ToString() + "&scantype=" + Request["ScanType"].ToUpper().ToString());
}
The odd behavior I've encountered is this. Leaving the location name blank and hitting the enter key, triggers validation, works perfect as intended.
Adding a location name and hitting enter, the first post back, fires the ibtnAdd_Click event, as intended. However, when it gets to the redirects, it doesn't redirect. It takes me back to the original page. Hitting enter again will do the exact same thing, however, the redirect then works.
Clicking the button as originally coded works perfectly each time. Hitting enter, it only gets redirected on the second attempt.
The code looks sloppy as hell at the moment, I've moved some things around so its sort of a mess.
Thanks
</form>Frank
Dyno1979b
Participant
1764 Points
347 Posts
Re: event.keyCode == 13 and validation
Apr 28, 2006 07:35 AM|LINK
Well check if you haven't put more than onkeydown (if you put it on body you shouldn't put it on another element) and also check autopostback attributes for fields.
I enjoy it every minute of my life
Frank Phillp...
Member
125 Points
30 Posts
Re: event.keyCode == 13 and validation
May 01, 2006 04:49 PM|LINK
Well, I've got it sort of working. It only posts once now.
however, what not working is the redirect after the page is done.
The stored procedure returns "OK" if the submission was ok, and another message stating why it failed.
If its ok, it redirects back to the calling page, if its not, it displays the message in a label and does nothing else.
The problem is, its not redirecting. No matter what or how I call the page, it does the same thing.
Thanks
Frank
Dyno1979b
Participant
1764 Points
347 Posts
Re: event.keyCode == 13 and validation
May 02, 2006 07:12 AM|LINK
If you're certain that ibtnAdd_Click is executed (meaning you have set tracks or simply tried with an throw new Exception) a redirect should be made to a page.
About the message received from database you should test it like: message.Trim().ToLower() == "ok" for being certain your method tests it the right way.
I enjoy it every minute of my life
Frank Phillp...
Member
125 Points
30 Posts
Re: event.keyCode == 13 and validation
May 02, 2006 11:39 AM|LINK
Yup, positive its executing. Walking through the function with the debugger.
When it hits Response.Redirect(page) it just goes back to the calling page.
I used to have __doPostBack("ibtnAdd", "click"); by itself as the function. That would post back twice. The first time would cycle past the response.redirect after validating that what came back from the stored procedure was valid, resubmit the form, try to add the data a second time, and then would redirect to the page.
Status.ToString() != "OK" << In the code I'm pushing back a capital OK, so no toupper/tolower is needed.
I've tried to just set up the method ibtnAdd_Click as just a simple redirect, no other code. Same thing happens.
The odd thing is that when I add this code:
[code]
function ProcessKeypress() {
if (event.keyCode == 13 )
{
document.getElementById("ibtnAdd").click();
event.cancelBubble = true;
return false;
}
}
[/code]
When that code is processed, it only posts back 1 time as expected. However, the redirect is still not being executed even tho the debugger is hitting it. I have to hit enter at least 2 times and as many as 5 times, before the redirect fires. Which makes no sense whatsoever to me.
Its goofy.
Frank
g_hristodol
Member
105 Points
23 Posts
Re: event.keyCode == 13 and validation
May 03, 2006 01:08 PM|LINK
I have an (i think) better idea:
form tag should look like:
<form id=Form1 onsubmit="return ValidateForm(this)" method=post runat="server">
< form id="Form1" onsubmit="return ValidateForm(this)" method="post" runat="server" >
and, in Javascript:
function ValidateForm(objForma)
{
//validate any text inputted in the required field
//in here you can also set some hidden textboxes with some values, and you can read from those hidden textboxes in Page_Load (code behind).
}
This way, ValidateForm(form) will execute first and then 1(one) postback will be done.
</form>