Easily Move Objects From .NET to Javascript
For those of you who are (un)fortunate enough to do web development you will probably run into the issue of wanting to move a .NET DTO and be able to access it in JavaScript. The simple way to do this is to use JSON (I know this is nothing new but a lot of people have not heard of this technology). JSON stands for the JavaScript Object Notation and is a simple string representation of an object that can be parsed using the JavaScript eval() method. Simply the way this works is that a .NET library (I am using...
Debugging Javascript
One thing I have noticed is that there are numerous ways to debug javascript with visual studio. I thought I would share some of them that I have found over the years.In internet explorer you can go to view->script debugger->Break
at next statement. This will cause the debugger to run when the next
javascript statement runs in the browser. This is not a feature that is
enabled by default. To enable it you will need to go to the advanced
tab in internet options and uncheck the "Disable Script Debugging"
option and restart IE. This is the method I use most often. Force javascript to...
~= friend
One of those nice little things not many people know about is the use
of a ~ character in relative URLS. The ~ allows you to access the
document root.i.e. <a href="~/showlog.aspx">Show the log</a> If this was in a virtual directory called sampleapp the url would render to behttp://localhost/sampleapp/showlog.aspxbut when deployed to a production and not in a sample app it would behttp://www.sampleapp.com/showlog.aspxif we just had a link without the tilde like this:i.e. <a href="/showlog.aspx">Show the log</a>then it would work in production but in development it would render to the root of the serveri.e. http://localhost/showlog.aspxinstead of the vdir like we...
Sending a file to the client
Sometimes you need to send a file to the client by them clicking a
link. If it is stored on your hard drive (appologies for the bad
formatting. busy day today):private sub SendFile(fileName as string)dim filepath as string = Server.MapPath("files") & fileNamefilepath = Server.UrlPathEncode(filepath) 'encoded to remove special characters i.e. spacesresponse.clear() 'clear the output bufferResponse.ContentType = "application/binary"
'binaries are almost always downloaded. If it was set to excel for an
excel file the browser might try to open it in the browser windowResponse.AddHeader("Content-Disposition", "attachment;filename=" + fileName) 'This is what tells the browser the filename to prompt the user with when they save it...
Determining if a session has expired
The first thing to discuss is Session.IsNewSession() this returns true if a session was just created on this request.On the first request to a page this will return true.durring the session it will return falseafter a timeout error occurs this will also return true (the session no longer exists so it gets recreated).So we know that if IsNewSession() returns true that either this is a new request or the session has timed out.Now
the way that sessions work is the server sends the browser a cookie
with the session ID. On every request the client sends this cookie and
the server loads...
Why not to depend on client side validation
A great example on why not to depend on client side validation only:http://thedailywtf.com/forums/65974/ShowPost.aspx
Javascript rounding errors
I have this in code:var total = parseFloat(txtGLItemAmount1.value) + parseFloat(txtGLItemAmount2.value)when I insert 110.16 and 7.74 I should get 117.90 but instead get 117.8999999999Pretty
freakin weird! I assume this has something to do with floating point
math and all its weirdness. The fix I found for this was this line:total = total.toFixed(2);This yeilds the correct value. I bet that I could do this as well to get it correct:var total = txtGLItemAmount1.value.toFixed(2) + txtGLItemAmount2.value.toFixed(2);
Cool web tool
It has been a long time since I saw a peice of software (Especially a
plugin) that makes me go "Wow how did I ever live without this / why
didn't I think of that?". I have only played with the IE Dev toolbar
for 5 mins now and I had to write about it.1. Allows you to hightlight all tables or table cells or divs on a page2. Show the DOM of a page in a handy little treeview3. Easily disable Cache, Images, Cookies, script, or the popup blocker4. Validate a pages HTML, CSS, XHTML, Feed, links, and some other stuff I...
I finally caved to AJAX
I had played a little with AJAX (Async Java and XML) but I finally
caved and started to use it in an app. I must say that it is pretty
freakin sweet but I am still concerned (as always with javascript) how
it will behave in different browsers and operating sysystems.<script>;function CreateRequestObject(){if (navigator.appName == "Microsoft Internet Explorer") return new ActiveXObject("Microsoft.XMLHTTP")else return new XMLHttpRequest();}var http = CreateRequestObject();function getData(){ http.open('get', 'info.ext') http.onreadystatechange = handleResponse http.send(null);}function handleResponse(){ if (http.readyState == 4) { var data = http.responseText; var arr = data.split(""); alert(arr[0]); }}</script><a onclick="getData()" href="#">get stuff</a>Now
this is a basic example but one thing that I have done...
Validator Controls and IE
I developed a little app last night that used the builtin ASP.NET
validators. Now that I am running firefox as my default browser I have
discovered that the validators client script does not work in anything
other than IE. The reason for this is that the BaseValidator class (all
validation controls derive from this class) have methods the emit
javascript to do the validation. This script also contains an array of
validator controls and summaries that looks like this:var Page_ValidationSummaries = new Array(document.all["ValidationSummary1"]);var
Page_Validators = new Array(document.all["RequiredFieldValidator1"],
document.all["RequiredFieldValidator2"],
document.all["RequiredFieldValidator3"],
document.all["RequiredFieldValidator4"],
document.all["RegularExpressionValidator1"],
document.all["RequiredFieldValidator5"]);And therin lies the
problem. Only IE uses document.all every other browser I know off does
not recognize document.all. Instead what should...