Solved

command button on user control fired when press enter key any where in aspx page

Posted on 2004-09-06
25
1,182 Views
Last Modified: 2008-01-16
hi
    i had made one login control and i place it on the aspx page,now i have some entry form on the web site and in that form i have put login control on the top and the remaning textboxex and command button below that login control ,now after if fill the form and as soon i press enter on the last textbox field the event on the login control for sign in button is fired and it is giving the login fail message,i donot want that the sign in button event gets fired when a user press enter key in the form,Plz help me how i can stop firing the event of the command button on the user control when the enter key is press at any text box in the form.

 
0
Comment
Question by:vikram_lashkari
  • 12
  • 11
25 Comments
 
LVL 3

Expert Comment

by:nhewitt21
ID: 11988886
I think probably the best way is by using javascript:
<SCRIPT language=""javascript"">
<!--
function NoEnter(btn, event){
 if (document.all){
  if (event.keyCode == 13){
   event.returnValue=false;
   event.cancel = true;
   btn.click();
  }
 }
 else if (document.getElementById){
  if (event.which == 13){
   event.returnValue=false;
   event.cancel = true;
   btn.click();
  }
 }
 else if(document.layers){
  if(event.which == 13){
   event.returnValue=false;
   event.cancel = true;
   btn.click();
  }
 }
}
// -->
</SCRIPT>

Then in your body:
<body onkeydown="return NoEnter(event);">

Hope this helps.
0
 

Author Comment

by:vikram_lashkari
ID: 11989161
where do i place this code,as you know that i had one login control and that control i had put on my one of the aspx page and in that aspx page there are other textboxes and command button.Now when i press enter key on any of the textboxes on the aspx page the event of the button on the Login Control is fired.I need to stop firing that event unless i press the sign in button.It should not fired automatically when ever i press enter in anywhere in the aspx page,so is there any property like in Vb 6.0  "default" property ,by disabling this property we can stop the firing of the command button.

Plz help me
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 11989203
put the script in your <head></head> tags on  what ever aspx page you don't enter to be to be pressed
0
 

Author Comment

by:vikram_lashkari
ID: 11989213
I donot want to use javascript i want to implement through the .net technology,is there any way but .net which i can do.

plz tell me
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 11989238
not that i know off, i've only ever used javascript to do what you want to do, if you look about experts exchange similar questions to this have been asked and everytime the javascript is the best suggested way.
0
 

Author Comment

by:vikram_lashkari
ID: 11989267
so ,u mean i use this javascript on every aspx page where i had place the login control to stop the firing of the command button of the login control when enter key is pressed any where in the aspx page.

And ,plz give me the "search keywords did u used to find the similar question in the Expert Exchange".If i found that javascript is suggested by experts then i will definately accept your answer.

thanx
vikram lashkari
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 11989348
I use headers and footers on some of my projects so i only need to put the code in the header and include that on every page, this stop me copying and pasting the bit of code onto every page.

I didn't search for  similar questions, i just remember seeing a few question that were similar, and they had javascript answers.  Try doing a search for something like 'enterkey' bit vague, but you'll probably find loads of results and i'm sure if my answer is no good for you there could be an alternative one.

I'm not saying it can't be done with .net becuase i don't know if this is true, but i certainly have no idea how to do it with .net  i've only ever used javascript for things like this soo just thought id see if it was any good for you.

If you do find a .net solution would you let me know i would me most interested.
I hope you get it sorted.  

Thanks
Nate
0
 

Author Comment

by:vikram_lashkari
ID: 11989367
thanx nate,
    i will search for solution in .net and if i donot find it then i have last alternative left is to accept your answer.


vikram lashkari
0
 

Author Comment

by:vikram_lashkari
ID: 11990423
hi nate,
    i am now using your javascript and paste the above javascript in the head section of the asp.net page and in the body i added the code for the onkeydown which you had given me,but still if i m pressing the enter key in any of the text field the event in the user control for the command button is firing,i must tell you that i had added the login user control in the asp.net page  so i think it is not filtering the onkeydown of the body as,even though i put the code and onkeyDown code the same thing is happening ,plz tell me how do i stop executing the event in the control added to the aspx page.

plz help me
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 11990468
i noticed a bug in the code i gave you:
change this:
<SCRIPT language=""javascript"">

to:
<SCRIPT language="javascript" type="text/JavaScript">

this code i sent you before should make it work in most browsers, this one (below) will only work in IE i think:

in between the <head></head> tags:
<script language="JavaScript" type="text/JavaScript">
<!--
function NoEnter() {
if(event.keyCode==13){
event.keyCode = null;
return;
}
}
// -->       
</script>

and your body code should then be:
<body onkeypress="NoEnter();">

hope this helps.
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 11990491
hmm, i also noticed i sent you this:
<body onkeydown="return NoEnter(event);">

for the version that works in most browsers, try replacing it with the below also:
<body onkeypress="return NoEnter(event);">


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:vikram_lashkari
ID: 11990559
Oh One problem, even if i press enter key on the button which is on the aspx page it is not doing anything as we had suppressed all the enter key event,i have to forcefully do the mouse click on the form which i have made in the aspx page.

It is quite rigid for the user to use the mouse click to submit the form,plz give me the code how can i filter my command buton name "Continue" ,so that if user press enter key on this "continue" button on the aspx page the event of this button should execute, Every thing else is working fine,like the event on the login user control button has stopped firing,but along with that event my original command button event is also stopped.

plz help me out from this akward situation

thanx
vikram
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 11990605
ahh, right, sorry i was assuming you wanting to stop the enter key from being pressed altogether.

you could take the onkeypress event out and put in the control on the text boxes and buttons etc that would only stop the enter key from being pressed on the control and the rest of the page should be fine.  Now you got it working your best bet is to play arround with the onkeypress event, take it of the bodyand try it in different places, just have a play see which works best for you.
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 11990753
you could also use something like this in your codebehind:
myTextBox.Attributes.Add("onKeypress", "NoEnter(event);")

    or

myTextBox.Attributes.Add("onKeypress", "NoEnter();")

dependent on which bit of code your using.

0
 

Author Comment

by:vikram_lashkari
ID: 11994683
hi nate,
 
       i will try this and let u know,wether it works for me or not.
0
 

Author Comment

by:vikram_lashkari
ID: 12005011
please give me the full code for this as you know there is no body element in my login control so where do i put this code and where do i put this code for

myTextBox.Attributes.Add("onKeypress", "NoEnter(event);")

    or

myTextBox.Attributes.Add("onKeypress", "NoEnter();")


plz tell me
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 12005062
myTextBox.Attributes.Add("onKeypress", "NoEnter(event);")
    or
myTextBox.Attributes.Add("onKeypress", "NoEnter();")

goes in your codebehind for your control, i'd probably put it in the Page_Load

if you didn't want to do that, keep the code in your header and footer or on the pages you need it and then put the bit below on each textbox or button, dropdown etc you need:
onkeypress="return NoEnter(event);"
   or
onkeypress="NoEnter();"

obviously it depends on which script you are using.

Hope this helps
0
 

Author Comment

by:vikram_lashkari
ID: 12005209
hi nate,
  this is working but it is quite long method as i have my login control on every page and i have to disable the onkeypress event of all the textboxes ,combo etc.Plz tell me such thing which i can do in the login control to stop the default execution of the OK button in that control.Now in this method which u suggested to me is that,i have to change all the pages ,and there r 100's of fields in my project and it is quite lengthy process,

Plz suggest me some way by which my default execution of the command button "ok" in the login control is stopped.

Plz help me out,i had increased points to 500.

SOS
Vikram Lashkari
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 12005461
Ok if i was doing it i would make a header file that i would include on all the pages that needed the javscript bit.

that way you are going to get around have to put the script on every page you'll only need to insert the one line on the pages that need the script (you could do this with find and replace if you have lots of pages to insert the line into).

ok now in your logon control you need to put the onkeypress bit on all your textboxes etc within that page.
Thats it for your logon control

now if you want to add this to other textboxes, combo boxes etc you don't have to change every page, you could do the stuff in the code behind for any textboxes etc you want to have this function on.

If for example you have 100 text boxes on a page and you want to add the function for all the textboxes you wouldn't have to do 100 lines of:
YOURTEXTBOXNAME.Attributes.Add("onKeypress", "NoEnter(event);")

you could just do a foreach textbox and that would add the attributes to your textboxes, something like this i think:
foreach (TextBox txtBox in this.Controls)
{
      txtBox.Attributes.Add("onKeypress", "NoEnter(event);")
}

or

foreach (TextBox txtBox in this.Controls)
{
      txtBox.Attributes.Add("onKeypress", "NoEnter();")
}

again dependent on which script your using.
obviously though if you only have single items then the one line is no real hassle, and for multiple comboboxes etc, it would be pretty much the same foreach loop just changing this bit:
foreach (TextBox txtBox in this.Controls)

to:
foreach (DropDownList drpList in this.Controls)
0
 

Author Comment

by:vikram_lashkari
ID: 12005659
hi nate,

    I know about foreach loop , but i am asking you the suggestion for login control as there is problem in the login contol not in  other pages,why the button in the login control is invoked, why can't i change the default invoking of the "ok" button on the login control. I need to change some setting  on the login control or "ok" button on login control.

 I want to know that how can i stop invoking the button_click event of the Login control when i press enter key in the aspx page which contain the login control. What is the relationship between the aspx page and login control that is resposible for invoking button_click of the login control.You know i have command button on my aspx page also why didn't they are invoked when i press enter key on the textbox on the aspx page.Actually the button_click() event of the aspx page has to be invoked if i press the enter key in any of the textbox on the aspx page,but in my case the button_click() event of the Login Control is fired as soon i press the enter key in the textbox in the aspx page which contain this login control.How can i get rid of these default firing of the event on the user control.

plz help me ,as this is the logical problem i donot want to use the inncorrect way to solve this logical problem.

thanx

Vikram Lashkari
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 12005771
ok, let me try and get this right.
You are including the control on many pages.

Your never going to completely get around this i don't think unless you completely stop the enterkey being pressed, this is because if you don't include the onkeypress event in your form or body then you can just click andywhere on the page press enter and it will fire your button_click event.  ( i thinks thats right ).

If you want your button to work when you press enter you simply don't have the onkeypress event on the buttons.

I don't know why its using the button on your control as the button_click to fire, i think probably because its the first button it comes accross?!?! if you want to invoke other buttons etc you simpy put the onkeypress event in them. or use the code behind bit.

I've never been forced to allow the enter button to be pressed in anything i've done so i've never allowed it to be pressed period except for on the buttons them selves which is fine.  I was hoping you would be able to use the code i gave you and adapt it someway to fit your needs, maybe its not going to work for you?!?!

0
 

Author Comment

by:vikram_lashkari
ID: 12005858
ya nate,
 
   I found your suggestion ok,but as a developer i want to know how can i write code in user control,so that the default firing of the button_click() of the user control is stopped, when i press enter key anywhere in the aspx page's textbox.

There is nothing wrong in your suggestion,but for me as a programmer i want to know what is the property or what kind of code i can write in the user control which can disable the default button_click() fire ,when enter key is pressed in the container's element(i.e textbox of aspx page).

Plz, try to understand me,

thanx
vikram
0
 
LVL 3

Accepted Solution

by:
nhewitt21 earned 500 total points
ID: 12006005
ok i get what your saying.

So you want to stop the button_click from firing when you press enter inside a textbox from on an aspx page.  The only way would be to put that onkeypress event on the textbox, that is literally the only way i can think off.

If you notice when you made you control it doesn't contain any body tags or anything like that.  I think how the control works when you call it on your page is wherever the place holder is for your control it will literally put you .aspx code in that space and treat it like its the one page, thats how i think it works but to be honest i don't know, someone else might know more about that.

So what your gettin when the page renders every time is just the one page, so your not going to be able to supress the enter key unless you include that onkeypress on everyything you want.  Thats why at the start i made the assumption you wanted to block the enter key altogether and hence the onkeypress in the body tags.

Sorry, i hope you understand me.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now