Command Button click when enter key is pressed

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
vrao92Asked:
Who is Participating?
 
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
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
 
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
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
 
vrao92Author Commented:
Yes..exactly..how Do I do that?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
Correction to above: I could not clearly understand your requirements.
0
 
hehdaddyCommented:
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
 
themathprofCommented:
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
 
vrao92Author Commented:
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
 
vrao92Author Commented:
I am using vb.net.
0
 
hehdaddyCommented:
You should try my solution. It is very easy.
0
 
guru_samiCommented:
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
 
guru_samiCommented:
sorry VB won't have ; at the end of statement...
         
 form1.DefaultButton = "Button1"
0
 
vrao92Author Commented:
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
 
vrao92Author Commented:
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
 
guru_samiCommented:
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
 
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
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
 
baiju_nagoriCommented:
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
 
vrao92Author Commented:
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
 
baiju_nagoriCommented:
Hey vrao92,

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

Baiju
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.