Forum Moderators: open

Message Too Old, No Replies

.Net button click code behind

how to make it run for enter key?

         

RossWal

6:24 pm on Dec 31, 2002 (gmt 0)

10+ Year Member



Say I have a web form with a single button, "Search". Its serverside onClick event is SearchIt(). When the user types in their search criteria and hits enter, the form is submitted, the onPageLoad executes, but the SearchIt code doesn't. I can't simply move the SearchIt code into the page load function because page load executes for other actions, such as sorting columns on a data grid.

I'm suprised the Seach button doesn't have a design time property making it the default button for the form. Anyone know a way to force a button click based on a whap on the enter key?

Thanks,
Ross

wardbekker

2:21 pm on Jan 1, 2003 (gmt 0)

10+ Year Member



Hi Ross,

When a form is submitted Page.IsPostBack is true, you can than call the SearchIt() method.

RossWal

4:58 pm on Jan 2, 2003 (gmt 0)

10+ Year Member



Thanks for the thought. I had tried that but there are other 'PostBack' actions on the form too (such as datagrid column sorts). I only want SearchIt to execute for a click on the search button or a press of enter key, but filtering on isPstBack doesn't exclude the column sorts.

I also tried calling the Search.click() from the form's onSubmit event in javascript but that didn't do the trick.

wardbekker

5:04 pm on Jan 2, 2003 (gmt 0)

10+ Year Member



Alternatively you can use the onSubmit action of the form. onSubmit calls a client-side javascript that fakes an Click event on the submit button.

andreasfriedrich

5:28 pm on Jan 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I´m baffled by the behaviour you describe. The form is submitted when the user hits enter. How does your script on the server side know whether the form was submitted by clicking the button or pressing enter? There is nothing in the HTML spec that would suggest that this information is supplied by the browser.

In whatever event handler gets called you could determine whether the form was actually submitted by checking whether the submitted data contain a control name/value pair for the submit button.

Andreas

tomasz

7:01 pm on Jan 2, 2003 (gmt 0)

10+ Year Member



this should work if this only one button on the page and it is not a child control i.e. button inside of the panel.

RossWal

9:39 pm on Jan 2, 2003 (gmt 0)

10+ Year Member




Alternatively you can use the onSubmit action of the form. onSubmit calls a client-side javascript that fakes an Click event on the submit button.

I'm not sure that I understand the fake click event, but I had tried this general idea without success. I have now gotten it to work with the following:

var go = false;
function forceButton(x) {
if (! go) {
go = true;
x.search.click();
return(false);
}
return(true);
}


I think it requires the two-pass approach in order to allow the onSubmit event to complete before it will fire the click() event. But I'm no expert on JS events.

In whatever event handler gets called you could determine whether the form was actually submitted by checking whether the submitted data contain a control name/value pair for the submit button.

This is apparently exactly what .net is doing when it decides whether to invoke the server side onClick function. The problem was the Enter keypress was not sending a name/value pair, and so the code behind the button was not executing.

Hopefully someone else will find the posted code useful. I'm still baffled as to why .Net doesn't allow a button to be set as the default for the page.

ralnikov

12:58 pm on Jan 6, 2003 (gmt 0)

10+ Year Member



Hi.
Default buttons component (http://www.metabuilders.com/Tools/DefaultButtons.aspx) is a good choice if you have a lot of controls and several buttons.