Tuesday, April 5, 2011

CSS Causing a Crash on Webkit Browsers

This absolutely blew my mind today. Turns out a piece of CSS code can cause a 30 second pinwheel and/or crash in Safari and Chrome on a Mac. This is extra crazy, because it works fine on all browsers on a PC, including IE6. At first I thought it was an issue with my JavaScript code or something wrong with JQuery, but after spending a whole boat load of hours on this problem, turns out the culprit was 1 line CSS.

The following line caused crashes and freezes for me: background-size: 100%;

This was set on a div in a code that I inherited.
div.searchFilter {
 background: #FFFFFF url(../images/search_options_bg.png) 0 0 repeat-x;
 width: 762px;
 margin-top: 0;
 display: none;
 border: 1px solid #b6ac9f;
}

I hope this saves someone some agony, because it drove me insane.

Tuesday, March 15, 2011

Sitecore and FieldRenderer

I get work with Sitecore quite a bit, and even though I do like the product itself, my biggest gripe is that the online documentation is sparse and not easily searchable. In particular, I could not find the documentation for FieldRenderer format parameters anywhere. These are extremely useful for formatting Date and Image fields.

First include WebControls namespace to use FieldRenderer:
using Sitecore.Web.UI.WebControls;

Next, use the following overload for the Render method:
FieldRenderer.Render(Item item, string fieldName, string parameters);

As far as the parameters argument goes, this where I had to dig endlessly to figure out the right string every time I use the functionality. Here is the info I was able to come up with, which will hopefully save you some time:

1. Date Formatting, add format= and then formatting string you want to use
FieldRenderer.Render(item, "Date", "format=MM/dd/yyyy")

2. Images - one thing to note, URL format is used to combine parameters together.
FieldRenderer.Render(item, "Date", "w=500&h=700&as=1")

For images the list of options is significantly larger:

w: - Width
h: - Height
mw: - Max Width
mh: - Max Height
la: - Language
vs: - Version
db: - Database
bc: - Background Color
as: - Allow Stretch
sc: - Scale (.33 = 33%)

Tuesday, March 1, 2011

WPF - Passing Arguments to Timer.Tick

Recently I worked on a projected that needed to animate objects based on a timer. I wanted to write a generic timer handler to perform an operation on the object passed in to the timer. Initially I tried messed with EventArgs passed to the event handler, but turns out there is an easier way - using the "Tag" property:

// Note: You can have only one DispatchTimer object per class
private DispatcherTimer _timer;

MyClass()
{
    // Timer Start Code
    var myObject = new SomeObject();
    _timer.Interval = TimeSpan.FromMilliseconds(700);
    _timer.Tag = myObject ;
    _timer.Tick += TimerTickEventHandler;
    _timer.Start();
}

private void TimerTickEventHandler(object  sender, EventArgs e)
{
    var timer = (DispatcherTimer)sender;
    var myObject = (SomeObject)timer.Tag;
    // now you can use myObject
    timer.Stop();
}

Monday, February 28, 2011

Using Code Behind For Property Binding in WPF

This is a very common thing in WPF, but after being away from WPF for some time, I often have to look it up. In order to bind to properties in the code behind, you need to set the DataContext of the window to "self" as per example below:

<Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

        <Label Content="{Binding StatusText}"/>
</Window>

Now you can bind to a dependency property like this in your code behind:

public string StatusText
{
    get { return (string)GetValue(StatusTextProperty); }
    set { SetValue(StatusTextProperty, value); }
}

public static readonly DependencyProperty StatusTextProperty =
    DependencyProperty.Register("StatusText", 
typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));

Thursday, February 24, 2011

Returning a Captcha Image as a Result from ASP.NET MVC

Recently I was trying to generate a simple Captcha for ASP.NET MVC. I looked at MVC ReCaptcha, which is a great tool, but I didn't want a confusing ReCaptcha for simple form validation. My goal was to use something very simple to prevent SPAM, but at the same time provide a seamless enough experience to not scare off human users.

Next, I came across this article - Simple CAPTCHA, Create your own in C#. It provides an excellent example on how to generate a simple Captcha. My next task was to convert this to ASP.NET MVC code (I used ASP.NET MVC 3.0). Documentation on this might be a little confusing, but the process ended up extremely simple.

The key that I learned here, is that you need to return FileContentResult. Initially I tried returning FileResult with no success.

    public class CaptchaController : Controller
    {
        public ActionResult GetCaptcha()
        {
             var captcha = GetCaptchaImage();
             var result = new FileContentResult(captcha, "image/png");
             result.FileDownloadName = "captcha.png"; // optional
             return result;
         }
         
         
          private Byte[] GetCaptchaImage()
          {
             // Code to generate your Captcha
             // I used the logic from Raymund Macaalay's blog
             // linked above
             ... 

             var oBytes = oMemoryStream.GetBuffer();
             oMemoryStream.Close();
             oMemoryStream.Dispose();
           
             return oBytes;
         }
}

Once you have this code, you can reference your image from views:

        <img src="/Captcha/GetCaptcha/anything_you_want.png" />

One more thing to note, you can put any file name at the end. This part gets completely ignored by your controller (unless you modify it to take a parameter and do something with it). And that's it!

Tuesday, February 22, 2011

Sitecore 6 and IIS6 form postback causes layout redirect

Recently, wile working on a legacy website build on Sitecore 6.0 and IIS6, I ran into a problem with form posts / post back. On postback, instead of seeing the actual page, I was seeing the layout page being posted to. This threw a 404 error.

After a bunch of research, it turned out that the issue was a missing App_Browsers folder. That folder contains Form.browser file, essential for the postback to work properly.

Thanks to Neil Pullinger's blog entry, I was able to get postbacks working correctly.

Monday, February 21, 2011

Finally - a dark color scheme for VisualStudio2010 that doesn't suck!

I've been using a dark theme for VS2010 for a while now, but especially with new releases of technologies such as ASP.NET MVC and Razor view engine, I started to notice that the scheme becomes unreadable and tough to deal with in certain situations. Finally someone stepped up and found a solution to this problem.

My favorite theme is Rob Conery's take on the Vibrant Ink for TextMate, but because it was released some years ago, it's lacking some of the new feature support. I found an updated version of the theme as well as a gold mine for VisualStudio themes in general - check out Wake Road Ink. For other themes check out Studio Styles.

Tuesday, February 15, 2011

VisualStudio 2008 crashes attaching to process for debugging - Fixed!

A lot of people have written about this HotFix but, I found that it's not easy to find, since the route cause is so obscure. I had a problem when VS2008 would crash when attaching to IIS process w3wp.exe. This would happen somewhat inconsistently, sometimes only once per session. Turns out that this is likely because I have my solution explorer window undocked. If you are like me, and you have multiple monitors or you just undock your solution explorer window to make more space for your code, you are likely experiencing the same problem. This HotFix solved it for me. I hope it helps someone else out as well:

KB960075 - VS Dev Environment crash after undocking windows or changing layouts

Monday, February 14, 2011

Displaying errors in PHP

If you are looking for a way to display errors in PHP on a server where you don't have access to the php.ini file, this is the best way to do it:

error_reporting(E_ALL);
ini_set("display_errors", 1);

I was doing some development on a shared hosting environment, and on error I was seeing blank white screen. The code above allowed me to see what's going on.

Friday, February 4, 2011

Update on T-SQL table from another based on ID

This is a simple SQL script I have to use now and again, but often forget the right syntax for. It uses a join between two tables to update a specific column in one table with the values in the column of the other table.

UPDATE
    table_one
SET
    some_column = t2.some_column
FROM
    table_one t1
INNER JOIN
    table_two t2
ON 
    t1.some_id = t2.some_id

Tuesday, January 11, 2011

Playing video on iPhone SDK 3.2 and above

I was trying to figure out for a while what is the best way to play a video inside an iPhone application. I wanted the video to play full screen and show the previous view once it is done. After many tries, this is the code that worked best for me:

NSURL *videoUrl = [NSURL URLWithString:url]; 
MPMoviePlayerViewController *mp = 
    [[MPMoviePlayerViewController alloc] initWithContentURL:videoUrl];

if (mp)
{
 mp.moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 
 mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
 [mp.moviePlayer play];
 
 [self presentMoviePlayerViewControllerAnimated:mp];
 
 [mp release];
}