Telerik ASP.NET AJAX Controls in MVC project

by Rok Bermež 18. August 2010 14:37
If we want to use Telerik ASP.NET AJAX controls in MVC project, we must also use either ScriptManager or RadScriptManager. But since neither works very well here is more MVC friendly solution: public class RadMvcScriptManager : RadScriptManager { static string JavaScriptBlockFormat = @"<script type=""text/javascript"">//<![CDATA[ {0}//]]></script>"; protected override void Render(HtmlTextWriter writer) { foreach (RegisteredScript script in GetRegisteredClientScriptBlocks()) { if (HttpContext.Current.Items[script.Key] == null) { HttpContext.Current.Items[script.Key] = true; if (script.ScriptType == RegisteredScriptType.ClientScriptInclude) { writer.WriteLine(String.Format(@"<script type=""text/javascript"" src=""{0}""></script>", HttpUtility.HtmlEncode(script.Url))); } } } if (HttpContext.Current.Items["AppInitialize"] == null) { Page.Items["AppInitialize"] = true; writer.WriteLine( String.Format( JavaScriptBlockFormat, @"if(typeof(Sys) != ""undefined""){ $addHandler(window, ""load"", function(){Sys.Application.initialize();}); } else { throw new Error(""Microsoft ASP.NET AJAX cannot be initialized!"")}")); } StringBuilder builder = new StringBuilder(); if (HasReflectionPermission()) { SerializeScriptsForScriptControls(Page, builder); } string scriptContent = builder.ToString(); if (!String.IsNullOrEmpty(scriptContent)) { writer.WriteLine(String.Format( JavaScriptBlockFormat, String.Format("Sys.Application.add_init(function(){{{0}}});", scriptContent))); } } internal void SerializeScriptsForScriptControls(Control control, StringBuilder builder) { if (control is IScriptControl && control.Visible) { IEnumerable<ScriptDescriptor> descriptors = ((IScriptControl)control).GetScriptDescriptors(); if (descriptors != null) { foreach (ScriptDescriptor descriptor in descriptors) { SerializeScriptControlDescriptor(descriptor, builder); } } } if (control.HasControls()) { foreach (Control child in control.Controls) { SerializeScriptsForScriptControls(child, builder); } } } internal void SerializeScriptControlDescriptor(ScriptDescriptor descriptor, StringBuilder builder) { MethodInfo getScriptInfo = descriptor.GetType().GetMethod( "GetScript", BindingFlags.Instance | BindingFlags.NonPublic); if (getScriptInfo != null) { string script = (string)getScriptInfo.Invoke(descriptor, new object[] { }); builder.AppendLine(script); } } internal static bool HasReflectionPermission() { try { new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand(); return true; } catch (SecurityException) { return false; } } }

Tags:

.Net | c# | Web

A really simple Silverlight 2 PostIt application

by Rok Bermež 16. June 2009 13:45
During the Silverlight 2.0 introductory course I held last week, we've chosen to implement a simple [More]

Tags: , , , , , ,

c# | Development | Silverlight | Visual Studio

Paste XML as XElement

by Rok Bermež 22. May 2009 11:17
Generating XML in c# can be a time consuming task. 'Paste XML as XElement' Visual Studio extension can save you a lot of time. You can find this extension in Visual Studios samples directory:C:\Program Files (x86)\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples.zipExtract PasteXmlAsLinq project and compile it.Add PasteXmlAsLinq.AddIn and PasteXmlAsLinq.dll to one of the Visual Studios AddIn folder (you can see or add them in Tools|Options|Environment|AddIns)Restart the Visual Studio Now you have extra menu item and you can directly paste XML like <?xml version="1.0" encoding="utf-8" ?> <rootElement> <element id="1"> <podatek>aaa</podatek> </element> <element id="2"> <podatek>bbb</podatek> </element> </rootElement> to XElement xml = new XElement("rootElement", new XElement("element", new XAttribute("id", "1"), new XElement("podatek", "aaa") ), new XElement("element", new XAttribute("id", "2"), new XElement("podatek", "bbb") ) );

Tags:

.Net | c# | Visual Studio

Calendar

<<  January 2018  >>
MonTueWedThuFriSatSun
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Page List

Month List