POST-TITLE-HERE

Posted by Author On Month - Day - Year

POST-SUMMARY-HERE

POST-TITLE-HERE

Posted by Author On Month - Day - Year

POST-SUMMARY-HERE

POST-TITLE-HERE

Posted by Author On Month - Day - Year

POST-SUMMARY-HERE

POST-TITLE-HERE

Posted by Author On Month - Day - Year

POST-SUMMARY-HERE

POST-TITLE-HERE

Posted by Author On Month - Day - Year

POST-SUMMARY-HERE

Set DefaultButton to LinkButton control in ASP.NET

Posted by Sweet Heart On 2:12 PM
ASP.NET has great limitation — only one server form on the page. Even if you are developing complex page, which looks like several forms with different submit buttons, in fact you have only one form. So we have a problem: How browser understanding which button should be triggered when user pressed ENTER? It uses first Button control (usually that’s wrong), and if you have LinkButton’s, they would never be triggered. In ASP.NET 2.0 new property has been added to the Panel and HtmlForm controls — DefaultButton, which can be used to specify ID of the control, which implements IButtonControl interface (usually Button and LinkButton). This button control would be triggered when user pressed ENTER. But there is one big problem exist: LinkButton control would not be triggered in Firefox on ENTER. In this article I will show why this problem take place and how to solve it.

Let’s look how DefaultButton is working in ASP.NET. Here is simple example:









When you will try to press ENTER button inside the text box, form will be submitted to the server, but LinkButton’s Click event will not be fired. Let’s try to specify DefaultButton property of the panel:



Great! It’s working perfectly in Internet Explorer, but what about Firefox? Oops, we found a problem. You could find solution at the end of the post, but first I want to show why this problem occurs. When you specify DefaultButton property, ASP.NET generates following code:




Here is WebForm_FireDefaultButton method, defined in ASP.NET JavaScript library:




The problem occurs in the code typeof(defaultButton.click) != "undefined" — Firefox does not define click() method for the a element, so the form will be submitted without __EVENTTARGET parameter, and button will not be triggered. The simplest way to solve the problem is to define click() method for the a:




Our click() method imitates a element behavior: first it calls onclick() method (OnClientClick attribute of the LinkButton), and if it returns false — stops processing. Then it evals href attribute (where ASP.NET puts __doPostBack call).

Let’s create custom control which encapsulates this logic




And corresponding .aspx file:



Of course, JavaScript code should be moved out from the C# code, but for my example it’s good enough. If you are using ASP.NET AJAX library, you should use ScriptManager.RegisterStartupScript method instead of ClientScriptManager.RegisterStartupScript.

Hope, this post will help you. Your comments are welcome.

0 Response to "Set DefaultButton to LinkButton control in ASP.NET"

Post a Comment

    Featured-video

    Tag-cloud