﻿/*
* ASCX-Loader
* Original: http://www.aspnetzone.de/blogs/robertobez/archive/2009/09/02/jquery-plugin-dynamisches-laden-von-asp-net-controls.aspx
*/
(function($) {
    $.fn.ascxLoader = function(params) {

        //Referenz auf den Container
        var obj = $(this);

        //Standardwerte
        var defaults = {
            servicePath: "",
            serviceMethod: "",
            controlPath: "",
            properties: {},
            successFunction: function(sHtml){obj.html(sHtml)},
            errorFunction: alert
        };

        //Standards und Parameter zusammenführen
        var config = $.extend(defaults, params);

        //JSON-String erzeugen
        var sData = JSON.stringify({
            controlLocation: config.controlPath,
            properties: config.properties
        });

        //Service-Abruf
        $.ajax({
            type: "POST",
            url: config.servicePath + '/' + config.serviceMethod,
            contentType: "application/json; charset=utf-8",
            data: sData,
            dataType: "json",
            success:
                function(result) {
                    config.successFunction(result.d);
                },
            error:
                function(xmlReq,
                         textStatus,
                         errorThrown) {
                    config.errorFunction(xmlReq.responseText);
                }
        });
    }
})(jQuery);

