Gosh I have been busy of late, hardly any time to blog these days. However, Friday afternoon always seems to give me a little time to write and a week of work usually turns up something worth blogging about.AspDotNet

This week (surprise, surprise) I will touch on some jQuery but the main focus is an issue I had with the asp:Login control.

If you have used the Membership goodies in asp.net you will know that they can save you a whole heap of time and effort but I had a little gotcha this week that had me scratching my head for a while.

The issue I had was that of clicking the Login button once the user had pressed the enter button. It would just not fire off the click event.

Now this issomething that you expect to see on page, a default button, and something I have blogged about recently and in the past. Check out my previous post Default buttons made easy ala jQuery

The trouble is, when using the asp.net login components there is no Click handler on the button (an anchor tag in this case)

image

You might expect to see something like this:

class="sprite login-large" id="LoginButton" onclick="Login();">

But what you actually get is:

"javascript:WebFormDoPostBackWithOptions(new WebFormPostBackOptions("ctl00$MainContent$LoginUser$LoginButton", "", true, "LoginUserValidationGroup", "", false, true))"class="sprite login-large" id="LoginButton">

Now this isn’t that surprising really as .net is doing a lot of stuff behind the scenes to handle the authentication process… so let’s notgoall Kick Ass onasp.net because it is saving us a whole loadof coding.

However, if you try and wire up something like this with jQuery to handle the Enter key being pressed nothing will happen:

$("input").keypress(function (e) { if ((e.which && e.which 13) || (e.keyCode && e.keyCode 13)) { $("#LoginButton").click(); } });

There is no click event to fire!

So what to do? I Googled and Binged a plenty for a solution but they all seemed pretty complex and convoluted. I mean come on, all I want to do is tell the button to do the same thing is would do if I actually pressed it. How hard can it be???

Well in actual fact not hard at all and the answer was staring me right in the face.

The ‘href’ attribute that asp.net generates on the anchor tag has everything we need right there.

All I actually had to do in the end is add a dab of jQuery/javascript and it worked like a charm:

$("#LoginButton").click(function () { window.location = $("#LoginButton").attr("href"); });

I have wired up my own Click event handler to the LoginButton anchor tag and that in turn will use window.location and set it to the ‘href’ content contained in the anchor tag.

There it was, the answer staring me right in the face all that time Smile

BondiGeek