Friday, 8 June 2012

Message box in Asp.Net application.


hi friends,
here is final code to show msg box in asp.net app.......


my mail id ..

shivram.popalghat@gmail.com

--------------------------------------------------------------------------------------------------------
Step 1).
create a code file, contains following code...
Name it as :- <Modal.cs>
Keep this file in App_Code folder.

using System.Text;
using System.Web.UI;

/// <summary>
/// Summary description for Modal
/// </summary>
public class Modal
{

    public static void Close(Page page)
    {
        Close(page, null);
    }

    public static void Close(Page page, object result)
    {
        page.Response.ClearContent();

        StringBuilder sb = new StringBuilder();
        sb.Append("<html>");
        sb.Append("<head>");
        sb.Append("<script type='text/javascript'>");
        sb.Append("if (parent && parent.DayPilot && parent.DayPilot.ModalStatic) {");
        sb.Append("parent.DayPilot.ModalStatic.result = " +
                  DayPilot.Web.Ui.Json.SimpleJsonSerializer.Serialize(result) + ";");
        sb.Append("if (parent.DayPilot.ModalStatic.hide) parent.DayPilot.ModalStatic.hide();");
        sb.Append("}");
        sb.Append("</script>");
        sb.Append("</head>");
        sb.Append("</html>");

        string output = sb.ToString();

        byte[] s = Encoding.UTF8.GetBytes(output);
        page.Response.AddHeader("Content-Length", s.Length.ToString());

        page.Response.Write(output);

        page.Response.Flush();
        page.Response.Close();

    }

}


-------------------------------------------------------------------------------------------------------

Step 2) Create a  script  file, contains following code...
Name it as :- <modal.js>
Keep this file in script folder
--------

if (typeof(DayPilot) === 'undefined') {
    DayPilot = {};
}

DayPilot.ModalStatic = {};

DayPilot.Modal = function() {

    // default values
    this.width = 300;
    this.height = 460;
    this.top = 20;
    this.opacity = 30;
    this.border = "1px solid black";

    // internal
    var This = this;

    this.id = '_' + new Date().getTime() + 'n' + (Math.random()*10); 
    this.closed = null;

    this.showHtml = function(html) {

   if (!this.div) {
   this.create();
   }
   else {
   this.div.style.display = '';
   }

   var delayed = function(p, innerHTML) {
       return function() {
           p.setInnerHTML(p.id + "iframe", innerHTML);
            }
        };
            
   window.setTimeout(delayed(this, html), 0);

    };

    this.showUrl = function(url) {

   if (!this.div) {
   this.create();
   }
   else {
   this.div.style.display = '';
   this.hideDiv.style.display = '';
   }
   DayPilot.ModalStatic = this;
   
   this.iframe.src = url;

    };

    this.create = function() {
        var scrollY = window.pageYOffset ? window.pageYOffset : ((document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);
        var scrollX = window.pageXOffset ? window.pageXOffset : ((document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft);
    
        var height = function () {
            var D = document;
            return Math.max(
                Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
                Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
                Math.max(D.body.clientHeight, D.documentElement.clientHeight)
            );
        };
    
        var width = function () {
            var D = document;
            return Math.max(
                Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
                Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
                Math.max(D.body.clientWidth, D.documentElement.clientWidth)
            );
        };
    
   var hide = document.createElement("div");
   hide.id = this.id + "hide";
   hide.style.position = 'absolute';
   hide.style.left = "0px";
   hide.style.top = "0px";
   hide.style.width = "100%";
   hide.style.height = height() + "px";
   hide.style.filter = "alpha(opacity=" + this.opacity + ")";
   hide.style.opacity = "0." + this.opacity;
   hide.style.backgroundColor = "black";
   hide.onclick = function() { This.hide(); };
   hide.oncontextmenu = function() { return false; };

   document.body.style.height = '100%';

   document.body.appendChild(hide);

   var iframe = document.createElement("iframe");
   iframe.id = this.id + "iframe";
   iframe.name = this.id + "iframe";
   iframe.frameBorder = '0';
   iframe.style.width = '100%';
   iframe.style.height = (this.height) + 'px';

   var div = document.createElement("div");
   div.id = this.id + 'popup';
   div.style.border = this.border;
   div.style.position = 'absolute';
   div.style.left = '50%';
   div.style.marginLeft = '-' + Math.floor(this.width/2) + "px";  // '-45%'
   div.style.top = (scrollY + this.top) + 'px';
   div.style.width = this.width + 'px';  // '90%'
   div.style.height = this.height + 'px';
   div.style.backgroundColor = 'white';

   div.appendChild(iframe);

   document.body.appendChild(div);

   this.div = div;
   this.iframe = iframe;
   this.hideDiv = hide;
    };

    this.setInnerHTML = function(id, innerHTML) {
   var frame = window.frames[id];

   var doc = frame.contentWindow || frame.document || frame.contentDocument;
   //alert(id + ' ' + frame);
   if (doc.document) {
   doc = doc.document;
   }

   doc.body.innerHTML = innerHTML;
    };

    this.hide = function() {
   if (this.div) {
   this.div.style.display = 'none';
   this.hideDiv.style.display = 'none';
   }
   if (this.closed) {
       this.closed();
   }
    };

};
-----------------------------------------------------------------------------------------------------------
Step 3) Call msg box in your application as follow....

       Modal.Close(this, "OK");









2 comments:

ASP.Net Development India said...

Information about message box in asp.net application development in coding. this coding is very useful for sandbox. Thank you

AspDotNetDeveloper said...

Welcome.

Thanks for viewing and commenting.