Page 1 of 1 in the ASPNet category
# Friday, June 03, 2011

A colleague showed me an interesting problem, the ASP:LinkButton controls in our ASP.Net 4.0 website wouldn't work when it was viewed from a browser control hosted inside an iOS app (ie. The viewer that pops up in the Facebook app when you click on a weblink) What made it weirder was that depending on the order you hit the webpage, either through the Facebook App first, then through Safari. The link button might or might not work.

We ran some test on an ASP.Net 2.0/3.5 site, and the LinkButtons on those sites worked fine. Baffled, I enabled trace.axd on the server to see if there were any differences between requesting for a page through the Facebook App, and through Safari. Immediately I noticed that the User Agent string was different.

When viewed through Safari the User Agent string was something like this.

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5

When the page was viewed through an app the User Agent looked like this.

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8H7

Notice that both the Version and Safari values are missing when viewed from the app.

In order to figure out what the difference was when these 2 values are missing, I fed the 2 UserAgent strings through IE9's developer toolbar and viewed the page on the desktop so I had more debugging ability. And to my surprise, when I used the UserAgent string without the Safari moniker, the following code was missing from the output page.

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

This happens to be the Javascript code which ASP.Net LinkButtons and various other auto postback controls will use to post the form through script on the page. So if the function was missing, then none of the controls would be able to post anything. So I begun my search on why the code was missing when the Safari moniker is missing. And I found the source of the problem, ASP.Net's feature for dynamically changing the way something is rendered based on the client's browser capability. Based on the sample shown on this page on how to see what browser capability was detected, I wrote a quick page which dumps the browser capabilities out with the code below.

        Dim ht As IDictionaryEnumerator = Request.Browser.Capabilities.GetEnumerator
        Response.Write(Request.Browser.Id)
        Response.Write("<br/>")
        Response.Write(Request.UserAgent)
        Response.Write("<br/>")
        Response.Write("----------------<br/>")
        While ht.MoveNext = True
           
            'If ht.MoveNext() = True Then
               
            Response.Write(String.Format("{0}:{1}", ht.Key, ht.Value))
            Response.Write("<br/>" & vbCrLf)
           ' End If
        End While

That's when I realized that WITHOUT the Safari moniker, the default browser capability DISABLES JAVASCRIPT that's why the clientside postback chunk was not delivered to the server.

Interestingly enough even without the Safari moniker, an ASP.Net 2.0 site will still flag the iPhone browser as Javascript capable.

Now that I knew what was missing, what was left was to make a new Browser Definition File which will enable Javascript for the ASP.Net 4.0 site even without the Safari moniker. I basically copied the definition for the Safari browser, but made it so it'll look for the AppleWebKit moniker in the UserAgent instead of the Safari moniker and flag it as Javascript capable.

<browsers>
  <browser id="safariiphone" parentID="mozilla">
    <identification>
      <userAgent match="AppleWebKit"/>   
    </identification>
    <capabilities>
            <capability name="version"                         value="${version}" />
            <capability name="majorversion"                    value="${major}" />
            <capability name="minorversion"                    value="${minor}" />
            <capability name="type"                            value="Safari${major}" />
            <capability name="ecmascriptversion"               value="3.0" />
            <capability name="javascript"                      value="true" />
            <capability name="javascriptversion"               value="1.6" />
            <capability name="w3cdomversion"                   value="1.0" />
            <capability name="tagwriter"                       value="System.Web.UI.HtmlTextWriter" />
            <capability name="cookies"                         value="true" />
            <capability name="frames"                          value="true" />
            <capability name="javaapplets"                     value="true" />
            <capability name="supportsAccesskeyAttribute"      value="true" />
            <capability name="supportsCallback"                value="true" />
            <capability name="supportsDivNoWrap"               value="false" />
            <capability name="supportsFileUpload"              value="true" />
            <capability name="supportsMaintainScrollPositionOnPostback" value="true" />
            <capability name="supportsMultilineTextBoxDisplay" value="true" />
            <capability name="supportsXmlHttp"                 value="true" />
            <capability name="tables"                          value="true" />
        </capabilities>
  </browser>
</browsers>

This file is saved as whateveryouwant.browser then placed under the App_Browsers folder of the website so it can be processed by ASP.Net. This fixed my problem but I was still still curious as to why it worked in ASP.Net 2.0 but NOT 4.0, so I dug around the Browser Definition Files for ASP.Net 2.0 and found this element it used to identify the Safari browser in ASP.Net 2.0

        <identification>
            <userAgent match="AppleWebKit/(?'webversion'\d+)" />
        </identification>

Which means that in ASP.Net 2.0 it's looking for the AppleWebKit moniker to identify Safari browsers, where as in ASP.Net 4.0 the identification element reads this.

        <identification>
            <userAgent match="Safari" />
            <userAgent nonMatch="Chrome" />
        </identification>

Seems like it was trying to avoid clashing with Google Chrome's Useragent which also happens to contain the AppleWebKit moniker but they ended causing problems in certain scenarios.


Friday, June 03, 2011 6:03:28 PM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [2]  | 
# Tuesday, October 14, 2008

My colleague pointed me at a Javascript library called JQuery today, he was tellling me that it was a neat library to do client side animations. I was a bit skeptical at first cause I didn't like how some of the libraries worked.

But after running through some tests just now I'm impressed, it's nice, simple and clean. Very straightforward to use and doesn't seem like it'll get in the way of our other scripting methods, including the ASP.Net Ajax Extensions. But I guess to be sure I'll need to go through it a bit more.

I guess it doesn't hurt that Microsoft feels it's pretty good too!

Hmmm.. ok… have to try out something one of these days.


Tuesday, October 14, 2008 2:35:11 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Saturday, July 05, 2008

Another interesting thing that happens during work. We receive some content to be entered onto a webpage from our copywriters, we copy the text from the mail, and paste it into our webpage. It looks fine when we pasted it in Visual Studio but then when other people look at it they see SOMETHING like this.

image

So you take a close look at the file and notice that where all the weird symbols were appearing is basically where an approstophe.. (Damn I can't spell it.. the ' in 's and 're)  is supposed to be, so the correct text should look like this.

image

So... what went wrong? Let see another example.

image

See the difference now? Still not yet? Look at the ', doesn't it look different between the 2 lines? Even the double quotes are actually different but they don't seem to be affected that much.

So... why is there a weird ' in the sentence? It's a very simple answer, just ask the person who prepared the document did he do it in MS Word? Now... dig into Word's auto correct options and you'll find something like this (varies depending on which version of Word you're looking at)

image

See the part about "Straight Quotes" with "smart quotes"? With that option on, basically when you use a non interesting " or ' Word will replace it with more pleasant looking ones, curvier ones, ones that look nicer when you print. But of course doesn't always work when you put it on a webpage, or a page designed to be viewed from a phone.

Just another one of the weird things you learn after developing web sites for a while


Saturday, July 05, 2008 1:32:50 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Friday, July 04, 2008

When you work with designers, it's very likely that sooner or later they'll come up with a web page design that uses multiple images that need to be joined together to form a display say something like this.

image

Where the yellow cell is actually where your content goes, and the cell below it is supposed to perfectly join up to the cell above to form a single unbreaking image. (Why does my sample suck? Because I can't draw! :P) But sometimes you'll end up with something like this. (The image below is simulated cause I can't get the error to show when I WANT it to! :P)

image

You'll see a gap between your supposedly perfectly joined cells (Usually about 1 pixel, I deliberately enlarged it so it's more visible right now).

Your HTML seems to be fine, with no additional cells, and the fact that you've already turned off cellpadding and cellspacing.

<table cellspacing="0" cellpadding="0" width="300">
        <tr>
            <td>
                <div style="background-color: yellow">
                    I'm in a TD
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <img src="footer.jpg" />
            </td>
        </tr>
</table>

So you're scratching your head wondering what went wrong. The next time you see a problem like this, remove ALL spaces between the TD tags. So essentially make your HTML look like this.

<table cellspacing="0" cellpadding="0" width="300">
    <tr>
        <td><div style="background-color: yellow">
                I'm in a TD
            </div></td>
    </tr>
    <tr>
        <td><img src="footer.jpg" /></td>
    </tr>
</table>

Why does it happen? I have no idea, but to fix the problem it's always the same thing. Remove all whitespace from the TD tags. If that still doesn't fix it or you're feeling paranoid, close up the TRs as well.

<table cellspacing="0" cellpadding="0" width="300">
    <tr><td><div style="background-color: yellow">
                I'm in a TD
            </div></td></tr>
    <tr><td><img src="footer.jpg" /></td></tr>
</table>

Again.. I have no idea why it just happens sometimes, but it does, and here's how I fix the problem. More little tips from the trenches to follow.


Friday, July 04, 2008 12:45:36 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Wednesday, November 28, 2007

I was deploying an ASP.Net 2.0 web application yesterday, after copying all the files in I browsed to the site and I got an error telling me that one of my handlers couldn't be found.

I immediately assumed that there was an error with the permissions of the Network Service user that was being used to run the website. Totally forgetting that if the Network Service user couldn't compile to the temporary ASP.NET folder the error would be an access denied writing to temporary folder.

After spending TOO MUCH time trying to figure out the permissions, my Project Manager came to me and asked me what was taking so long. I told him that there was something wrong with the service permissions and that the compiler was acting as if the files weren't there. My PM asked the question "Are you sure the files are there?" I replied affirmatively since I was sure I saw that there were files in the web folder, so I decided to show him and browsed to the web site's APP_CODE folder, it was EMPTY....

I did a quick check on all the other folders and sure enough... ALL THE .VB CODE BEHIND FILES WERE GONE!!!! Some how the fact that I right clicked dragged the ZIP file containing the web files from a network share and then directly extracting it using the built in Compressed Folders handler of Windows 2003 cause all the VB files to NOT be extracted.

Copying the ZIP file to the web server first and then extracting it from there solved the problem.

So I learnt to ALWAYS CHECK THE OBVIOUS LESS WORK INTENSIVE REASONS FIRST BEFORE ANY OTHER SOLUTIONS when something bad happens. But still a few questions exists... namely Why the heck were the files removed in the first place? It wasn't the antivirus cause it didn't log any deletes, then I repeated the same action on a similarly configured Win2k3 server but the VB files weren't deleted.

Weird.....


Wednesday, November 28, 2007 10:01:57 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [1]  | 
# Wednesday, November 14, 2007

Well, the security company came and went without much of a problem. So I guess I survived my first code review pretty well with no major problems reported.

Experience, and of course this book (absolute must read for ANY programmer) helped ensure that our code was pretty tight (yes it wasn't perfect, but we did very good).

Best quote from a member of the review team.

Generally .Net code has fewer security problems.

I know a few people who would love to have you make that an official statement. ;)


Wednesday, November 14, 2007 9:34:48 PM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Sunday, May 06, 2007
Ahhh.. how far we've come now in application developement. Things used to be so easy when I started delving into programming, back then an application was just a program that just ran on your computer and that was it, hmmm and if I started work back then I guess it be a lot of database applications since that was when everything started being digitized. After the announcement of Silverlight recently I think I'll now use this post to reflect on the different methods/types of application developement that are now available.. from a MS technology based perspective of course since that's what I'm most experienced in.

Sunday, May 06, 2007 11:56:26 PM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Saturday, April 21, 2007

When I first saw WPF/e in action, a thought ran through my head "Cool, this is MS's Flash like rich UI strategy where I'd be designing in XAML and coding the logic in any .NET language"

Now that WPF/e has been revealed as Silverlight, I've finally managed to sit down and take a closer look at it. And after my run throught the quick start and the various examples on the net, I realized something... Silverlight is NOT a Flash killer. Silverlight is not meant to be what Macromedia Adobe Flash is, Flash is an entire mini application platform (and a pretty nifty one at that) But Silverlight isn't that, Silverlight is more of a PRESENTATION technology than an entire platform. It doesn't even come with it's own script interpreter, all the logic is coded in Javascript (DAMN! I really really thought I could get away with .Net on this one) and the Silverlight plugin basically just handles the displaying of data.

This is exciting to me as a developer because now I have access to a rich presentation framework that I didn't have access to before (cause I didnt have time to polish up my Flash/ActionScript skills)

Confused as to why this is the case? Picture this scenario (ok.. I'd love to actually MAKE out the demo, but I've just started out here, so give me a break) Let's say I wanted to make one of those snazzy bar graph animations, say a list of mobile phones sold grouped by phone manufacturer. I want the bars to start extending from 0 to their values, and each bar to fade in following the complete extension of the previous one.

This animation is ridiculously simple to achieve in Flash, but the question is.. what if the data is not static? What if the data is supposed to come from a dynamic server generated source? Ah.. yes.. we can just use Flash's capability of performing HTTP GETs to a server page and grab a CSV file containing the values! But then what if the data is complex? We could probably just use XML to represent then data, and then walk through it with Flash's DOM explorer. But then.. here comes a huge problem, your Flash designer with his l33t actionscript skills doesn't understand the concept of how XML files work, he can't grasp the concept of an XML Element, Attribute, etc. etc. As a developer we'd waste time trying to hammer out a way to paste data to the Flash designer, while the Flash designer tries to work out how to read our data.

How does this change with Silverlight? First the designer will create the template of the animation  (he'll be DESIGNING which is what he's good at.) And then the developer will take the XAML which he generated and then using the template the developer creates an ASP.Net file capable of generating the full XAML of the graph and displaying it on a page (the developer will be CODING which is what he's good at.)

It's not that this kind of server side generation of rich animation like content wasn't available previously, Adobe has a product called FLEX which essentially (if I remember correctly) allows you to generate Flash files with server side backend code. Silverlight essentially opens the door of rich content generation to the other developers out there, without having to buy another server, or application platform.

Why did I mention OTHER developers instead of Microsoft technology based developers? As I mentioned, Silverlight is a PRESENTATION plugin, it reads and displays XAML, and all logic for client side interactivity is in Javascript (the language of choice for web client side interactivity coding... damn I miss VBScript.. curse those ;s!) So you don't have to be using ASP.Net to take advantage of Silverlight, you can use anything you like.. PHP, Java, PYTHON anything as long as you can deliver the XAML to the plugin.

There are still some hurdles that need to be jumped through first though, the main one being...

There Aren't Any XAML Editors Out There That Supports Silverlight Yet!

There isn't any *official* way to create the loose XAML files that Silverlight uses yet, you can probably rely on tools that create XAMLs for use with WPF, but there might be issues since there are certain extensions in WPF XAML files which aren't used in Silverlight. The current version of Microsoft Expression Blend which is THE application to create XAML with doesn't yet support Silverlight files, but with some slight tweaks I was able to use it to make XAML files suitable for Silverlight so I do believe that it's quite possible that Expression Blend will be released with support for Silverlight files.

Can't display HTML styled formating in it's textblocks

One really sad thing is that the text block doesn't support text that is HTML formatted, I don't mean anything like ACTUAL HTML formatting, I just want the <b>,<i> and <a> tags to work that would be super useful. Flash has this feature though, which is cool.

No Intellisense For Client-Side Scripting in VS2k5

Yes.. it's not essentlal.. but hey it'd be a VERY welcomed addition!

An end user probably wouldn't see what the big fuss about Silverlight is about other than MS trying to knock Flash off, but to developers who understand what can be done with Silverlight the possibilities are astounding. I personally can't wait to put Silverlight and Atlas together and take them out for a spin!


Saturday, April 21, 2007 8:45:40 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Thursday, April 19, 2007

As I was mucking around MSDN a few days ago I came across the article "Data Storage Architecture with SQL Server 2005 Compact Edition" It was essentially talking about where you could use the ever nifty SQL2k5CE. It also mentioned that you could use it with websites, something which I read from the SQL2k5CE Books Online that it wasn't supported. But the golden line in the article was this

If you choose to use SSCE for Web applications, some configuration is required to allow SSCE to be hosted in the Web application process.

I quickly rummaged through the documentation for SQL2k5CE more carefully this time, and I found this:-

AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true)

Aaaa.. the magic key! My eyes must have glazed over the paragraph the moment I read the part about ASP.Net not being a SUPPORTED host for SQL2k5CE. Essentially all you need to do is throw that line in your Application_OnStart event of your Global.asax file and you'll be able to use it in your ASP.Net application.

Wonder how performance is though, since the article mentions that SQL2k5CE should only be used on websites with a low performance of oh... <300~500 request an HOUR. I guess that would mean most personal websites that dont get slashdotted! ;)


Thursday, April 19, 2007 10:03:25 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
# Sunday, March 11, 2007

AJAX is the current in thing to do with interactive webpages, there's no lack of free frameworks available out there for achieving AJAX functionality in your webpages. Typically an AJAX framework will help you easily map out and call a function on your server side pages so you can do a query to a DB, perform an action, and then return the results to the client browser without incurring a post back.

The problem was.. AFTER you got the results back, how are you going to update the page? In order to present the results you had to be pretty well versed in manipulating the HTML DOM to rebuild say.. a table of query results, and that was the usual stumbling block when someone wanted to implement AJAX functionality on their webpages.

Then Microsoft released the ASP.Net 2.0 AJAX Extensions, which used a very neat *trick* to solve the update problem. With the AJAX Extensions installed, you first code everything JUST AS YOU WOULD NORMALLY and this is important, for the most part you don't need to change a thing on how you code. Then you identify which controls require to be dynamically updated and put them into a container control called the UpdatePanel and like magic, the control now updates without a post back!

And what I find to be the coolest feature of the Extensios toolkit? If for some reason the user disabled Javascript on their browser, or the browser doesn't support the javascript required to dynamically update your page. He can still have access to all the functions of your page because it would just post back normally instead of being dynamically updated so you don't have to worry about compatibility with lesser browsers!

Of course that's a very simple description of the magic that happens, but if you're an ASP.Net 2.0 programmer you owe it to yourself to check out the extensions TODAY! ;)


Sunday, March 11, 2007 12:33:33 AM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [0]  | 
Page 1 of 1 in the ASPNet category