Saturday, February 9, 2008

Make Google Black and White


Have you ever dreamed about seeing how the Google logo looks in black and white (or grey scale). Well, there is a chance for you. Just go to google.com and type "make google black and white" then press "I am feeling lucky." And if today is your lucky day, the Google logo will switch to black and white :). Actually it should switch anyway.



Google log in black and white


So how does that work? It turns out that there is a web site that totally mimicks the Google web site look in a signed off state. But it has a logo in black and white. More importantly, this site happens to be the first result on Google search when you search for "make google black and white." Pressing the "I am feeling lucky" button has the same effect as going to the first link in the search results. So when you press the button you are actually going a new site but since everything looks the same except for the logo, it looks like as if Google understood what you wanted and changed its logo like magic. But sorry not this time. No magic there.

Monday, February 4, 2008

Submitting a Form upon Pressing the Enter Key


It is very common on web forms that a page is desired to submit its form when a user presses the Enter key while the focus in a certain or any text box. This is typically done using JavaScript. I want to share how it's done as it can be very useful. In essence all that is done is that every time a user presses a key, a JavaScript code checks if the key pressed is the Enter key. If so it submits the form to the server, otherwise it does nothing. The following is a snippet of the code would look like:




On the JavaScript side


function enter_submit(pevent)
{
if(window.event) //Internet Explorer
{
if(myEvent.keyCode == 13) //check enter was pressed
{
document.formname.submit();
//or __doPostBack("",'');
return;
}
}
else if(pevent.which) // Netscape/Firefox/Opera
{
var pressed = pevent.which;
if(pressed == 13)
document.formname.submit();
//or __doPostBack('','');
}
}



On the HTML or ASP .NET side



<asp:Textbox ID="txtPassword" runat="server" onkeypress="javascript:enter_submit(event);/>

<input id="inPassword" type="password" onkeypress="enter_submit(event);"/>

Tuesday, January 22, 2008

asp:Button and Javascript

It is useful many times to decide the behavior of a button based on some client side code result. For example, when a button is pressed a web page might need to execute Javascript code to validate some user input or to have the user confirm a certain action and based on the output of the client code either post back to the web server or ignore the click or execute more client side code.

Let's take the scenario of asking the user to confirm a certain action as this scenario has actually come up with me at a client. The web page has a button that deletes an object permanently. Upon a user click on the button, the page executes Javascript code to open a confirmation box with an OK and a Cancel button. The confirmation box should at the least minimize accidental deletions. If the user clicks OK the page posts back and the object is deleted. However, if the user clicks Cancel the button click is ignored and nothing happens. Let's take a look at the code behind this:

Javascript part:

<script type="text/javascript">
function confirmDelete() {
return window.confirm("You really want to delete all your work?");
}
</script>


ASPX side:

<asp:button id="btnDelete" onclick="btnDelete_Click" runat="server" text="Delete" tooltip="Permanetly remove this item" onclientclick="javascript:return confirmDelete();" />


That's it.

Free Online Silverlight Training Resources

I wanted to share some free online resources for the new Microsoft web technology Silverlight.

Microsoft Press E-Book: Introducing Silverlight 1.0
http://csna01.libredigital.com/?urvs5cn3s8

visitmix.com Firestarter:
http://www.visitmix.com/University/silverlight/firestarter/

Lynda.com Training:
http://movielibrary.lynda.com/html/modPage.asp?ID=473

On another note, Microsoft has extended the deadlines for 3 beta exams.
From: Trika msdn blog

The dates for the following three betas have been extended and you are welcome to spread the word. taking a beta exam is free. If you pass it, you have passed that exam (and earned the related certification, if applicable)--you do not need to take the live version of the exam. if you've never taken a beta exam before, read this. If you have questions, check out Gerry's Blog, I'm sure he'll be posting this info too and he knows more about it than YT.


Link to registration: http://www.register.prometric.com/ClientInformation.asp

· Not available in China, India, Pakistan
· Need to stress there’s no guarantee of a seat

Sunday, January 6, 2008

Submitting Files to Multiple Sites

An issue came up recently at my client where the client wanted to keep a backup copy of every file submitted by a user to a third-party site. The backup copy was going to be saved a separate server. The solution I implemented was some simple JavaScript code. Essentially, the code submits the form to one site then changes the "action" and "target" properties of the form and submits it again to the other site. The new target could be an HTML iframe or a new window. In my case it was a small popup window. Here is a sample of the code:


function submitForms() {
window.open("", "smallPopup", "location=1, status=1, scrollbars=1, width=100, height=100");
var theForm = document.myForm;
theForm.action = "http://firstSite.com/backup";
theForm.target = "smallPopup";
theForm.submit();

theForm.action = "http://2ndSite.com/receive";
theForm.target = "_self";
theForm.submit();
}

Now you might wonder why I didn't just create another form and have two forms, one for each server. The problem with that is the user would have to select the file twice, once for each form. This is because for security reasons, it is not possible to programmatically set the file path in an HTML input of type file. So a programmer is prevented from assigning the path of one input tag to be the same as another input tag of type file. A programmer only has read access to the file path. Of course this makes a lot of sense too because if it were possible then any malicious site would be able to upload any file its owner wishes from your computer to his server.