Thursday, March 31, 2011

ASP.NET Page Life Cycle Overview

"When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. It is important for you to understand the page life cycle so that you can write code at the appropriate life-cycle stage for the effect you intend."

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Thursday, March 24, 2011

Friday, March 18, 2011

Using jQuery with Other Libraries

I had a situation recently where I was using jQuery 1.5.1 and it was conflicting with another menu control library since both use "$" as a shortcut. I had no control over the menu control library, but fortunately there was a quick and easy way to work around the issue by overriding the $-function in jQuery.

<script type="text/javascript" language="javascript">
   var $j = jQuery.noConflict();

   function txtCity_onChange(CountyClientID) {
      $j("#" + CountyClientID).val("");
   }
</script>

http://docs.jquery.com/Using_jQuery_with_Other_Libraries

Monday, March 14, 2011

Firefox; The state information is invalid for this page and might be corrupted.

I ran into an issue recently where a web application was throwing a System.Web.HttpException in Firefox with this message:

The state information is invalid for this page and might be corrupted.

It was easily reproduced in Firefox 3.6.13 and 3.6.15 when the user would do something on the page to cause a postback (e.g. click a button), and then use the browser's back button to go back to the previous section, and then use the browser's refresh button to refresh the page, and then click a button to cause another postback.

It seems this issue is only encountered in Firefox for ASP.Net websites that utilize ViewState and has the "autocomplete" form option enabled. In which case, Firefox attempts to "autocomplete" the form with values after the browser's refresh button is clicked, thus causing the ViewState to become invalid.

In my case, the solution was turning off the "autocomplete" attribute on the form:

<form id="form1" autocomplete="off" runat="server">
   ...
</form>

Although there are other solutions to turn "autocomplete" off at the input level, if you don't want to turn it off for the entire form.

Here are some articles that I referenced along the way during my research.

http://msdn.microsoft.com/en-us/library/ms533032(v=vs.85).aspx

http://ryanfarley.com/blog/archive/2005/02/23/1739.aspx

http://www.hanselman.com/blog/TurningOffAutoCompleteForTextBoxesInIEAndFireFox.aspx

http://lachlankeown.blogspot.com/2008/04/firefox-refresh-viewstate-updatepanel.html

http://ra-ajax.org/firefox-retains-form-element-values-on-refresh-including-the-viewstate-in-asp-net

https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion

Wednesday, March 9, 2011

Checking All CheckBoxes in a GridView Using Client-Side Script and a Check All CheckBox

By Scott Mitchell

"In May 2006 I wrote two articles that showed how to add a column of checkboxes to a GridView and offer the ability for users to check (or uncheck) all checkboxes in the column with a single click of the mouse. The first article, Checking All CheckBoxes in a GridView, showed how to add "Check All" and "Uncheck All" buttons to the page above the GridView that, when clicked, checked or unchecked all of the checkboxes. The second article, Checking All CheckBoxes in a GridView Using Client-Side Script and a Check All CheckBox, detailed how to add a checkbox to the checkbox column in the grid's header row that would check or uncheck all checkboxes in the column. Both articles showed how to implement such functionality on the client-side, thereby removing the need for a postback."

"The JavaScript presented in these two previous articles still works, but the techniques used are a bit antiquated and hamfisted given the advances made in JavaScript programming over the past few years. For instance, the script presented in the previous articles uses server-side code in the GridView's DataBound event handler to assign a client-side onclick event handler to each checkbox. While this works, it violates the tenets of unobtrusive JavaScript, which is a design guideline for JavaScript programming that encourages a clean separation of functionality from presentation. (Ideally, event handlers for HTML elements are defined in script.) Also, the quantity of JavaScript used in the two previous articles is quite hefty compared to the amount of code that would be needed using modern JavaScript libraries like jQuery."

"This article presents updated JavaScript for checking (and unchecking) all checkboxes within a GridView. The two examples from the previous articles - checking/unchecking all checkboxes using a button and checking/unchecking all checkboxes using a checkbox in the header row - are reimplemented here using jQuery and unobtrusive JavaScript techniques. Read on to learn more!"

http://www.4guysfromrolla.com/articles/120810-1.aspx

Sunday, March 6, 2011

ASP.NET 4 GridView EditIndex

"Prior to version 4 of ASP.NET the GridView’s EditIndex property was not automatically set when firing the ‘RowEditing’ event. ASP.NET 4 is now automatically setting the EditIndex property, which is affecting backward compatibility in converted projects. The only way to prevent the grid from now going into edit mode on a row is to set ‘e.Cancel=True’ in the ‘RowEditing’ event"

http://www.craigwardman.com/blog/index.php/2010/10/asp-net-4-gridview-editindex/

http://connect.microsoft.com/VisualStudio/feedback/details/554166/gridview-sets-editindex-property-when-in-previous-net-versions-it-didnt

.NET Framework 4 Migration Issues

"This topic describes migration issues between the .NET Framework version 3.5 Service Pack 1 and the .NET Framework version 4, including fixes, changes for standards compliance and security, and changes based on customer feedback. Most of these changes do not require any programming modifications in your applications. For those that may involve modifications, see the Recommended changes column of the table."

http://msdn.microsoft.com/en-us/library/ee941656.aspx

Saturday, March 5, 2011

"Failure sending mail" error message when you send an email message by using a .NET Framework 4.0-based application that uses the "System.Net.Mail.SmtpClient" class if the email attachment is larger than 3 MB

I recently had an issue after upgrading a website from .Net 3.5 to .Net 4.0, particularly when trying to send an email attachment larger than 3 MB using the System.Net.Mail.SmtpClient class.

System.Net.Mail.SmtpException:
Failure sending mail.

System.IndexOutOfRangeException:
Index was outside the bounds of the array.

System.IndexOutOfRangeException stack trace:
at System.Net.Base64Stream.EncodeBytes(Byte[] buffer, Int32 offset, Int32 count, Boolean dontDeferFinalBytes, Boolean shouldAppendSpaceToCRLF) at System.Net.Base64Stream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Mime.MimePart.Send(BaseWriter writer) at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer) at System.Net.Mail.SmtpClient.Send(MailMessage message)

Turns out this was a known bug with a hotfix already available:

http://support.microsoft.com/kb/2183292

https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=30226

Some other articles I found along the way:

http://stackoverflow.com/questions/2803132/net-4-0-fails-when-sending-emails-with-attachments-larger-than-3mb

http://connect.microsoft.com/VisualStudio/feedback/details/544562/cannot-send-e-mails-with-large-attachments-system-net-mail-smtpclient-system-net-mail-mailmessage