ASP.NET Atlas DefaultButton bug?
A buddy of mine works for Virtual Bank. They were working on an internal application that is based on ASP.NET/ATLAS. They were setting the defaultbutton attribute at the form level to a button they had on the form and it was only firing the OnClick event the first time it ran.
He called me up and asked if I could help solve there issue. I recreated the problem by creating a simple web application and dropping a button on it. I set the defaultbutton attribute and ran the application so I could see what was going on behind the scenes.
The client code that was produced was the folloing:
<form name="form1" method="post"
action="Default.aspx"
onkeypress="return WebForm_FireDefaultButton(event, 'Button1')"
id="form1">
I noticed the call to the javascript function WebForm_FireDefaultButton. So I saved the website to the hard drive. Two files are created in the subfolder with the same name of the page you saved. Inside the folder you will find two files. One of them will be named "WebResource.axd". I opened this file and found the code to WebForm_FireDefaultButton. The code looks like the following:
var __defaultFired = false;
function WebForm_FireDefaultButton(event, target) {
if (!__defaultFired && event.keyCode == 13 &&
!(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
var defaultButton;
if (__nonMSDOMBrowser) {
defaultButton = document.getElementById(target);
}
else {
defaultButton = document.all[target];
}
if (defaultButton &&
typeof(defaultButton.click) != "undefined") {
__defaultFired = true;
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
return true;
}
You will notice that __defaultFired is being set to true when the function is fired. So here is the kicker. With ATLAS it only updates sections of the code.
In the old postback model this works fine because everything is reset. In ATLAS this variable is not being reset because of the intelligent updates. So we have to create a work around. This is what I suggested and it seemed to work great. I told my friend to create a client side JavaScript function called ResetDefault and reset the __defaultFired variable to false manually. Then call that by adding it to the button using the OnClientClick attribute. So the two parts look like this:
<script type="text/javascript">
function ResetDefault()
{
__defaultFired = false;
}
</script>
<asp:Button ID="Button1" runat="server"
Text="Button"
OnClientClick="ResetDefault" />
This works everytime you hit the enter key on the page now. I just figured I would share this in case somone else runs into the same issue. If there is a better way, please let me know. This seems to be a bug? There might be a proper way to do default buttons with ATLAS and I am just not aware of it yet due to my lack of experience using it.
Take care