<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blog.dotnetstyling.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>DotNetStyling - .Net World by Armen Ayvazyan : C#, Web Controls</title><link>http://blog.dotnetstyling.com/archive/tags/C_2300_/Web+Controls/default.aspx</link><description>Tags: C#, Web Controls</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.2)</generator><item><title>Issue of Rendering JavaScript in WebControls</title><link>http://blog.dotnetstyling.com/archive/2007/11/05/issue-of-rendering-javascript-in-webcontrols.aspx</link><pubDate>Mon, 05 Nov 2007 08:42:00 GMT</pubDate><guid isPermaLink="false">5b0fd9f5-e499-434e-81d8-bae286ef57b6:69</guid><dc:creator>Armen Ayvazyan</dc:creator><slash:comments>1</slash:comments><comments>http://blog.dotnetstyling.com/comments/69.aspx</comments><wfw:commentRss>http://blog.dotnetstyling.com/commentrss.aspx?PostID=69</wfw:commentRss><description>Originally I was not planning to post this subject until I spent hours for hunting some ghost JavaScript code. Problem was in generating custom JavaScript inside of WebControl. Basically this is not about "How" to generate JavaScript code but "When"....(&lt;a href="http://blog.dotnetstyling.com/archive/2007/11/05/issue-of-rendering-javascript-in-webcontrols.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blog.dotnetstyling.com/aggbug.aspx?PostID=69" width="1" height="1"&gt;</description><category domain="http://blog.dotnetstyling.com/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blog.dotnetstyling.com/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blog.dotnetstyling.com/archive/tags/Web+Controls/default.aspx">Web Controls</category></item><item><title>.NET 2.0. Access file in Design Time mode.</title><link>http://blog.dotnetstyling.com/archive/2007/04/09/net-2-0-access-file-in-design-time-mode.aspx</link><pubDate>Sun, 08 Apr 2007 22:02:11 GMT</pubDate><guid isPermaLink="false">5b0fd9f5-e499-434e-81d8-bae286ef57b6:11</guid><dc:creator>admin</dc:creator><slash:comments>1</slash:comments><comments>http://blog.dotnetstyling.com/comments/11.aspx</comments><wfw:commentRss>http://blog.dotnetstyling.com/commentrss.aspx?PostID=11</wfw:commentRss><description>&lt;p&gt;Most of programmers who are dealing with ASP.NET know how to access files on the server. The common way how to do this is to use &lt;strong&gt;HttpContext.Current.Server.MapPath&lt;/strong&gt; static method and get physical path of the file by passing virtual. But what if we need for instance to read some data from text file which located in&amp;nbsp;our project in &lt;strong&gt;Design Time mode&lt;/strong&gt;. Server.MapPath won't help us so much as our application is not hosted by IIS or Cassini and HttpContext didn't fill yet. How to get physical path of the file then?&amp;nbsp; Thanks to .Net 2.0 it is simple.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;For instance let's try to get physical path of the file (sample.txt) stored in the root directory of project thru server control .&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;To achieve&amp;nbsp;our goal we&amp;nbsp;need&amp;nbsp;three players &lt;strong&gt;ISite&lt;/strong&gt;,&amp;nbsp;&amp;nbsp;&lt;strong&gt;IWebApplication &lt;/strong&gt;and &lt;strong&gt;IProjectItem&lt;/strong&gt;.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;ISite &lt;/strong&gt;- Gets information about the container that hosts the current control when rendered on a design surface.&lt;br&gt;We have this property available in control&amp;nbsp;level (&lt;strong&gt;Control.Site&lt;/strong&gt;). Be aware Site property is filled &lt;strong&gt;only &lt;/strong&gt;in design time mode and not available when application hosted under the IIS or Cassini. &lt;br&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;IWebApplication &lt;/strong&gt;- IServiceProvider based interface which provides access to Web application in Design Mode. &lt;br&gt;By using this service we have ability to access items in our projects and reading web.config file in Design Mode.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;IProjectItem&lt;/strong&gt; - Provides an interface for an item that is retrieved at design time.&lt;br&gt;Contains such information as physical path, virtual path, name etc&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;First we have to get IWebApplication service from the list of running services.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;IWebApplication webApp =&amp;nbsp;(IWebApplication)Site.GetService(typeof(IWebApplication));&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Get project item by calling appropriate method.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;IProjectItem item = webApp.GetProjectItemFromUrl("~/sample.txt");&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Then just get information about physical path from property of IProjectItem interface.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;string filePhysicalPath = item.PhysicalPath;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;That is it.&amp;nbsp;Easy isn't it, but big headache for the programmers who create controls and doesn't know&amp;nbsp;about it. :)&amp;nbsp;&lt;/p&gt;&lt;img src="http://blog.dotnetstyling.com/aggbug.aspx?PostID=11" width="1" height="1"&gt;</description><category domain="http://blog.dotnetstyling.com/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blog.dotnetstyling.com/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blog.dotnetstyling.com/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blog.dotnetstyling.com/archive/tags/Tip+and+Trick/default.aspx">Tip and Trick</category><category domain="http://blog.dotnetstyling.com/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://blog.dotnetstyling.com/archive/tags/Web+Controls/default.aspx">Web Controls</category><category domain="http://blog.dotnetstyling.com/archive/tags/Controls/default.aspx">Controls</category></item><item><title>How to determine Design Time mode in ASP.NET</title><link>http://blog.dotnetstyling.com/archive/2007/04/04/how-to-determine-design-time-mode-in-asp-net.aspx</link><pubDate>Tue, 03 Apr 2007 20:04:12 GMT</pubDate><guid isPermaLink="false">5b0fd9f5-e499-434e-81d8-bae286ef57b6:9</guid><dc:creator>admin</dc:creator><slash:comments>0</slash:comments><comments>http://blog.dotnetstyling.com/comments/9.aspx</comments><wfw:commentRss>http://blog.dotnetstyling.com/commentrss.aspx?PostID=9</wfw:commentRss><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp; &lt;p&gt;During&amp;nbsp;creating of Custom Controls sometimes programmers are dealing with implementation of code which should run only in Design Time mode. And the main question is, how to determine in ASP.NET whether your code is running in Design Time mode or not?  &lt;p&gt;&amp;nbsp; &lt;p&gt;I can offer several approaches for checking that.  &lt;p&gt;&lt;b&gt;&lt;/b&gt; &lt;p&gt;&lt;b&gt;&lt;/b&gt;&amp;nbsp; &lt;p&gt;&lt;b&gt;DesignMode property&lt;/b&gt;  &lt;p&gt;First and obvious decision is to use &lt;b&gt;DesignMode&lt;/b&gt; property of &lt;b&gt;Control&lt;/b&gt; class. It is a Boolean value which returns &lt;b&gt;true&lt;/b&gt; in case when your code in running in Visual Studio. &lt;p&gt;&amp;nbsp; &lt;p&gt;But this approach has two disadvantages:  &lt;p&gt;1.&amp;nbsp; What if you need to determine design time mode not in control implementation but in some class which is part of your WebSite project. In that case access to DesignMode won’t be such easy ;)  &lt;p&gt;2.&amp;nbsp; DesignMode property is new one in .Net Framework 2.0 and not exists in 1.1  &lt;p&gt;There are several ways how to avoid this problem.&lt;b&gt;&lt;/b&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&amp;nbsp; &lt;p&gt;&lt;b&gt;HttpContext&lt;/b&gt;  &lt;p&gt;We can check HttpContext object against null value. During design time mode HttpContext object is empty which means we can write something like this:  &lt;p&gt;public bool IsDesignMode&lt;br&gt;{ &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; get { return System.Web.HttpContext.Current == null; }&lt;br&gt;}  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&amp;nbsp; &lt;p&gt;&lt;b&gt;HostingEnvironment&lt;/b&gt;  &lt;p&gt;Second approach is to use IsHosted static property of HostingEnvironment object which provides functionality to manage application within application domain. During design time application is not hosted.  &lt;p&gt;&lt;b&gt;&lt;/b&gt;public bool IsDesignMode&lt;br&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; get{&amp;nbsp;return !System.Web.Hosting.HostingEnvironment.IsHosted; }&lt;br&gt;}  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&amp;nbsp; &lt;p&gt;&lt;b&gt;Site&lt;/b&gt;  &lt;p&gt;Next approach is to use System.Web.UI.Control.Site&lt;b&gt; . &lt;/b&gt;It&lt;b&gt; &lt;/b&gt;gets&lt;b&gt; &lt;/b&gt;information about a container which hosts the current control when rendered on a design surface . And in our case the code can look like this:  &lt;p&gt;public bool IsDesignMode&lt;br&gt;{ &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; get { return Site!=null; }&lt;br&gt;}  &lt;img src="http://blog.dotnetstyling.com/aggbug.aspx?PostID=9" width="1" height="1"&gt;</description><category domain="http://blog.dotnetstyling.com/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blog.dotnetstyling.com/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blog.dotnetstyling.com/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blog.dotnetstyling.com/archive/tags/Tip+and+Trick/default.aspx">Tip and Trick</category><category domain="http://blog.dotnetstyling.com/archive/tags/Web+Controls/default.aspx">Web Controls</category><category domain="http://blog.dotnetstyling.com/archive/tags/Controls/default.aspx">Controls</category></item></channel></rss>