Tag: howto

Just delete the damn folder…

So I run into a problem where there are folders on my “Storage” drive (like drive “D:” that is way bigger than my boot drive) that for some unknown friggin’ reason I am unable to delete, Windows 7 just gives me some run around and makes me want to take a sledgehammer to the damn thing.

So searching around and I found how to delete it; Change the owner the admin, change the acl’s, then use rmdir from cmd line.

Like so (to whack folder “D:\delete_damnit”);

  • Right-click on folder -> Properties -> Security Tab
  • Click on Advanced button -> Owner tab
  • Edit.. button
  • Select Administrators in ‘Change owner to’, check ‘replace owner on subcontainers and objects’
  • Open cmd.exe as administrator
  • Run ‘cacls D:\delete_damnit /T /e /g Administrators:f’
  • Run ‘rmdir /S D:delete_damnit’

Enjoy!

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

jQuery UI Themeswitcher Cookie Expires

So I disagree with the current setup that is the default configuration with jQuery UI Themeswitcher, it expires the cookie at the end of the session. I mean really if the user is going to change the cookie to some color scheme they like chances are its going to be for longer than the current session and for crying out loud the user is going to expect your site to load the theme they selected last – else it looks broken. Just a poor decision in my opinion – so here is how I fixed it to last 1 year.

Download the themeswitcher.js and save it locally wherever you like.

Now add the “cookieName” below this to the options defition in the file;

    var options = jQuery.extend({
        loadTheme: null,
        initialText: 'Switch Theme',
        width: 150,
        height: 200,
        buttonPreText: 'Theme: ',
        closeOnSelect: true,
        buttonHeight: 14,
        cookieName: 'jquery-ui-theme',
        cookieExpires: 365,
        onOpen: function () { },
        onClose: function () { },
        onSelect: function () { }
    }, settings);

Now modify where the cookie gets made to use this new “cookieExpires” option (line 49);

        $.cookie(options.cookieName, themeName, { expires: options.cookieExpires });

Know you can either use the default set here (365) or set your own in the initializer in your calling script;

    $('#switcher').themeswitcher({ cookieExpires: 365, path: '/', loadTheme: "Sunny"});

Enjoy!

VN:F [1.9.11_1134]
Rating: 8.3/10 (6 votes cast)

Securing ELMAH with ASP.NET MVC

So I like Elmah and its logging of my unhandled exceptions suer, but I don’t need every yahoo with visibility to that. This is the steps I took to only allow Elmah access to users in the “Admin” role and have it still work with ASP.NET MVC.

Put this into the web.config;

  
    
      
        
      
      
        
        
      
    
  

Now put this into your global.asax, up top (before all the other routing) is best;

            routes.IgnoreRoute("admin/elmah.axd");
            routes.IgnoreRoute("admin/{resource}.axd/{*pathInfo}");

You don’t have have any controller action setup, the handler builds this on the fly to handle the incomding axd request. Nice and simple.

Enjoy!

VN:F [1.9.11_1134]
Rating: 10.0/10 (2 votes cast)

Simple merging object to object

I use View Models to hand to my MVC views, nice and slim with little if any methods on them and I use a think ORM model that has all my business logic and is generated by my ORM (BLToolkit).

So the problem is I wanted to take the View Model and update the ORM model with the modified values from the post, kind of a reverse AutoMapper (or BLToolkit.Mapper).

So a ORM class like this;

    public class Contact
    {
         public int Id { get; set; }
         public string Firstname { get; set; }
         public string Lastname { get; set; }
         // Loads of other properties
    }

And a View Model like this (note the type and name should be “EXACTLY” the same;

    public class ContactModel
    {
         public int Id { get; set; }
         public string Firstname { get; set; }
         public string Lastname { get; set; }
    }

Then with this generic Extension method;

        public static void MergeWith(this T1 primary, T2 secondary)
        {
            foreach (var pi in typeof(T2).GetProperties())
            {
                var tpi = typeof(T1).GetProperties().Where(x => x.Name == pi.Name).FirstOrDefault();
                if (tpi == null)
                {
                    continue;
                }
                var priValue = tpi.GetGetMethod().Invoke(primary, null);
                var secValue = pi.GetGetMethod().Invoke(secondary, null);
                if (priValue == null || !priValue.Equals(secValue))
                {
                    tpi.GetSetMethod().Invoke(primary, new object[] { secValue });
                }
            }
        }

So you make the magic in the Controller (for example) like this;

[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult EditContact(ContactModel model)
    Contact c = Repo.Get<Contact,long>(model.Id);
    c.MergeWith<Contact,ContactModel>(model);
    Repo.Update<Contact>(c);
}

Yea its cheesy, but it works for me ;)

Enjoy!

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

Nice javascript to set window size for popups

This is some spiffy javascript to get the proper sizes of the window for say a nifty colorbox popup;

    var myWidth = 0, myHeight = 0;
    function setWindowSize() {
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
};

An example of using it is like this;

$.fn.colorbox({
    iframe: true,
    open: true,
    href: '/Controller/Action',
    width: myWidth - 40, // just a bit smaller than the window
    height: myHeight - 40 // just a bit smaller than the window
});

Dont forget to bind the window resize to reset your size variables;

$(window).resize(function(e){
    setWindowSize();
});

Enjoy!

VN:F [1.9.11_1134]
Rating: 7.0/10 (1 vote cast)

Two forms same page?

I have a page that has two forms in the same page, the catch is the second form is nested inside the first form. Well this violates W3C, and it seems that IE8 doesn’t work with it. Color me surprised that IE8 actually is doing what it suppose to do according to the HTML specs.

Anyhow I needed this to work for my form to function and I was limited on time. So here is what I did to make the submit work with IE8.

1. Remove the “Submit” on the Button and just make it plain button;

2. Add some jQuery magic to submit the first (the outer) form;

$("#Save").click(function() {
    var form = $("form:first").serialize();
        $.ajax({
	    type: "POST",
	    url: "/Tickets/Edit/",
	    data: form,
	    success: function(msg) {
	    window.location = '/Tickets/Details/';
	    }
	});
});

Enjoy!

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

Using Fiddler with Localhost and ASP.NET DEV IIS Server

Quick and to the point (the 1234 is the port your local IIS dev server is running on);

http://localhost:1234 <- fails

http://ipv4.fiddler:1234 <- works

Enjoy!

 

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

jqGrid get Row Column Value

This took me a while to figure out, so I am putting it here for prosperity. This is how to get the value of a specific column in a jqGrid selected Row;

ondblClickRow: function(id, rowid) {
    var ret = $("#grid").getRowData(id);
    window.location = '/Clients/Details/' + ret.ClientID;
}

Enjoy!

VN:F [1.9.11_1134]
Rating: 8.4/10 (11 votes cast)

Copyright © 1996-2010 Bits, Bytes And Burps. All rights reserved.
iDream theme by Templates Next | Powered by WordPress