<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sentia &#124; Sydney IT Consultancy, Software Development, Ruby on Rails, Web Application Development, Rails Development, Test Driven Development, Microsoft.Net, Asp.Net , Agile, Continuous Integration Training, iPhone development &#187; asp.net</title>
	<atom:link href="http://www.sentia.com.au/category/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sentia.com.au</link>
	<description>Sentia company website and blog about all things development, Ruby on Rails, Microsoft .Net, ASP.Net, C#.Net, Agile web development, Test Driven Development</description>
	<lastBuildDate>Thu, 02 Feb 2012 07:16:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Fixing the enter key in ASP.NET with jQuery</title>
		<link>http://www.sentia.com.au/2009/03/fixing-the-enter-key-in-aspnet-with-jquery/</link>
		<comments>http://www.sentia.com.au/2009/03/fixing-the-enter-key-in-aspnet-with-jquery/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 22:44:31 +0000</pubDate>
		<dc:creator>Michael Cindric</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.sentia.com.au/?p=100</guid>
		<description><![CDATA[One of the fundamental problems with ASP.NET WebForms is the &#8230; <a href="http://www.sentia.com.au/2009/03/fixing-the-enter-key-in-aspnet-with-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the fundamental problems with ASP.NET WebForms is the fact that you can only have one form per page. This jumps up and bites you when you have a set of fields and a button and you want the enter key to submit the form.</p>
<p>In ASP.NET 2.0 they came out with a &#8220;fix&#8221; for this. You can wrap your fields in a Panel and set the default button:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;asp:Panel ID=&quot;_pnlLogin&quot; DefaultButton=&quot;_btnLogin&quot; runat=&quot;server&quot;&gt;
    UserName: &lt;asp:TextBox ID=&quot;_txtUserName&quot; runat=&quot;server&quot; /&gt;
    Password: &lt;asp:TextBox ID=&quot;_txtPassword&quot; runat=&quot;server&quot; /&gt;
    &lt;asp:LinkButton ID=&quot;_btnLogin&quot; Text=&quot;Login&quot; runat=&quot;server&quot;/&gt;
&lt;/asp:Panel&gt;
</pre>
<p>Some of you will know limitation here. This works with a Button, but not a LinkButton. Let&#8217;s see why.<br />
Here is the resulting HTML:
</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;_pnlLogin&quot; onkeypress=&quot;javascript:return WebForm_FireDefaultButton(event, &amp;amp;#x27;_btnLogin&amp;amp;#x27;)&quot;&gt;
    UserName: &lt;input name=&quot;_txtUsername&quot; type=&quot;text&quot; id=&quot;_txtUsername&quot; /&gt;
    Password: &lt;input name=&quot;ctl00$mainContent$_txtPassword&quot; type=&quot;text&quot; id=&quot;_txtPassword&quot; /&gt;
    &lt;a id=&quot;_btnLogin&quot; href=&quot;javascript:__doPostBack('_btnLogin','')&quot;&gt;Login&lt;/a&gt;
&lt;/div&gt;
</pre>
<p><br/>The Panel renders as a div, and on any key pressed will call WebForm_FireDefaultButton: </p>
<pre class="brush: jscript; title: ; notranslate">
function WebForm_FireDefaultButton(event, target) {
    if (!__defaultFired &amp;amp;&amp;amp; event.keyCode == 13 &amp;amp;&amp;amp; !(event.srcElement &amp;amp;&amp;amp; (event.srcElement.tagName.toLowerCase() == &quot;textarea&quot;))) {
        var defaultButton;
        if (__nonMSDOMBrowser) {
            defaultButton = document.getElementById(target);
        } else {
            defaultButton = document.all[target];
        }
        if (defaultButton &amp;amp;&amp;amp; typeof(defaultButton.click) != &quot;undefined&quot;) {
            __defaultFired = true;
            defaultButton.click();
            event.cancelBubble = true;
        if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}
</pre>
<p><br/>This method checks for the enter key and then calls click on the default button. All good except for one thing: the LinkButton renders as an &#8220;a&#8221; tag.  Anchor tags don&#8217;t have a click() method unfortunately. At least not in Firefox.</p>
<p>So let&#8217;s fix it! I&#8217;ve recently been reading <a href="http://www.learningjquery.com/" title="Learning jQuery">&#8220;Learning jQuery&#8221;</a><br />
  which is a great book on this <a href="http://jquery.com/">cool javascript library</a>. I&#8217;ll use jQuery instead of straight javascript as it is much easier to write cross browser code and it&#8217;s a lot more concise. Plus there are heaps of plugins for it, and it&#8217;s a pretty small download for your page.</p>
<p>I want to improve the markup of my page at the same time so I&#8217;ll try to minimise the amount of server controls I use and get rid of that inline javascript call. My new aspx looks like this:</p>
<pre class="brush: xml; title: ; notranslate">&lt;div class=&quot;form&quot;&gt;
    UserName: &lt;asp:TextBox ID=&quot;_txtUsername&quot; runat=&quot;server&quot; /&gt;
    Password: &lt;asp:TextBox ID=&quot;_txtPassword&quot; runat=&quot;server&quot; /&gt;
    &lt;asp:LinkButton ID=&quot;_btnLogin&quot; Text=&quot;Login&quot; runat=&quot;server&quot; CssClass=&quot;form_submit&quot;/&gt;
&lt;/div&gt;
</pre>
<p><br/>So if I have a div with the class &#8220;form&#8221;, the default submit button is the first element with the &#8220;form_submit&#8221; class. Nice, simple and unobtrusive.</p>
<p>Here&#8217;s the javascript I ended up with:</p>
<pre class="brush: jscript; title: ; notranslate">
$(document).ready(function(){
    var $btn = $('.form_submit');
    var $form = $btn.parents('.form');

    $form.keypress(function(e){
        if (e.which == 13 &amp;amp;&amp;amp; e.target.type != 'textarea') {
            if ($btn[0].type == 'submit')
                $btn[0].click();
            else
                eval($btn[0].href);
            return false;
        }
    });
});
</pre>
<p>Basically it&#8217;s going to find any .form_submit elements, find a parent form, and add a keypress handler for the form. When the keypress is triggered, if it&#8217;s the enter key and we&#8217;re not in a textarea, we&#8217;ll click the default submit button if it IS a button, if it&#8217;s a link we&#8217;ll Eval the href attribute, which is going to be our javascript postback call.</p>
<p>Cool, I can keep it tucked away in an js file include, and i&#8217;ve improved my markup.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sentia.com.au/2009/03/fixing-the-enter-key-in-aspnet-with-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

