Posts

Showing posts from 2009

ASP.NET Cached Items Being Removed

Lately some of the items I've been placing in cache in ASP.NET have been being removed. When I assigned a callback method to trap these removals, I saw that the reason for removal was "underused" -- ASP.NET was removing these items to free up memory because they weren't being used often. To prevent this from happening, use the following value for the priority parameter of Cache.Add() : CacheItemPriority.NotRemovable

HttpRuntime.Cache vs. HttpContext.Current.Cache in ASP.NET

Lately I'd been using the static Current property of the HttpContext object in ASP.NET to access it's Cache property and cache information in a web app. I was adding these cache entries with dependencies, so that when certain files were modified, the cache would trigger a callback function to reload. However, when testing this, I ran into an unforseen complication: when I modified a file I had specified as a dependancy, and the app tried to add the new data to the cache using HttpContext.Current.Cache , it couldn't because there was no current HttpContext . The solution was to use HttpRuntime.Cache instead. This, like HttpContext.Current.Cache , represents the cache of the entire app, but does not require a reference to a current HttpContext . Also, I've heard it's a little bit faster.

"Object reference not set to an instance of an object" Exception When Using A DetailsView Control In ASP.NET

I ran into an exception recently when developing a website in ASP.NET which made use of a DetailsView control bound to an ADO.NET Entity Data Model as its data source (EntityDataSource). The complete exception message was: [NullReferenceException: Object reference not set to an instance of an object.] System.Web.UI.WebControls.EntityDataSourceView.ConvertWCProperty(IDictionary values, Dictionary`2 convertedValues, List`1 visitedProperties, PropertyDescriptor pd, ParameterCollection referenceParameters, Dictionary`2& exceptions) +35 System.Web.UI.WebControls.EntityDataSourceView.ConvertProperties(IDictionary values, PropertyDescriptorCollection propertyDescriptors, ParameterCollection referenceParameters) +216 System.Web.UI.WebControls.EntityDataSourceView.CreateEntityForInsert(EntityDataSourceWrapper entityWrapper, IDictionary values, ParameterCollection insertParameters) +49 System.Web.UI.WebControls.EntityDataSourceView.ExecuteInsert(IDictionary values) +239 System...

jQuery AJAX Calls Not Working In IE8

I recently discovered that some jQuery code I had written for making AJAX calls was not working in IE8, even in Compatibility Mode. Turns out the reason why is that IE8 caches AJAX GET requests. Changing the calls to POST instead of GET corrected the problem. I hope this helps anyone else having this same issue.

How To Sort Elements By ID In jQuery

I recently came across an issue wherein I needed to be able to sort a set of HTML elements by their IDs. I was retrieving a wrapped set with jQuery, but didn't know how to sort them. I found a blog post by a guy named Bill Richards which can be found here . This put me on the right track and allowed me to solve this issue. My solution is a little different than his, but it was his post that showed me the solution to this problem and saved me a lot of time. Also, a comment left there by a guy named Tom helped as well. Thanks guys! To sort elements with jQuery, all you need to do is create a function that will do the actual sorting (comparing two elements) and then pass the name of this function to the sort() method (using jQuery 1.3.2 or higher). Here is a function which will sort elements by ID: function sortByID(a, b) {    return a.id > b.id ? 1 : -1; }; To use this function to sort a wrapped set, you can call it inline with your code which creates the set by passing t...

How To Set The Value Attribute Of An ASP.NET RadioButton In Server-Side Code

Recently I ran into a situation wherein I needed to set the value attribute of a radio button rendered via an ASP.NET RadioButton control -- except I found there was no Value property. I attempted to set it via the InputAttributes property of the control, but this didn't work either -- if I set it using the value key, my value would be ignored and replaced with the server-side ID of the control (which is what ASP.NET uses as the value attribute by default), and if I added a value attribute this way, it would add an additional value attribute, giving me two in the same control. The solution was actually a simpler approach: merely setting the value using the control's Attributes property (not InputAttributes ). Here's an example: rdoButton.Attributes["value"] = "somevalue";

How To Get The Selected RadioButton From A List Of Element IDs Using jQuery

I've been doing some work with jQuery lately and now that I've begun digging into it am truly impressed at how much you can do with it so easily. Here's a quick line of code that will take a string of element IDs belonging to RadioButtons, find the one that's selected, and display an alert box with it's value: alert("Selected radio button value: " + $(radioButtonIds).filter(":checked").get(0).value); The format of the string containing the RadioButton IDs would look something like this: "#rdoFirstChoice, #rdoSecondChoice, #rdoThirdChoice"

Crazy Internal Server Error When Developing With WCF and AJAX with jQuery

I've been doing some development lately involving calling WCF services from client-side script using jQuery's ajax function, and ran into some strange behavior. I had some code written that would run successfully without error, but then added some additional, un-related code, which would also attempt to call a WCF service via jQuery and AJAX. The original, working code remained untouched. The new code would be unsuccessful -- however, the old, previously working code would now also fail. The error was Error 500, Internal Server Error . Stopping and restarting the development server didn't solve the issue, nor did exiting and restarting Visual Studio. Even rebooting my machine didn't help. It just didn't make any sense. I finally resolved the problem by deleting the Temporary ASP.NET files for the site. By default, these are located in C:\WINDOWS\Microsoft.NET\Framework\(Framework Version)\Temporary ASP.NET Files , in a sub-folder matching the name of your site. Not...

How To Easily Convert Strings to Mixed Case (aka Title Case, aka Proper Case) in C#

I found a very easy way to convert strings to mixed case (aka Title Case, aka Proper Case) in C# today. The String class does not have a method to do this, but the TextInfo class of the System.Globalization namespace does -- the ToTitleCase() method. An important thing to note is that it doesn't work on strings that are all upper case, so convert the string to lower case first to make it work. See the example below, where sTheStringYouWannaConvert represents the string you want to convert (go figure): string sMixedCaseString = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(sTheStringYouWannaConvert.ToLower()); It's just that easy. :)

Another Instance of IE vs Firefox vs Standards

Here's another example of behavioral differences between IE and Firefox. I ran into this one about a month ago and then again this morning. I currently have some webpages where I'm dynamically displaying and hiding controls. This is done via JavaScript/DOM by setting the style.display property of the control I wish to hide or display. To hide a control, setting this property to none does the trick, but to display it, there's a way that works only in IE, and a way that works for both IE and Firefox -- which would be the correct way of doing it. In IE, setting the style.display property to block will re-display the control after it has been hidden, and will display it correctly. But in Firefox, doing this will display the control again but in a different way -- for example, the height of the control as it was before it was hidden is no longer maintained. The correct way of doing this is to set the style.display property to an empty string. Apparently, this is the ...

Z-Index Not Working In Firefox

Even though I've been using Firefox a lot more than IE lately (especially with the speediness of Firefox 3.5), I finally made Firefox my default browser today to assist in doing more proper (i.e. standards-friendly) web development. Firefox is stricter when it comes to standards, whereas IE is more like the parent that lets you stay up past your bedtime or eat junk food after brushing your teeth -- it's lenient and more flexible -- but this isn't a good thing. Anyway, my decision to make Firefox the default browser was justified today when I encountered an issue with z-indexes: certain elements were appearing underneath others, and even though I set the z-index of these elements, there was no change. Then I realized that it worked in IE -- because IE is lenient. According to the CSS spec, z-index only applies to positioned elements (i.e. those whose position property has a value other than static). IE was lenient with this, but Firefox was not -- it was requiring the positi...

ASP.NET HiddenField Values Modified By JavaScript Not Being Persisted During Postback

Here's a quick fix to a problem I ran into today: I have a UserControl in .NET which includes a HiddenField control. I use a JavaScript function to modify the value of the control (getting the control with document.getElementById() and passing in the ClientID of the HiddenField control), and then make use of the value later in my server side code during postbacks. The problem I ran into was that the value wasn't being persisted -- however, it used to work. I realized that because I had moved the code that made use of this value into the Page_Init event handler a short time earlier, this had broken the functionality. I moved it to Page_Init because I'm doing dynamic control creation, and I've read in countless places that the preferred location for this is Page_Init. Moving this code back to Page_Load corrected the issue -- in the ASP.NET Page Lifecycle, the controls values have not been restored from ViewState during Page_Init.

How to Get the ASP.NET LinkButton CssClass Property to Work

Hopefully this will save someone a lot of time. Today I was working on a webpage in ASP.NET and made use of the LinkButton control. But when I applied a CSS class using the CssClass property, it just wouldn't take. After much searching, I found the solution. First off, this is a bug -- you can't spin it any other way. Fortunately, there IS a way to get it to work: you need to specify one of the following qualifiers on your CSS class: visited, link, or active . For example, when I tried to apply the following class, it wasn't reflected in the control: .Pager { font-family:Verdana; font-size:8pt; font-weight:bold; color:Orange; } However, when I added the link qualifier (":link" added to the class name), it worked like a charm: .Pager:link { font-family:Verdana; font-size:8pt; font-weight:bold; color:Orange; } When you apply the class name to the CssClass property of the control, leave out the qualifier . For example, using my class above, I set the valu...