XYZPDQ

3/27/2009

Response.Redirect inside of a Try/Catch

Ok, first off, let me state, that I am not entirely certain WHY I did this in the first place.  I am going to say I was rushed and wasn’t thinking clearly.  However, I glazed over it and moved on and when it came time to test, I was getting bizarre behavior that didn’t throw an error.

Session[“var”] = “”; 

try 
{ 
    Session[“var”] = “Good Value”;

    Response.Redirect(“newpage.html”); 
} 
catch(Exception ex) 
{ 
    Session[“var”] = “Bad Value”; 
}

In the code above, Session[“var”] will ALWAYS equal “Bad Value”.  Why you may ask?

Response.Redirect throws a ThreadAbortException.  … Fun, no?

If you are building a URL inside of a try block that you want to then redirect to, declare a string, build your url, and then pass that on to the Response.Redirect statement. 

For Example:

string _url;

try 
{ 
    _url = “yourpage.aspx?var=” + iffyMethodCall(); 
} 
catch(InvalidOperationException ex) 
{ 
    _url = “error.html”; 
}

Response.Redirect(url);

So, If you ever run into odd behavior in a site you are working on and when debugging, your code goes straight past your Response.Redirect and into the catch block and the debugger starts giving you cryptic messages, this may be what you’re seeing.

3/21/2008

WPF: Element State Binding

I've been working with WPF lately.  Partly for work, but mostly to teach myself while at the same time being productive.  I am definitely one of those people that learns best by doing.

I have been working on adding some encryption to a application recently and I am making a form that will allow the user to enter a password that will be used for the encryption/decryption process.

encrypt.jpgOn the form that I made, I had a checkbox to determine whether they even want to use encryption at all, as well as two password boxes for the password and the confirmation.  I wanted to make it such that when they clicked the checkbox that it would enable the password fields and disable them when the checkbox was unchecked.  Essentially tie the IsEnabled property of the PasswordBox's to the IsChecked property of the CheckBox.  

Well, I could tie a method to the Checked event of the CheckBox, and then adjust the PasswordBoxes accordingly, or I could do it the WPF way and use Binding to tie the elements together to one another.

Opting for the latter (since I am trying to learn after all), I dug around in my WPF book and in short order, had the answer.

Assuming that the CheckBox is named checkbox1:

<PasswordBox Name="Password1" Margin="0,5" Width="200"
IsEnabled="{Binding ElementName=checkbox1, Path=IsChecked}"/>

What is happening here is that WPF is telling the IsEnabled property to watch checkbox1 and specifically the IsChecked property of that control and grab the value from there.

Pretty cool stuff.

5/7/2007

Silverlight Airlines

There are a lot of great examples of what Silverlight is going to be capable of. Silverlight Airlines is just one among many. Check out David Anson blog on it which includes a live link and of course full source code! Also, be sure to head over to Lutz's site and check out several of the samples that he has written as well.

5/1/2007

Silverlight, CoreCLR, DLR, oh My!

Wow... so lots has been going on lately. Adobe is trying to encroach on Microsoft's turf with Apollo and in reply, here comes Silverlight. Now, Silverlight by itself is alright. I am not going to profess that it is going to kick Flash to the curb by any means. To be honest, it has a ways to go before it will be a major competitor against the offering that Adobe already has out there. However, given that Flex is still being adopted for RIA development if Microsoft focuses their efforts with Silverlight to that front, then they will most likely succeed. They will succeed due to the sheer mass of people developing against their technologies. The big announcment that a lot of people seem to be overlooking in the big Flash vs. Silverlight debate is that beneath Silverlight lies something so massive that it could change things all by itself. CoreCLR. CoreCLR is a trimmed down CLR with all the goodies of the larger .Net Framework that runs cross platform. So what? Microsoft has done some cross platform stuff in the past and ditched it. I don't see that happening in this case however. I think Microsoft is finally starting to come around on the fact that Windows, while holding a massive amount of the market share, is not the end-all-be-all when it comes to things like... the internet. Microsoft is realizing that it is time to start competing against people like Adobe/Macromedia who have ruled the internet for quite a while now, who have gotten so cocky with their market share on the internet side of things that they have started working toward a desktop takeover. The other nice little inclusion with Silverlight is the DLR or Dynamic Language Runtime. The DLR allows Ruby, Python and other traditionally NON-.Net languages to be compiled into IL code so that they can utilize the CLR. This is being done with respect to the language as well. i.e. Microsoft is not trying to assert their way of doing things into these community run languages, but rather are taking the accepted ways of doing things and incorporating those into the DLR. If that wasn't enough, Microsoft has released the DLR to the community so that it can be built upon and more languages can be added in over time. There are too many things to list here and to be honest, since I am not at Mix and am relying on News and Blog Postings to get all of my information, I am going to quit writing for the time being and go continue reading. I would highly recommend reading Scott Hanselman's post on the subject though I leave you with this: It is going to be interesting to watch what happens over the next year, between Adobe and Microsoft, things are going to move even further off of the desktop and this is going to cement the RIA into the mainstream environment. I am not going to say that one technology is better than the other at this point and I am going to concentrate on learning both Flex/Apollo and Silverlight/WPF over the next few months so that I can be proficient in both, because much like the battle between BlueRay and HD-DVD, this is just getting started. Just don't write Microsoft off because they are behind. They aren't the underdog by any means. They've been sitting back and watching, silently plotting, and they've just made their move.