<?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 : Visual Studio, Controls</title><link>http://blog.dotnetstyling.com/archive/tags/Visual+Studio/Controls/default.aspx</link><description>Tags: Visual Studio, Controls</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.2)</generator><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>Asp.Net: How to debug custom controls in Design Time mode.</title><link>http://blog.dotnetstyling.com/archive/2007/04/03/asp-net-how-to-debug-custom-controls-in-design-time-mode.aspx</link><pubDate>Tue, 03 Apr 2007 11:28:00 GMT</pubDate><guid isPermaLink="false">5b0fd9f5-e499-434e-81d8-bae286ef57b6:8</guid><dc:creator>admin</dc:creator><slash:comments>0</slash:comments><comments>http://blog.dotnetstyling.com/comments/8.aspx</comments><wfw:commentRss>http://blog.dotnetstyling.com/commentrss.aspx?PostID=8</wfw:commentRss><description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are planning to create WebControlLibrary you will certainly need to debug the code not only in normal runtime but also in &lt;B&gt;Design Time&lt;/B&gt; mode. In case of error in a code in design time usually you see rectangle with an exception message. But of course you need to step thru the code to fined the bug. But how to do that ? Is there a way to debug a code during design time? 
&lt;P&gt;I am glad to say “YES”, it is possible and easy. 
&lt;P&gt;It will be much easier if we will do it together step by step in concrete example. 
&lt;P&gt;First of all lets create a test WebControlLibrary project. 
&lt;P&gt;&lt;IMG height=271 alt=DTM-NewLibraryProject.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image001%5B5%5D.jpg" width=450&gt; 
&lt;P&gt;Then right click on project and select property. 
&lt;P&gt;&lt;IMG height=426 alt=DTM-Property.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image002%5B5%5D.jpg" width=148&gt; 
&lt;P&gt;In the property window navigate to &lt;B&gt;Debug&lt;/B&gt; section and switch radio button from “Start Project” (set by default) to “Start external program” and browse for devenv.exe executable of Visual Studio which you are using. 
&lt;P&gt;&lt;IMG height=283 alt=DTM-DebugWin.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image003%5B4%5D.jpg" width=500&gt; 
&lt;P&gt;Close the property window and for testing purposes set a breakpoint in RenderContent method. 
&lt;P&gt;&lt;IMG height=148 alt=DTM-Breakpoint.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image004%5B4%5D.jpg" width=303&gt; 
&lt;P&gt;To start debugging right click on project and select “Debug --&amp;gt; Create New Instance” 
&lt;P&gt;&lt;IMG height=339 alt=DTM-StartNewInstance.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image005%5B3%5D.jpg" width=300&gt; 
&lt;P&gt;Debug process will create an instance of Visual Studio. To test behavior of control you can choose one of your existing web projects or create new one. For now I will create fresh WebSite project. Notice that original version of Visual Studio is in Debug mode. During creation or opening web project I am suggesting to turn off any Add-ins if you are using in Visual Studio. Sometimes those tools are raising exceptions and I don’t see reason to use them during testing of design time behavior of custom control. 
&lt;P&gt;&lt;IMG height=332 alt=DTM-NewWebSite.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image006.jpg" width=391&gt; 
&lt;P&gt;Now it will be maybe easier to add our assembly to the Toolbox and use it from there. It will automatically add reference of our assembly. By using Drag and Drop technique it will automatically register&amp;nbsp;assembly on a page . 
&lt;P&gt;On Toolbox right click and select “Choose Items”. 
&lt;P&gt;&lt;IMG height=295 alt=DTM-Toolbox.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image007.jpg" width=222&gt; 
&lt;P&gt;Navigate for our assembly by using “Browse” button. Make sure that you are picking debug version of assembly (by default it will be located in Debug folder inside of Bin). 
&lt;P&gt;&lt;IMG height=326 alt=DTM-AddToToolbox.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image008.jpg" width=450&gt; 
&lt;P&gt;After clicking “OK” on “Choose Toolbox Items” window our custom control (or controls if you have several in project) will appear in Toolbox window of Visual Studio. 
&lt;P&gt;&lt;IMG height=305 alt=DTM-ToolboxItem.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image009.jpg" width=300&gt; 
&lt;P&gt;Now just find it there, drug and drop to the web page. Switch from “Source” mode of your page editor to “Design” and “the magic will appear” :) 
&lt;P&gt;&lt;IMG height=181 alt=DTM-MagicResult.jpg src="http://blog.dotnetstyling.com/blogs/dotnetworld/WindowsLiveWriter/Asp.NetHowtodebuginDesignTimemode_BB3A/clip_image010.jpg" width=361&gt; 
&lt;P&gt;Now add breakpoints you need, step thru code, do the stuff you are usually doing during debugging. Next time you can just run "Debug--&amp;gt;Create New Instance" and just choose created WebSite project. 
&lt;P&gt;I hope this information will help you to create nice, professional controls without any bugs :). 
&lt;P&gt;Have a fun.&lt;/P&gt;&lt;img src="http://blog.dotnetstyling.com/aggbug.aspx?PostID=8" 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/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></channel></rss>