Solved

Command Button click when enter key is pressed

Posted on 2009-05-11
18
869 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

705 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

18 Experts available now in Live!

Get 1:1 Help Now