Monday, January 5, 2009

Model Popup (Ajax Control Toolkit) and ASP.MVC




Model popup is another usefull tool in web applications. Therefore I thought to create easy way to include Model popup in ASP.NET MVC Projects as Stephen Walther (ASP.NET MVC Tip #36 – Create a Popup Calendar Helper) did for Calender Control. 



Added Following Class ModelPopupExtensions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace AjaxControlToolkitMvc
{
public static class ModelPopupExtensions
{
public static string ModelPopup(this AjaxHelper helper,
string BackgroundCssClass,
string CancelControlID,
string DropShadow,
string OkControlID,
string OnOkScript,
string PopupControlID,
string PopupDragHandleControlID,
string dynamicServicePath,
string id,
string elementId)
{
var sb = new StringBuilder();

// Add Microsoft Ajax library
sb.AppendLine(helper.MicrosoftAjaxLibraryInclude());

// Add toolkit scripts
sb.AppendLine(helper.ToolkitInclude
(
"AjaxControlToolkit.ExtenderBase.BaseScripts.js",
"AjaxControlToolkit.Common.Common.js",
"AjaxControlToolkit.DynamicPopulate.DynamicPopulateBehavior.js",
"AjaxControlToolkit.RoundedCorners.RoundedCornersBehavior.js",
"AjaxControlToolkit.Compat.Timer.Timer.js",
"AjaxControlToolkit.DropShadow.DropShadowBehavior.js",
"AjaxControlToolkit.Compat.DragDrop.DragDropScripts.js",
"AjaxControlToolkit.DragPanel.FloatingBehavior.js",
"AjaxControlToolkit.ModalPopup.ModalPopupBehavior.js"
));

// Perform $create
string properties = "";
properties = string.Format(@"""BackgroundCssClass"": ""{0}"", ""CancelControlID"": ""{1}"", ""DropShadow"": {2}, ""OkControlID"": ""{3}"", ""OnOkScript"": ""{4}"", ""PopupControlID"": ""{5}"", ""PopupDragHandleControlID"": ""{6}"", ""dynamicServicePath"": ""{7}"", ""id"": ""{8}""",
BackgroundCssClass, CancelControlID, DropShadow, OkControlID, OnOkScript, PopupControlID, PopupDragHandleControlID, dynamicServicePath, id);
properties = "{ " + properties + " }";
sb.AppendLine(helper.Create("AjaxControlToolkit.ModalPopupBehavior", properties, elementId));

return sb.ToString();
}
}
}

Added New overloaded method to the AjaxExtensions class. This method takes an extra parameter for properties.
1
2
3
4
5
6
7
8
9
10
        public static string Create(this AjaxHelper helper, string clientType, string properties, string elementId)
{
var sb = new StringBuilder();
sb.AppendLine("<script type='text/javascript'>");
sb.AppendLine("Sys.Application.add_init(function(){");
sb.AppendFormat(@"$create({0}, {1},null,null,$get('{2}'))", clientType, properties, elementId);
sb.AppendLine("});");
sb.AppendLine("</script>");
return sb.ToString();
}





6 comments:

Owais F said...
This comment has been removed by the author.
Owais F said...
This comment has been removed by the author.
teh steve said...

This is useful - just what I was looking for. However an example of usage would greatly assist.

Kris said...

Can anyone help me with a working example of this?

Unknown said...

This great! however a working example would be appreciated.

Hakim

Unknown said...

This great just want I am looking for! however, I would appreciate it if you could provide the complete code with a working example.