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.