Saturday, December 03, 2011 #

Minify JS and CSS in Visual Studio Build Events

It’s pretty widely accepted (and common sense) that if you’re putting a site out into the wild – such a simple task as minifying CSS and JavaScript files can save a bunch of load time, especially on mobile devices.

I’ve been playing about with this today and was quite happily using jsmin as described here until I noticed that in IE 7 and IE 8 the minified CSS didn’t work.

The problem seems to be when using a background style, it takes out the space between the image url close bracket and the next style.

background:url(image.png) no-repeat;
background:url(image.png)no-repeat;

Not a problem in IE 9 or chrome (haven’t tested others).

So I set about researching the problem and found nothing…struggled a bit getting the search term right and clearly never mastered it as I found zip.

Not to be put off, I was looking at the YUI compression tool from Yahoo, said to be better…but needs the java runtime and seems to complex to simply use, certainly for my simple taste!

Then I found a YUI .net implementation on codeplex.  Excellent, its was even in NuGet.

However…I wanted it as a build task to run in VS, I don’t really want to think about this stuff more than once.

So I wrote a little command line wrapper for it.  Excellent.  Even got ILMerge involved to merge the YUI dll’s into my exe.  I know there are ways to use Reflection to load dll’s as embedded resources…but I didn’t fancy doing that!

I did look at using CommandLine (also on codeplex) as I have done before, but ILMerge does not like that.  Don’t take my word for it, but it seems to be something to do with ILMerge is .net 2 and CommandLine uses some .net4 features.  It wasn’t happy…anyway, I just wrote a few lines of code to parse the command line args myself.

Perfect!!!

It’s not complex or very flexible, but is very suitable to my needs.

You can pass in an array of files to minify and a target path.  That’s it.  It’ll merge the files if you pass more than one…you have no choice!

> SCmin.exe –s C:\big.css C:\big1.css –t C:\small.css

// This will take big.css and big1.css and put it into small.css

Simples!

Download the exe or full source.

  
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Saturday, December 03, 2011 3:52 AM | Feedback (0)

Tuesday, November 22, 2011 #

Visual Web Part as a Sandboxed solution

You want the RAD wonderfulness of a visual web part, but it needs to be deployed as a Sandboxed solution.
Problem?
No, SharePoint powertools for visual studio to the rescue!
 
 
There are a couple limitations, read the above page, nothing major. e.g.
1. Javascript debugging is not supported
2. Debugging asp.net code is not supported.
3. Use of <% Assembly Src= is not supported
 
I understand it does it by adding the markup as an embedded resource, but I haven't actually tried it yet!  To come!
 
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Tuesday, November 22, 2011 2:03 AM | Feedback (0)

Monday, July 04, 2011 #

jQuery Mobile…is it ready???

I hope you haven’t come here looking for an answer!!! Because I want one too!!!

I have recently been playing with JQM…and initially loved it….but….

I mean, jQuery mobile is fantastic...whats been done here I think is awesome.  I'm enjoying working with it.

(I know its beta 1!, but the below seems odd for even a beta release)

There seems to be some fundamental bugs knocking about.  Links for example.  I have a fixed footer toolbar...3 buttons, Home, New, Settings.

Home - I wanted to be non-ajax, so I set data-ajax=false ( I need that page to be refreshed from the server each time its hit - I'm sure I could do something better - but I'm working on a POC)...In the browser it works as expected.  Go to the iPhone (added to Home Screen) hit that button and a new safari window opens?

New - This is an ajax true page, but doesn't always work...sometimes it goes to the new page, sometimes it goes to another page.

Back - rarely works as expected.

To be honest - links in general I find a little ropey...

Form Post - This seems to struggle with consistency...9/10 times it works a treat, then occasionally the loading icon just spins and it continues does so for a long while...too long to wait.  To be fair, this could be a drop in the iPhone wifi connection for whatever reason - I'm not sure anyway that form posting is the best solution for a mobile device targeted app...but like i said...POC :)

It's probably worth saying I am using asp.net mvc 3.  Each jQuery mobile page is in a separate Action and View...I haven't (except 1 dialog) got more than 1 page in each file.

The reason I posted this thread is really to raise my concerns and to get feedback from the community...I'm looking at a reasonable project coming my way and I make heavy use of jQuery so this seems natural to me...but I don't want to realise I've made a bad decision in 2 months time and need to start again???

Perhaps its just not ready….and that’s fine...but I don't want to realise I've made a bad decision in 2 months time and need to start again???

I'd also like to add...Window Phone 7...is NOT fully supported.  Tested on HTC

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Monday, July 04, 2011 9:53 AM | Feedback (1)

Monday, March 07, 2011 #

SharePoint 2010 Sandboxed solution SPGridView

If you didn’t know, you probably will soon, the SPGridView is not available in Sandboxed solutions.

To be honest there doesn’t seem to be a great deal of information out there about the whys and what nots, basically its not part of the Sandbox SharePoint API.

Of course the error message from SharePoint is about as useful as punch in the face…

An unexpected error has been encountered in this Web Part.  Error: A Web Part or Web Form Control on this Page cannot be displayed or imported. You don't have Add and Customize Pages permissions required to perform this action

…that’s if you have debug=true set, if not the classic “This webpart cannot be added” !! Love that one!

but will a little digging you should find something like this…

[TypeLoadException: Could not load  type Microsoft.SharePoint.WebControls.SPGridView from assembly 'Microsoft.SharePoint, Version=14.900.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.]

 

Depending on what you want to do with the SPGridView, this may not help at all.  But I’m looking to inherit the theme of the site and style it accordingly.

After spending a bit of time with Chrome’s FireBug I was able to get the required CSS classes.  I created my own class inheriting from GridView (note the lack of a preceding SP!) and simply set the styles in there.

Inherit from the standard GridView

  1. public class PSGridView : GridView

 

Set the styles in the contructor…

  1. public PSGridView()
  2. {
  3.     this.CellPadding = 2;
  4.     this.CellSpacing = 0;
  5.     this.GridLines = GridLines.None;
  6.     this.CssClass = "ms-listviewtable";
  7.     this.Attributes.Add("style", "border-bottom-style: none; border-right-style: none; width: 100%; border-collapse: collapse; border-top-style: none; border-left-style: none;");
  8.  
  9.     this.HeaderStyle.CssClass = "ms-viewheadertr";
  10.     
  11.     this.RowStyle.CssClass = "ms-itmhover";
  12.     this.SelectedRowStyle.CssClass = "s4-itm-selected";
  13.     this.RowStyle.Height = new Unit(25);
  14. }

 

Then as you cant override the Columns property setter, a custom method to add the column and set the style…

  1. public void AddColumn(DataControlField field)
  2. {
  3.     if (field == null) return;
  4.  
  5.     field.ItemStyle.CssClass = "ms-vb2";
  6.     field.HeaderStyle.CssClass = "ms-vh2-nofilter ms-vh2-gridview";
  7.     this.Columns.Add(field);
  8. }

And that should be enough to get the nicely styled SPGridView without the need for the SPGridView, but seriously….get the SPGridView in the SandBox!!!

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Monday, March 07, 2011 10:56 AM | Feedback (4)

Saturday, March 05, 2011 #

jQuery Datatable in MVC … extended.

There are a million plugins for jQuery and when a web forms developer like myself works in MVC making use of them is par-for-the-course!  MVC is the way now, web forms are but a memory!!

Grids / tables are my focus at the moment.  I don’t want to get in to righting reems of css and html, but it’s not acceptable to simply dump a table on the screen, functionality like sorting, paging, fixed header and perhaps filtering are expected behaviour.  What isn’t always required though is the massive functionality like editing etc you get with many grid plugins out there.

You potentially spend a long time getting everything hooked together when you just don’t need it.

That is where the jQuery DataTable plugin comes in.  It doesn’t have editing “out of the box” (you can add other plugins as you require to achieve such functionality).

What it does though is very nicely format a table (and integrate with jQuery UI) without needing to hook up and Async actions etc. 

Take a look here… http://www.datatables.net

I did in the first instance start looking at the Telerik MVC grid control – I’m a fan of Telerik controls and if you are developing an in-house of open source app you get the MVC stuff for free…nice!  Their grid however is far more than I require. 

Note: Using Telerik MVC controls with your own jQuery and jQuery UI does come with some hurdles, mainly to do with the order in which all your jQuery is executing – I won’t cover that here though – mainly because I don’t have a clear answer on the best way to solve it!

One nice thing about the dataTable above is how easy it is to extend http://www.datatables.net/examples/plug-ins/plugin_api.html and there are some nifty examples on the site already…

I however have a requirement that wasn’t on the site … I need a grid at the bottom of the page that will size automatically to the bottom of the page and be scrollable if required within its own space i.e. everything above the grid didn’t scroll as well.  Now a CSS master may have a great solution to this … I’m not that master and so didn’t! The content above the grid can vary so any kind of fixed positioning is out.

So I wrote a little extension for the DataTable, hooked that up to the document.ready event and window.resize event.

Initialising my dataTable ( s )…

  1. $(document).ready(function () {
  2.     var dTable = $(".tdata").dataTable({
  3.         "bPaginate": false,
  4.         "bLengthChange": false,
  5.         "bFilter": true,
  6.         "bSort": true,
  7.         "bInfo": false,
  8.         "bAutoWidth": true,
  9.         "sScrollY": "400px"
  10.     });

My extension to the API to give me the resizing….

 

  1. // **********************************************************************
  2. // jQuery dataTable API extension to resize grid and adjust column sizes
  3. //
  4. $.fn.dataTableExt.oApi.fnSetHeightToBottom = function (oSettings) {
  5.     var id = oSettings.nTable.id;
  6.     var dt = $("#" + id);
  7.     var top = dt.position().top;
  8.     var winHeight = $(document).height();
  9.     var remain = (winHeight - top) - 83;
  10.     dt.parent().attr("style", "overflow-x: auto; overflow-y: auto; height: " + remain + "px;");
  11.     this.fnAdjustColumnSizing();
  12. }

 

This is very much is debug mode, so pretty verbose at the moment – I’ll tidy that up later!

You can see the last call is a call to an existing method, as the columns are fixed and that normally involves so CSS voodoo, a call to adjust those sizes is required.

Just above is the style that the dataTable gives the grid wrapper div, I got that from some firebug action and stick in my new height.

The –83 is to give me the space at the bottom i require for fixed footer!

 

Finally I hook that up to the load and window resize.  I’m actually using jQuery UI tabs as well, so I’ve got that in the open event of the tabs.

 

  1. $(document).ready(function () {
  2.         var oTable;
  3.         $("#tabs").tabs({
  4.             "show": function (event, ui) {
  5.                 oTable = $('div.dataTables_scrollBody>table.tdata', ui.panel).dataTable();
  6.                 if (oTable.length > 0) {
  7.                     oTable.fnSetHeightToBottom();
  8.                 }
  9.             }
  10.         });
  11.         $(window).bind("resize", function () {
  12.             oTable.fnSetHeightToBottom();
  13.         });
  14.     });

And that all there is too it.  Testament to the wonders of jQuery and the immense community surrounding it – to which I am extremely grateful.

I’ve also hooked up some custom column filtering on the grid – pretty normal stuff though – you can get what you need for that from their website.  I do hide the out of the box filter input as I wanted column specific, you need filtering turned on when initialising to get it to work and that input come with it!  Tip: fnFilter is the method you want.  With column index as a param – I used data tags to simply that one.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Saturday, March 05, 2011 3:58 PM | Feedback (0)

Monday, February 07, 2011 #

Setting jQuery after ASP.net AJAX partial post back

OK, so for some reason you have a mega mashup solution with ASP.net AJAX, jQuery and web forms. 

Perhaps you are just on the migration from AjaxControlToolkit to the jQuery UI framework – who knows!!

Anyway, the problem is that when you post back with something like an UpdatePanel, you will find that your nicely setup jQuery stuff, like the datepicker for example will no longer work.

You may have something like this…

  1. $(document).ready(function () {
  2.     $(".date-edit").datepicker({ dateFormat: "dd/mm/yy", firstDay: 1, showOtherMonths: true, selectOtherMonths: true });
  3. });

 

When you’re ASP.net UpdatePanel post back, you will find that your datepicker has gone.  Bugger!

Well you need to add this little gem to set it back up again once the UpdatePanel comes back to the page.

  1. var prm = Sys.WebForms.PageRequestManager.getInstance();
  2. prm.add_endRequest(function () {
  3.     $(".date-edit").datepicker({ dateFormat: "dd/mm/yy", firstDay: 1, showOtherMonths: true, selectOtherMonths: true });
  4. });

 

Or like me, you would have a javascript function, something like InitPage(); do all your work in there and call that on document.ready and endRequest.

Your choice…you have the power Smile

 

Share this post :
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Monday, February 07, 2011 2:26 AM | Feedback (1)

Wednesday, January 05, 2011 #

Using Telerik MVC with your own custom jQuery and or other plug-ins

If you are using MVC it might be worth checking out the telerik controls (http://demos.telerik.com/aspnet-mvc), they are free if you are doing an internal or “not for profit” application.

If however you do choose to use them, you could come up against a little problem I had.  Using the telerik controls with your own custom jQuery.  In my case I was using the jQuery UI dialog.

It kept throwing an error where I was setting my div to a dialog.

Code Snippet
  1. $("#textdialog").dialog({

The problem is when you use the telerik mvc stuff you need to call ScriptRegistrar

Code Snippet
  1. @Html.Telerik().ScriptRegistrar()

in order to setup the javascript for the controls.

By default this adds a reference to jQuery and if you have already added a reference to jQuery because you are using it elsewhere, this causes a problem.

I found the solution here

And it was to change the above ScriptRegistrar call to this…

Code Snippet
  1. @Html.Telerik().ScriptRegistrar().jQuery(false).DefaultGroup(g => g.Combined(true).Compress(true));

 

If you come across this one on stackoverflow it wont work – in my case the HtmlEditor would render no problem, but was unusable.  Which is the same as someone else found when using the tab control – they went to the bother of re-writing the ScriptRegistrar.  Not for me that one!!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Wednesday, January 05, 2011 3:11 AM | Feedback (2)

Monday, December 13, 2010 #

Gotcha | Installing .net 4.0 and IIS 6

Just a quick one, seems pretty weird to me.

I installed .net 4.0 on an old IIS6 box, ready to deploy a asp.net mvc app targeting .net 4.0.  I thought, which to me seems logical, that I install .net 4.0, setup a new web site, new app pool, set the web site to asp.net 4.0 (other configuration also needed to run MVC on IIS6 here and here) and it would just work.

Errr… No.  The page cannot be displayed!  Nothing to do with MVC.

Apparently just because you have installed .net 4 and the option is available in IIS, it’s not enabled.  I’m not going to repeat anything here…take a look at this post – clear, easy steps on exactly what you need to do and how to check if this is the problem.

http://johan.driessen.se/archive/2010/04/13/getting-an-asp.net-4-application-to-work-on-iis6.aspx

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Monday, December 13, 2010 4:29 AM | Feedback (0)

Monday, November 29, 2010 #

ASP.net MVC | MvcBuildViews | MachineToApplication Error

You may come up against this little error when working on a MVC application, I specifically find things like this crop up when you are upgrading from previous version e.g. VS 2008 to 2010 and the like.

 

They are a complete pain in the bum quite frankly, you can spend hours googling around, trying this trying that, posting questions here and there etc etc…so I’m adding to the this and that which you will of course try, because it’s the sort of error you don’t care about…it’s just getting in the way of your day!

 

There would seem to be some kind of issue with MvcBuildViews and this error http://goo.gl/ogo1e

 

Anyway, what worked for me was to delete the debug directory from within the obj directory and it all worked…

 

Hopefully this will help someone else!! Or at the very least be something else to try on the way to solving another time wasting issue!!

 

UPDATE:  May have been a bit hasty in my excitement of solving this…its really only part solved as I find i have to delete the debug directory every time I want to publish/package!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Monday, November 29, 2010 8:12 AM | Feedback (0)

Saturday, April 24, 2010 #

Blog comment spammers … help!

Hey, I need some advice/help, now I’ve been writing on this blog for a few years, I don’t blog a great deal in comparison to many of my peers and my blog doesn’t get a massive number of hits, BUT I seem to get a fair share of comment spammers!!

I have image verification on the comments and I have put an Akismet API key into the Geekswithblogs settings, but I still get a bunch of spam comments.

They could almost be real, until you see the link going off to some dating site or casino crap.

What does everyone do?? Delete them every time?? Moderate comments??

I would rather not moderate comments if possible, but is that the only way to stop the crap??

Thx

Technorati Tags: ,,
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Saturday, April 24, 2010 3:26 AM | Feedback (5)

Copyright © Steve Clements

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski