Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Command Button click when enter key is pressed

Posted on 2009-05-11
18
Medium Priority
?
879 Views
Last Modified: 2012-06-21
I have a webform(vb.net) and it has a grid control with 2 button in item template(edit,delete).
I also have 2 command button outside the gridview. One is to add a new row in gridview and the other is to "return to a previous webpage". I want the "return to previous page" click event to be fired when the user presses "enter key".
How do I do that?

Thanks
0
Comment
Question by:vrao92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +3
18 Comments
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24357426
I could clearly understand your requirements. Do you wish that the button to go to previous page be activated when the Enter is pressed on this button (and mouse clicking be disabled for this button) OR Enter is pressed anywhere on the page?
0
 

Author Comment

by:vrao92
ID: 24357432
Yes..exactly..how Do I do that?
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24357433
Correction to above: I could not clearly understand your requirements.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Assisted Solution

by:hehdaddy
hehdaddy earned 300 total points
ID: 24357490
Hi there. If your button lives inside a panel control, you can set the default button on the panel control and that should fix your issue.

<asp:Panel ID="ExamplePanel" runat="server" DefaultButton="MyDefaultButton">
...Content goes here...
<asp:Button ID="MyDefaultButton" runat="server" Text="Default" />
</asp:Panel>

That's a simple illustration.
0
 
LVL 1

Expert Comment

by:themathprof
ID: 24357554
Say your Grid Control is Grid1 and the Command Button
outside your grid is Command1. Add this code to the
grids keypress event

Private Sub Grid1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Command1_Click
End Sub
0
 

Author Comment

by:vrao92
ID: 24357577
The grid ,button and all teh controls are in a Content control called content1.

Will the content can be set as default button?
Thanks
0
 

Author Comment

by:vrao92
ID: 24357592
I am using vb.net.
0
 
LVL 6

Expert Comment

by:hehdaddy
ID: 24357727
You should try my solution. It is very easy.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24357749
in the Page_Load ... set the default button property of your Form like this:
         
form1.DefaultButton = "Button1";

form1 === ID of your form element
"Button1" === ID of your Button that takes your to pervious page.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24357758
sorry VB won't have ; at the end of statement...
         
 form1.DefaultButton = "Button1"
0
 

Author Comment

by:vrao92
ID: 24357763
Hehdaddy.. I tried creating a pael and I have placed the grid control and the command button inside the panela nd tried your code. It is still not working.
0
 

Author Comment

by:vrao92
ID: 24357837
guru_sami:
I have one master page and several pages underneath that. This is one of the page and in this page I have a gridview and a button.

I do not see DefaultButton property? where should I see that?
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24358861
There might be a better way but one I could get is in code below.
Pag Load of your content Page you set the default button and default focus of your Button.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
            Form.DefaultButton = Button1.UniqueID
            Form.DefaultFocus = Button1.ClientID
        End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24359217
When I asked explanation above of your requirements, you did not come with a clear answer. I am still confused if:

You wish to restrict the user on the client side so he/she would not be able to click the button with mouse or press any key other than Enter. So the button for "return to previous page" will submit the request to server (click event fired on server side ASP.NET code) only when the Enter key is pressed on that button.

OR

You wish that whenever Enter key is pressed anywhere in any control of the page, the button for "return to previous page" be pressed that will ultimately submit the request to the server (click event fired on server side ASP.NET code).

If any one of the above is actually your requirement, it is more of a JavaScript job rather than server-side ASP.NET although you might have to set the JavaScript code from within the ASP.NET code. Let me know if it is what you wish to have, and I may be able to post some sample code/references to solve your problem.
0
 
LVL 4

Assisted Solution

by:baiju_nagori
baiju_nagori earned 300 total points
ID: 24359707
Hi

Try this

function GoBack(e) {
    var keynum;
    if(window.event) {    //IE
        keynum = e.keyCode;
    }
    else if(e.which)  { // Netscape/Firefox/Opera
        keynum = e.which;
    }
    if (keynum==13) {
        document.getElementById('yourbutton').click();
    }
}

<form  onkeydown="GoBack(event);" >


</fomr>
0
 

Author Comment

by:vrao92
ID: 24365422
moghazali:
I am sorry My post is not clear.
I need this to happen..
 whenever Enter key is pressed anywhere in any control of the page, the button for "return to previous page" be pressed that will ultimately submit the request to the server (click event fired on server side ASP.NET code).

Thanks again

commandButtonProblem.txt
0
 
LVL 4

Expert Comment

by:baiju_nagori
ID: 24366987
Hey vrao92,

Did you try my code? It will work whenever Enter key is pressed anywhere on the page.

Baiju
0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 900 total points
ID: 24367613
Please see the attached snippet.
Either directly use the JavaScript code in ASPX file OR Use the VB version. The code assumes that the button ID for "return to previous page" is btnSubmit. This code can surely be enhanced with more efforts.
I hope this helps.

//USE THIS JS CODE DIRECTLY
<script language="JavaScript">
	function goToPreviousPage(e)
	{
		var source, code;
		if (!e) var e = window.event;
		if (e.target) source = e.target;
		else if (e.srcElement) source = e.srcElement;		
		if (e.keyCode) code = e.keyCode;
		else if (e.charCode) code = e.charCode;
		
		if (code == 13)
		{
			if( source.type == "button" && source.id != "btnSubmit" )
				source.click();
			else
				document.getElementById("btnSubmit").click();
		}
	}
	document.onkeypress = goToPreviousPage;
</script>
 
'OR THIS VB CODE
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
 
	Dim strScript As String = "function goToPreviousPage(e){var source, code;if (!e) var e = window.event;if (e.target) source = e.target;else if (e.srcElement) source = e.srcElement;if (e.keyCode) code = e.keyCode;else if (e.charCode) code = e.charCode;if (code == 13){if( source.type == "button" && source.id != """ & btnSubmit.ClientID & """ ){source.click();}else{document.getElementById(""" & btnSubmit.ClientID & """).click();}}}document.onkeypress = goToPreviousPage;"
 
	Page.ClientScript.RegisterClientScriptBlock(Me, Me.GetType(), strScript, True)
 
End Sub

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses

609 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