In my previous post I mentioned the one way to set default button in ASP.NET. There are few other methods also. One of them is to use asp panel and set its DefaultButton property. When we need to set multiple default buttons on single page we can do it by using asp panels. Lets consider that you have login control and search control on same page. User must be redirect to proper page while he press enter on search and login control. Lets look at following code,
Fig – (1) Default Button with asp panel in ASP.NET
You can add multiple asp panel and set default button for each panel. I found this as the easiest way to set multiple default button in asp.net.
Lets look at how default button works. First step is to observe the rendered HTML,
Fig – (2) HTML code for Default Button with asp panel in ASP.NET
ASP panel is rendered as DIV. You may have observer that onkeypress event is added to DIV, onkeypress events calles WebForm_FireDefaultButton(event, ‘btnSearch’) function. ASP.NET automatically includes WebForm_FireDefaultButton function when default button property is set. WebForm_FireDefaultButton takes button’s ClientID as one of the argument and second argument is event. As Click is the default event of button, button click event will be fired on post back.
Now take a look at WebForm_FireDefaultButton function,
Fig (3) – WebForm_FireDefaultButton function included with default button
When user press the enter key while the focus is set to any control within DIV it calls WebForm_FireDefaultButton function.
Few interesting articles you may like to read,
Happy Programming !!!