Solved

Command Button click when enter key is pressed

Posted on 2009-05-11
18
874 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Assisted Solution

by:hehdaddy
hehdaddy earned 100 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 100 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 300 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

786 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