<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Vault of Thoughts - .NET Blog - ASP.NET</title>
    <link>http://vaultofthoughts.net/</link>
    <description>My random thoughts on programming and software development. ASP.NET, C#, Best Practices and other stuff.</description>
    <language>en-us</language>
    <copyright>Michal Talaga</copyright>
    <lastBuildDate>Mon, 25 Jan 2010 00:09:59 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>mikeon@vaultofthoughts.net</managingEditor>
    <webMaster>mikeon@vaultofthoughts.net</webMaster>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=9c7878f9-83a3-43d6-949a-4e7f1a531872</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,9c7878f9-83a3-43d6-949a-4e7f1a531872.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <title>Search Engine Optimization Tools - Online!</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,9c7878f9-83a3-43d6-949a-4e7f1a531872.aspx</guid>
      <link>http://VaultOfThoughts.net/SearchEngineOptimizationToolsOnline.aspx</link>
      <pubDate>Mon, 25 Jan 2010 00:09:59 GMT</pubDate>
      <description>&lt;p&gt;
Microsoft recently release it's Search Engine Optimization Toolkit as part of the
Internet Information Services. Making SEO Toolkit part of IIS is a strange decision
to start with, and reasons for that are beyond me. To work around this "issue" I've
invested some time to make it possible to use the same functionallity online via a
web application. So with no further adieu I present to you the &lt;a href="http://seotoolsonline.net"&gt;Search
Engine Optimization Tools - Online&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://seotoolsonline.net"&gt;SEO Tools&lt;/a&gt;&lt;/p&gt; I just made available is a first
step in building a comprehensive workshop for professional web developers who are
not pro at SEO. 
&lt;p&gt;
Comments are welcome!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=9c7878f9-83a3-43d6-949a-4e7f1a531872" /&gt;</description>
      <category>.NET</category>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=237a2709-0d18-4ef9-99ed-b19d55765156</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,237a2709-0d18-4ef9-99ed-b19d55765156.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Just few days ago, my friend at work found a peculiar error message in ASP.NET <em>RangeValidator</em> control.
He was trying to validate length of a string in a <em>TextBox</em>. Controls on page
definded as follows:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">asp</span>
          <span style="COLOR: rgb(0,0,255)">:</span>
          <span style="COLOR: rgb(163,21,21)">TextBox</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(255,0,0)">ID</span>
          <span style="COLOR: rgb(0,0,255)">="TB"</span>
          <span style="COLOR: rgb(0,0,255)">/&gt;
&lt;</span>
          <span style="COLOR: rgb(163,21,21)">asp</span>
          <span style="COLOR: rgb(0,0,255)">:</span>
          <span style="COLOR: rgb(163,21,21)">RangeValidator</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(255,0,0)">ID</span>
          <span style="COLOR: rgb(0,0,255)">="RV"</span>
          <span style="COLOR: rgb(255,0,0)">MinimumValue</span>
          <span style="COLOR: rgb(0,0,255)">="3"</span>
          <span style="COLOR: rgb(255,0,0)">MaximumValue</span>
          <span style="COLOR: rgb(0,0,255)">="20"</span>
          <span style="COLOR: rgb(255,0,0)">ControlToValidate</span>
          <span style="COLOR: rgb(0,0,255)">="TB"</span>
          <span style="COLOR: rgb(255,0,0)">Type</span>
          <span style="COLOR: rgb(0,0,255)">="String"</span>
          <span style="COLOR: rgb(0,0,255)">/&gt;</span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
What he got was a best error message ever:
</p>
        <p>
"<em>The MaximumValue 20 cannot be less than the MinimumValue 3</em>"
</p>
        <p>
That's right! 20 cannot be less than 3!
</p>
        <p>
Interesting isn't it?
</p>
        <p>
I made some research of my own and I've found out that sane people should not look
inside <em>RaneValidator</em>'s (and other related classes) code! What it does inside
is it uses a <em>String.Compare</em> to check if ranges are ok.
</p>
        <p>
Of course, the problem are not the ranges, but the <em>Type</em> property of a validator.
If set to String, it treates minimum and maximum values as strings and compares them
as such - meaning that it compares character by character instead of just number to
number.
</p>
        <p>
That explains, why my friend got the error, but it certainly does not justify the
text of the message! It might take long hours for an inexperienced developer to find
where the problem lies. Come on Microsoft! You can do better than that!
</p>
        <p>
Funny thing is that I never even thought about using <em>RangeValidator</em> to ensure
string length, but suggested <em>RegularExpressionValidator</em> right from the start
:-).
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=237a2709-0d18-4ef9-99ed-b19d55765156" />
      </body>
      <title>RangeValidator Can't Count?</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,237a2709-0d18-4ef9-99ed-b19d55765156.aspx</guid>
      <link>http://VaultOfThoughts.net/RangeValidatorCantCount.aspx</link>
      <pubDate>Wed, 21 May 2008 18:44:48 GMT</pubDate>
      <description>&lt;p&gt;
Just few days ago, my friend at work found a peculiar error message in ASP.NET &lt;em&gt;RangeValidator&lt;/em&gt; control.
He was trying to validate length of a string in a &lt;em&gt;TextBox&lt;/em&gt;. Controls on page
definded as follows:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;asp&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;TextBox&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="TB"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;asp&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;RangeValidator&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="RV"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;MinimumValue&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="3"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;MaximumValue&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="20"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ControlToValidate&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="TB"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;Type&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="String"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
What he got was a best error message ever:
&lt;/p&gt;
&lt;p&gt;
"&lt;em&gt;The MaximumValue 20 cannot be less than the MinimumValue 3&lt;/em&gt;"
&lt;/p&gt;
&lt;p&gt;
That's right! 20 cannot be less than 3!
&lt;/p&gt;
&lt;p&gt;
Interesting isn't it?
&lt;/p&gt;
&lt;p&gt;
I made some research of my own and I've found out that sane people should not look
inside &lt;em&gt;RaneValidator&lt;/em&gt;'s (and other related classes) code! What it does inside
is it uses a &lt;em&gt;String.Compare&lt;/em&gt; to check if ranges are ok.
&lt;/p&gt;
&lt;p&gt;
Of course, the problem are not the ranges, but the &lt;em&gt;Type&lt;/em&gt; property of a validator.
If set to String, it treates minimum and maximum values as strings and compares them
as such - meaning that it compares character by character instead of just number to
number.
&lt;/p&gt;
&lt;p&gt;
That explains, why my friend got the error, but it certainly does not justify the
text of the message! It might take long hours for an inexperienced developer to find
where the problem lies. Come on Microsoft! You can do better than that!
&lt;/p&gt;
&lt;p&gt;
Funny thing is that I never even thought about using &lt;em&gt;RangeValidator&lt;/em&gt; to ensure
string length, but suggested &lt;em&gt;RegularExpressionValidator&lt;/em&gt; right from the start
:-).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=237a2709-0d18-4ef9-99ed-b19d55765156" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=3c91b1f9-f013-47b9-a02f-8b58712ac9db</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,3c91b1f9-f013-47b9-a02f-8b58712ac9db.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
As we all know, ASP.NET uses one form model to do all it's magic. Love it or hate
it, we have to live with it. 99% of the time, it is not a problem, but then there
is this 1%...
</p>
        <p>
The main problem with ASP.NET's model is that the <strong>form</strong> tag has a
fixed <strong>action</strong> attribute. What if we want to change it? No, there is
no simple way of doing it. Solutions vary. Most common one is to create your own FormControl
that inherits from <em>HtmlForm</em> and override <em>RenderAttributes</em> method.
Unfortunately, inside that method, we have to do everything the original method does
and only change the <strong>action</strong> attribute to the desired value. Ugly!
</p>
        <p>
Another one is to provide an OutputFilter for the Response but that is also a lot
of coding.
</p>
        <p>
My preferred way of dealing with this issue is to provide my own, special <em>HtmlTextWriter</em>.
Its role is to intercept the <em>WriteAttribute</em> method call and provide my own
value. Following code illustrates the concept:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">class</span>
          <span style="COLOR: rgb(43,145,175)">ActionAttributeWriter </span> : <span style="COLOR: rgb(43,145,175)">HtmlTextWriter </span>{ <span style="COLOR: rgb(0,0,255)">string</span> action; <span style="COLOR: rgb(0,0,255)">public</span> ActionAttributeWriter( <span style="COLOR: rgb(43,145,175)">HtmlTextWriter</span> writer, <span style="COLOR: rgb(0,0,255)">string</span> action)
: <span style="COLOR: rgb(0,0,255)">base</span>(writer) { <span style="COLOR: rgb(0,0,255)">this</span>.action
= action; } <span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,255)">override</span><span style="COLOR: rgb(0,0,255)">void</span> WriteAttribute
(<span style="COLOR: rgb(0,0,255)">string</span> name, <span style="COLOR: rgb(0,0,255)">string</span> value, <span style="COLOR: rgb(0,0,255)">bool</span> fEncode)
{ <span style="COLOR: rgb(0,0,255)">if</span> (name == <span style="COLOR: rgb(163,21,21)">"action"</span>)
{ value = action; } <span style="COLOR: rgb(0,0,255)">base</span>.WriteAttribute(name,
value, fEncode); } } </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Only thing that is needed then is to use this writer instead of a normal one. <em>Page.CreateHtmlTextWriter</em> can
be used for this purpose:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">protected</span>
          <span style="COLOR: rgb(0,0,255)">override</span>
          <span style="COLOR: rgb(43,145,175)">HtmlTextWriter</span> CreateHtmlTextWriter(System.IO.<span style="COLOR: rgb(43,145,175)">TextWriter</span> tw)
{ <span style="COLOR: rgb(43,145,175)">ActionAttributeWriter</span> writer = <span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">ActionAttributeWriter</span>( <span style="COLOR: rgb(0,0,255)">base</span>.CreateHtmlTextWriter(tw),
"action"); <span style="COLOR: rgb(0,0,255)">return</span> writer; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Solution is not very clean and I would certainly not recommend it for a public product,
but it's better than nothing.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=3c91b1f9-f013-47b9-a02f-8b58712ac9db" />
      </body>
      <title>Overriding ASP.NET Form Action Attribute</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,3c91b1f9-f013-47b9-a02f-8b58712ac9db.aspx</guid>
      <link>http://VaultOfThoughts.net/OverridingASPNETFormActionAttribute.aspx</link>
      <pubDate>Sat, 17 May 2008 13:09:17 GMT</pubDate>
      <description>&lt;p&gt;
As we all know, ASP.NET uses one form model to do all it's magic. Love it or hate
it, we have to live with it. 99% of the time, it is not a problem, but then there
is this 1%...
&lt;/p&gt;
&lt;p&gt;
The main problem with ASP.NET's model is that the &lt;strong&gt;form&lt;/strong&gt; tag has a
fixed &lt;strong&gt;action&lt;/strong&gt; attribute. What if we want to change it? No, there is
no simple way of doing it. Solutions vary. Most common one is to create your own FormControl
that inherits from &lt;em&gt;HtmlForm&lt;/em&gt; and override &lt;em&gt;RenderAttributes&lt;/em&gt; method.
Unfortunately, inside that method, we have to do everything the original method does
and only change the &lt;strong&gt;action&lt;/strong&gt; attribute to the desired value. Ugly!
&lt;/p&gt;
&lt;p&gt;
Another one is to provide an OutputFilter for the Response but that is also a lot
of coding.
&lt;/p&gt;
&lt;p&gt;
My preferred way of dealing with this issue is to provide my own, special &lt;em&gt;HtmlTextWriter&lt;/em&gt;.
Its role is to intercept the &lt;em&gt;WriteAttribute&lt;/em&gt; method call and provide my own
value. Following code illustrates the concept:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;ActionAttributeWriter &lt;/span&gt; : &lt;span style="COLOR: rgb(43,145,175)"&gt;HtmlTextWriter &lt;/span&gt;{ &lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt; action; &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; ActionAttributeWriter( &lt;span style="COLOR: rgb(43,145,175)"&gt;HtmlTextWriter&lt;/span&gt; writer, &lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt; action)
: &lt;span style="COLOR: rgb(0,0,255)"&gt;base&lt;/span&gt;(writer) { &lt;span style="COLOR: rgb(0,0,255)"&gt;this&lt;/span&gt;.action
= action; } &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; WriteAttribute
(&lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt; name, &lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt; value, &lt;span style="COLOR: rgb(0,0,255)"&gt;bool&lt;/span&gt; fEncode)
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;if&lt;/span&gt; (name == &lt;span style="COLOR: rgb(163,21,21)"&gt;"action"&lt;/span&gt;)
{ value = action; } &lt;span style="COLOR: rgb(0,0,255)"&gt;base&lt;/span&gt;.WriteAttribute(name,
value, fEncode); } } &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Only thing that is needed then is to use this writer instead of a normal one. &lt;em&gt;Page.CreateHtmlTextWriter&lt;/em&gt; can
be used for this purpose:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;HtmlTextWriter&lt;/span&gt; CreateHtmlTextWriter(System.IO.&lt;span style="COLOR: rgb(43,145,175)"&gt;TextWriter&lt;/span&gt; tw)
{ &lt;span style="COLOR: rgb(43,145,175)"&gt;ActionAttributeWriter&lt;/span&gt; writer = &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;ActionAttributeWriter&lt;/span&gt;( &lt;span style="COLOR: rgb(0,0,255)"&gt;base&lt;/span&gt;.CreateHtmlTextWriter(tw),
"action"); &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; writer; }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Solution is not very clean and I would certainly not recommend it for a public product,
but it's better than nothing.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=3c91b1f9-f013-47b9-a02f-8b58712ac9db" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=413d5fda-2596-43a7-b942-266afb85924c</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,413d5fda-2596-43a7-b942-266afb85924c.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
While working with any kind of configuration files in .NET, be it Web.config in case
of ASP.NET or App.config in case of Windows Forms applications, we very often use
the appSettings section. We use it to store all kinds of simple configuration options.
Options, that are too simple for us to implement a completely new SectionHandler type.
There is however one problem with appSettings section not very expressive.
</p>
        <p>
What does it mean to be expressive? Consider the following example:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">appSettings</span>
          <span style="COLOR: rgb(0,0,255)">&gt;
&lt;</span>
          <span style="COLOR: rgb(163,21,21)">add</span>
          <span style="COLOR: rgb(0,0,255)">
          </span>
          <span style="COLOR: rgb(255,0,0)">key</span>
          <span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">source1user</span>"<span style="COLOR: rgb(0,0,255)"></span><span style="COLOR: rgb(255,0,0)">value</span><span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">user</span>"<span style="COLOR: rgb(0,0,255)"> /&gt;<br /><span style="COLOR: rgb(0,0,255)"> &lt;</span><span style="COLOR: rgb(163,21,21)">add</span><span style="COLOR: rgb(0,0,255)"></span><span style="COLOR: rgb(255,0,0)">key</span><span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">source1password</span>"<span style="COLOR: rgb(0,0,255)"></span><span style="COLOR: rgb(255,0,0)">value</span><span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">pass</span>"<span style="COLOR: rgb(0,0,255)"> /&gt;</span> &lt;/</span><span style="COLOR: rgb(163,21,21)">appSettings</span><span style="COLOR: rgb(0,0,255)">&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
In simple cases, it may be acceptable, but what happens if more user keys are
required for some reason? Maybe we need to access few different locations, each of
which requires a username and password? We may use some kind of prefix for each
key like in the above example, but that is not very elegant. What is <em>key</em> and <em>value</em> anyway?
</p>
        <p>
Another obvious problem here is when we need to have 2 parameters associated with
a single logical functionality in the application, we need 2 entries in appSettings
section.
</p>
        <p>
If we still don't want to implement a new SectionHandler type, we have very nice option
left: <em>SingleTagSectionHandler</em>. 
</p>
        <p>
MSDN describes it as: "Handles configuration sections that are represented by a single
XML tag in the .config file". And that's about it. Unfortunately, (as usual) MSDN
provides no example of how to use it. Fortunately it is quite simple:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">configSections</span>
          <span style="COLOR: rgb(0,0,255)">&gt;
&lt;</span>
          <span style="COLOR: rgb(163,21,21)">section</span>
          <span style="COLOR: rgb(0,0,255)">
          </span>
          <span style="COLOR: rgb(255,0,0)">name</span>
          <span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">remoteDataSource</span>"<span style="COLOR: rgb(0,0,255)"></span><span style="COLOR: rgb(255,0,0)">type</span><span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">System.Configuration.SingleTagSectionHandler</span>"<span style="COLOR: rgb(0,0,255)"> /&gt;
&lt;/</span><span style="COLOR: rgb(163,21,21)">configSections</span><span style="COLOR: rgb(0,0,255)">&gt;
&lt;</span><span style="COLOR: rgb(163,21,21)">remoteDataSource</span><span style="COLOR: rgb(0,0,255)"></span><span style="COLOR: rgb(255,0,0)">username</span><span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">user</span>"<span style="COLOR: rgb(0,0,255)"></span><span style="COLOR: rgb(255,0,0)">password</span><span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">pass</span>"<span style="COLOR: rgb(0,0,255)"></span><span style="COLOR: rgb(255,0,0)">url</span><span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">http://remote/</span>"<span style="COLOR: rgb(0,0,255)"> /&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Using the newly declared section from the code is also easy:
</p>
        <pre class="code">
          <span style="COLOR: rgb(43,145,175)">Hashtable</span> remoteDataSource
= (<span style="COLOR: rgb(43,145,175)">Hashtable</span>)<span style="COLOR: rgb(43,145,175)">WebConfigurationManager</span>.GetSection(<span style="COLOR: rgb(163,21,21)">"remoteDataSource"</span>); <span style="COLOR: rgb(0,0,255)">string</span> username
= (<span style="COLOR: rgb(0,0,255)">string</span>)remoteDataSource[<span style="COLOR: rgb(163,21,21)">"username"</span>]; <span style="COLOR: rgb(0,0,255)">string</span> password
= (<span style="COLOR: rgb(0,0,255)">string</span>)remoteDataSource[<span style="COLOR: rgb(163,21,21)">"password"</span>]; <span style="COLOR: rgb(0,0,255)">string</span> url
= (<span style="COLOR: rgb(0,0,255)">string</span>)remoteDataSource[<span style="COLOR: rgb(163,21,21)">"url"</span>];</pre>
        <a href="http://11011.net/software/vspaste">
          <a href="http://11011.net/software/vspaste">
          </a>
          <p>
Simple, yet useful.
</p>
          <p>
            <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/UsingSingleTagSectionHandlerInsteadOfAppSettings.aspx">
              <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/UsingSingleTagSectionHandlerInsteadOfAppSettings.aspx" border="0" alt="kick it on DotNetKicks.com" />
            </a>
          </p>
          <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=413d5fda-2596-43a7-b942-266afb85924c" />
        </a>
      </body>
      <title>Using SingleTagSectionHandler Instead Of appSettings</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,413d5fda-2596-43a7-b942-266afb85924c.aspx</guid>
      <link>http://VaultOfThoughts.net/UsingSingleTagSectionHandlerInsteadOfAppSettings.aspx</link>
      <pubDate>Wed, 05 Sep 2007 19:45:40 GMT</pubDate>
      <description>&lt;p&gt;
While working with any kind of configuration files in .NET, be it Web.config in case
of ASP.NET or App.config in case of Windows Forms applications, we very often use
the appSettings section. We use it to store all kinds of simple configuration options.
Options, that are too simple for us to implement a completely new SectionHandler type.
There is however one problem with appSettings section not very expressive.
&lt;/p&gt;
&lt;p&gt;
What does it mean to be expressive? Consider the following example:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;appSettings&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;add&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;key&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;source1user&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;value&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;user&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; /&amp;gt;&lt;br&gt;
&lt;span style="COLOR: rgb(0,0,255)"&gt; &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;add&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;key&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;source1password&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;value&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;pass&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; /&amp;gt;&lt;/span&gt; &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;appSettings&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
In&amp;nbsp;simple&amp;nbsp;cases, it may be acceptable, but what happens if more user keys&amp;nbsp;are
required for some reason? Maybe we need to access few different locations, each of
which requires a username and password? We may use some kind of prefix&amp;nbsp;for each
key like in the above example, but that is not very elegant. What is &lt;em&gt;key&lt;/em&gt; and &lt;em&gt;value&lt;/em&gt; anyway?
&lt;/p&gt;
&lt;p&gt;
Another obvious problem here is when we need to have 2 parameters associated with
a single logical functionality in the application, we need 2 entries in appSettings
section.
&lt;/p&gt;
&lt;p&gt;
If we still don't want to implement a new SectionHandler type, we have very nice option
left: &lt;em&gt;SingleTagSectionHandler&lt;/em&gt;. 
&lt;/p&gt;
&lt;p&gt;
MSDN describes it as: "Handles configuration sections that are represented by a single
XML tag in the .config file". And that's about it. Unfortunately, (as usual) MSDN
provides no example of how to use it. Fortunately it is quite simple:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;configSections&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;section&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;name&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;remoteDataSource&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;type&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;System.Configuration.SingleTagSectionHandler&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; /&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;configSections&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;remoteDataSource&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;username&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;user&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;password&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;pass&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;url&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;http://remote/&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; /&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Using the newly declared section from the code is also easy:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(43,145,175)"&gt;Hashtable&lt;/span&gt; remoteDataSource
= (&lt;span style="COLOR: rgb(43,145,175)"&gt;Hashtable&lt;/span&gt;)&lt;span style="COLOR: rgb(43,145,175)"&gt;WebConfigurationManager&lt;/span&gt;.GetSection(&lt;span style="COLOR: rgb(163,21,21)"&gt;"remoteDataSource"&lt;/span&gt;); &lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt; username
= (&lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt;)remoteDataSource[&lt;span style="COLOR: rgb(163,21,21)"&gt;"username"&lt;/span&gt;]; &lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt; password
= (&lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt;)remoteDataSource[&lt;span style="COLOR: rgb(163,21,21)"&gt;"password"&lt;/span&gt;]; &lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt; url
= (&lt;span style="COLOR: rgb(0,0,255)"&gt;string&lt;/span&gt;)remoteDataSource[&lt;span style="COLOR: rgb(163,21,21)"&gt;"url"&lt;/span&gt;];&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Simple, yet useful.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/UsingSingleTagSectionHandlerInsteadOfAppSettings.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/UsingSingleTagSectionHandlerInsteadOfAppSettings.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=413d5fda-2596-43a7-b942-266afb85924c" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=1e6f54ba-ec78-440e-b875-5baaa9d4e8c3</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,1e6f54ba-ec78-440e-b875-5baaa9d4e8c3.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
OnClientClick is a useful property introduced in ASP.NET 2.0 that allows us to
add some client-side behavior to button control. Using is as simple as providing
the script to be called when a button is clicked by a user:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">asp</span>
          <span style="COLOR: rgb(0,0,255)">:</span>
          <span style="COLOR: rgb(163,21,21)">Button</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(255,0,0)">ID</span>
          <span style="COLOR: rgb(0,0,255)">="Save"</span>
          <span style="COLOR: rgb(255,0,0)">OnClientClick</span>
          <span style="COLOR: rgb(0,0,255)">="return
confirm('Are you sure?');"</span>
          <span style="COLOR: rgb(0,0,255)">/&gt;</span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
The problem is that if you use it like that, client-side validation won't fire. Looking
at the rendered HTML quickly explains the situation:
</p>
        <p>
onclick="return confirm('Are you sure?');WebForm_DoPostBackWithOptions(...)"
</p>
        <p>
As you can see, the validation doesn't even have a chance to fire (which happens when
WebForm_DoPostBackWithOptions is called).
</p>
        <p>
Solving the issue is simple (or not). All that has to be done is a little change in
our OnClientClick script (a piece of code found somewhere on the Internet):
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">asp</span>
          <span style="COLOR: rgb(0,0,255)">:</span>
          <span style="COLOR: rgb(163,21,21)">Button</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(255,0,0)">ID</span>
          <span style="COLOR: rgb(0,0,255)">="Save"</span>
          <span style="COLOR: rgb(255,0,0)">OnClientClick</span>
          <span style="COLOR: rgb(0,0,255)">="if
(!confirm('Are you sure?')) return false;"</span>
          <span style="COLOR: rgb(0,0,255)">/&gt;</span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Now we only return false (preventing the submit) in case a user didn't confirm the
action, otherwise, the rest of the script will be called thus firing validation.
</p>
        <p>
The reason I said that it may not be a simple issue is the fact, that the validation
happens AFTER the confirmation, which is not the best thing in my opinion. Why ask
the user about saving his data if there are still errors on the form, of which we
will inform him after he confirms that he wants to save it?
</p>
        <p>
After analyzing a bit, the code responsible for dealing with OnClientScript, I have
come to a conclusion, that solving this problem is not an easy task. It would require
some dirty hacks on the server side to make it pretty or calling validation routines
on the client, before displaying the confirmation dialog (keeping in mind that checking
if there are validation routines present at all is necessary in this case). 
</p>
        <p>
I've just left it as it is. After all it's only a minor inconvenience - at least in
my case.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=1e6f54ba-ec78-440e-b875-5baaa9d4e8c3" />
      </body>
      <title>OnClientClick Breaks Validation</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,1e6f54ba-ec78-440e-b875-5baaa9d4e8c3.aspx</guid>
      <link>http://VaultOfThoughts.net/OnClientClickBreaksValidation.aspx</link>
      <pubDate>Thu, 09 Aug 2007 09:38:51 GMT</pubDate>
      <description>&lt;p&gt;
OnClientClick is&amp;nbsp;a useful property introduced in ASP.NET 2.0 that allows us to
add some client-side behavior to&amp;nbsp;button control. Using is as simple as providing
the script to be called when a button is clicked by a user:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;asp&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Button&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Save"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;OnClientClick&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="return
confirm('Are you sure?');"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The problem is that if you use it like that, client-side validation won't fire. Looking
at the rendered HTML quickly explains the situation:
&lt;/p&gt;
&lt;p&gt;
onclick="return confirm('Are you sure?');WebForm_DoPostBackWithOptions(...)"
&lt;/p&gt;
&lt;p&gt;
As you can see, the validation doesn't even have a chance to fire (which happens when
WebForm_DoPostBackWithOptions is called).
&lt;/p&gt;
&lt;p&gt;
Solving the issue is simple (or not). All that has to be done is a little change in
our OnClientClick script (a piece of code found somewhere on the Internet):
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;asp&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Button&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Save"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;OnClientClick&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="if
(!confirm('Are you sure?')) return false;"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Now we only return false (preventing the submit) in case a user didn't confirm the
action, otherwise, the rest of the script will be called thus firing validation.
&lt;/p&gt;
&lt;p&gt;
The reason I said that it may not be a simple issue is the fact, that the validation
happens AFTER the confirmation, which is not the best thing in my opinion. Why ask
the user about saving his data if there are still errors on the form, of which we
will inform him after he confirms that he wants to save it?
&lt;/p&gt;
&lt;p&gt;
After analyzing a bit, the code responsible for dealing with OnClientScript, I have
come to a conclusion, that solving this problem is not an easy task. It would require
some dirty hacks on the server side to make it pretty or calling validation routines
on the client, before displaying the confirmation dialog (keeping in mind that checking
if there are validation routines present at all is necessary in this case). 
&lt;/p&gt;
&lt;p&gt;
I've just left it as it is. After all it's only a minor inconvenience - at least in
my case.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=1e6f54ba-ec78-440e-b875-5baaa9d4e8c3" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=eee3e56b-d7ca-4fff-8758-1daae77898c7</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,eee3e56b-d7ca-4fff-8758-1daae77898c7.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
ASP.NET Ajax comes with a nice way to attach to a load event of a page. To do it just
call Sys.Application.add_load and pass it a function as an argument. Done. Everything
works as expected... WRONG!
</p>
        <p>
It comes out that add_load fires on each callback and not only when the page loads
for the first time. That is of course a problem if you want something to happen on
page load only.
</p>
        <p>
From what I've been able to learn, this behavior is by design. Although it is never
mentioned in a strigh forward way in a description:
</p>
        <p>
"Raised after all scripts have been loaded and after the objects in the application
have been created and initialized."
</p>
        <p>
There are some places in the documentation, that suggests that this is a way it was
intended to be. First of all, ASP.NET Ajax tries to mimic that ASP.NET Page life cycle
and as such, fires the load event every time there is a callback. The other thing
that makes me believe, that load's behavior is there by design is this:
</p>
        <p>
"You can use the event arguments to determine whether the page is being refreshed
as a result of a partial-page update and what components were created since the previous
load event was raised."
</p>
        <p>
So now I know, but I still cannot accept the naming convention. Sys.Application plainly
suggests, it's a kind of singleton, and not a per load thingy.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=eee3e56b-d7ca-4fff-8758-1daae77898c7" />
      </body>
      <title>Sys.Application.add_load Fires On Each Callback</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,eee3e56b-d7ca-4fff-8758-1daae77898c7.aspx</guid>
      <link>http://VaultOfThoughts.net/SysApplicationaddloadFiresOnEachCallback.aspx</link>
      <pubDate>Tue, 07 Aug 2007 11:18:19 GMT</pubDate>
      <description>&lt;p&gt;
ASP.NET Ajax comes with a nice way to attach to a load event of a page. To do it just
call Sys.Application.add_load and pass it a function as an argument. Done. Everything
works as expected... WRONG!
&lt;/p&gt;
&lt;p&gt;
It comes out that add_load fires on each callback and not only when the page loads
for the first time. That is of course a problem if you want something to happen on
page load only.
&lt;/p&gt;
&lt;p&gt;
From what I've been able to learn, this behavior is by design. Although it is never
mentioned in a strigh forward way in a description:
&lt;/p&gt;
&lt;p&gt;
"Raised after all scripts have been loaded and after the objects in the application
have been created and initialized."
&lt;/p&gt;
&lt;p&gt;
There are some places in the documentation, that suggests that this is a way it was
intended to be. First of all, ASP.NET Ajax tries to mimic that ASP.NET Page life cycle
and as such, fires the load event every time there is a callback. The other thing
that makes me believe, that load's behavior is there by design is this:
&lt;/p&gt;
&lt;p&gt;
"You can use the event arguments to determine whether the page is being refreshed
as a result of a partial-page update and what components were created since the previous
load event was raised."
&lt;/p&gt;
&lt;p&gt;
So now I know, but I still cannot accept the naming convention. Sys.Application plainly
suggests, it's a kind of singleton, and not a per load thingy.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=eee3e56b-d7ca-4fff-8758-1daae77898c7" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=c177d3dd-1041-416a-9567-74706e96dbcb</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,c177d3dd-1041-416a-9567-74706e96dbcb.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
For a couple of months now I've been running Windows Vista and Visual Studio 2005
without major problems (other than the commonly known ones). Since the first day of
this setup I have been working on solutions that inlcude from one to many ASP.NET
web applications. All of them are configured to use IIS rather than the built in web
server. For all that time, everything was working fine until recently...
</p>
        <p>
One of those days, while opening one of the solutions in Visual Studio, I was presented
with a message telling me that my site is configured to use ASP.NET 1.1:
</p>
        <p>
---------------------------<br />
Microsoft Visual Studio<br />
---------------------------<br />
The site 'http://localhost/WebSite' is currently configured for use with ASP.NET 1.1.4322.573.
Microsoft Visual Studio has been designed for use with ASP.NET 2.0; if not configured
some features may make incorrect assumptions, and pages designed with the tool may
not render correctly. 
</p>
        <p>
Would you like the site to be configured for use with ASP.NET 2.0?<br />
---------------------------<br />
Yes   No   Cancel   Help   
<br />
--------------------------- 
</p>
        <p>
Needles to say that there were no changes made to IIS configuration of Application
Pools or the pool the site is running on, but just to be sure, I've checked IIS Manager.
The pool for the site was ASP.NET V2.0. I have agreed and allowed Visual Studio to
make the "necessary" change. Needles to say, that nothing was changed in scope of
application pools by Visual Studio. Fortunately, my solution kept working after that...
for few days. 
</p>
        <p>
After few days I've got the very same message. Again, nothing was changed in IIS.
This time I decided to leave my supposedly ASP.NET 1.1 in place. Of course everything
worked after that also. Including debugging! But then I have the same message every
time I open any of the solutions :-(. 
</p>
        <p>
I have found that other people also had this problem, and "aspnet_regiis -i" helped
them, but it doesn't work for me :-(
</p>
        <p>
Today I noticed that it gets worse. Now I get the message every time I open any of
my solutions that include web sites :-(
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=c177d3dd-1041-416a-9567-74706e96dbcb" />
      </body>
      <title>Site Configured for use with ASP.NET 1.1.4322.573</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,c177d3dd-1041-416a-9567-74706e96dbcb.aspx</guid>
      <link>http://VaultOfThoughts.net/SiteConfiguredForUseWithASPNET114322573.aspx</link>
      <pubDate>Fri, 20 Jul 2007 13:05:07 GMT</pubDate>
      <description>&lt;p&gt;
For a couple of months now I've been running Windows Vista and Visual Studio 2005
without major problems (other than the commonly known ones). Since the first day of
this setup I have been working on solutions that inlcude from one to many ASP.NET
web applications. All of them are configured to use IIS rather than the built in web
server. For all that time, everything was working fine until recently...
&lt;/p&gt;
&lt;p&gt;
One of those days, while opening one of the solutions in Visual Studio, I was presented
with a message telling me that my site is configured to use ASP.NET 1.1:
&lt;/p&gt;
&lt;p&gt;
---------------------------&lt;br&gt;
Microsoft Visual Studio&lt;br&gt;
---------------------------&lt;br&gt;
The site 'http://localhost/WebSite' is currently configured for use with ASP.NET 1.1.4322.573.
Microsoft Visual Studio has been designed for use with ASP.NET 2.0; if not configured
some features may make incorrect assumptions, and pages designed with the tool may
not render correctly. 
&lt;p&gt;
Would you like the site to be configured for use with ASP.NET 2.0?&lt;br&gt;
---------------------------&lt;br&gt;
Yes&amp;nbsp;&amp;nbsp; No&amp;nbsp;&amp;nbsp; Cancel&amp;nbsp;&amp;nbsp; Help&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
--------------------------- 
&lt;p&gt;
Needles to say that there were no changes made to IIS configuration of Application
Pools or the pool the site is running on, but just to be sure, I've checked IIS Manager.
The pool for the site was ASP.NET V2.0. I have agreed and allowed Visual Studio to
make the "necessary" change. Needles to say, that nothing was changed in scope of
application pools by Visual Studio. Fortunately, my solution kept working after that...
for few days. 
&lt;p&gt;
After few days I've got the very same message. Again, nothing was changed in IIS.
This time I decided to leave my supposedly ASP.NET 1.1 in place. Of course everything
worked after that also. Including debugging! But then I have the same message every
time I open any of the solutions :-(. 
&lt;p&gt;
I have found that other people also had this problem, and "aspnet_regiis -i" helped
them, but it doesn't work for me :-(
&lt;/p&gt;
&lt;p&gt;
Today I noticed that it gets worse. Now I get the message every time I open any of
my solutions that include web sites :-(
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=c177d3dd-1041-416a-9567-74706e96dbcb" /&gt;</description>
      <category>ASP.NET</category>
      <category>Misc</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=b1300df8-1c7c-4a22-ae13-130995589c17</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,b1300df8-1c7c-4a22-ae13-130995589c17.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
ASP.NET despite its many nice features, has also a lot of nasty ones. Some of them
are critical - like a design flaw in the way controls are rendered - by the controls
themselves (only in version ASP.NET 2.0 Microsoft introduced the concept of Adapters,
but this thing is beyond repair. There is just to much logic already in <em>Render</em> methods
of all the existing controls to make using adapters easy). Others are just plain annoying,
but we mostly we can live with them and even find workarounds.
</p>
        <p>
Today I simply had enough of the <em>style="border-width:0px;"</em> attribute that
ASP.NET adds to every img tag it renders from the <em>Image</em> control. There is
strightforward way (that I know of) to remove this thing! Try as you might, there
will always be a border-width style attribute on you images with either 0px, or whatever
value you assign in you ASPX file. But there is a workaround.
</p>
        <p>
To make the border style gone, we have to create our own Image control (inheriting
controls is usually a good thing). In this control we have to resort to a trick of
overriding the <em>BorderWidth</em> property by returning anything other than <em>Unit.Empty</em>:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">public</span>
          <span style="COLOR: rgb(0,0,255)">override</span>
          <span style="COLOR: rgb(43,145,175)">Unit</span> BorderWidth
{ <span style="COLOR: rgb(0,0,255)">get</span> { <span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(43,145,175)">Unit</span>.Pixel(0);
} <span style="COLOR: rgb(0,0,255)">set</span> { ; } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
This will cause the border style not to be rendered at all. Additional side effect
which is obvious when looking at the above code is that now it is impossible to set <em>BorderWidth</em> property
in any way. The value will alwyas be <em>Unit.Pixel(0)</em>. This is also a good thing
since we really shouldn't set any of the style properties inline but rather use a
css style sheets for this purpose.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b1300df8-1c7c-4a22-ae13-130995589c17" />
      </body>
      <title>Getting Rid Of ASP.NET Image Control Border</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,b1300df8-1c7c-4a22-ae13-130995589c17.aspx</guid>
      <link>http://VaultOfThoughts.net/GettingRidOfASPNETImageControlBorder.aspx</link>
      <pubDate>Sun, 15 Jul 2007 13:39:59 GMT</pubDate>
      <description>&lt;p&gt;
ASP.NET despite its many nice features, has also a lot of nasty ones. Some of them
are critical - like a design flaw in the way controls are rendered - by the controls
themselves (only in version ASP.NET 2.0 Microsoft introduced the concept of Adapters,
but this thing is beyond repair. There is just to much logic already in &lt;em&gt;Render&lt;/em&gt; methods
of all the existing controls to make using adapters easy). Others are just plain annoying,
but we mostly we can live with them and even find workarounds.
&lt;/p&gt;
&lt;p&gt;
Today I simply had enough of the &lt;em&gt;style="border-width:0px;"&lt;/em&gt; attribute that
ASP.NET adds to every img tag it renders from the &lt;em&gt;Image&lt;/em&gt; control. There is
strightforward way (that I know of) to remove this thing! Try as you might, there
will always be a border-width style attribute on you images with either 0px, or whatever
value you assign in you ASPX file. But there is a workaround.
&lt;/p&gt;
&lt;p&gt;
To make the border style gone, we have to create our own Image control (inheriting
controls is usually a good thing). In this control we have to resort to a trick of
overriding the &lt;em&gt;BorderWidth&lt;/em&gt; property by returning anything other than &lt;em&gt;Unit.Empty&lt;/em&gt;:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;Unit&lt;/span&gt; BorderWidth
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;get&lt;/span&gt; { &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;Unit&lt;/span&gt;.Pixel(0);
} &lt;span style="COLOR: rgb(0,0,255)"&gt;set&lt;/span&gt; { ; } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
This will cause the border style not to be rendered at all. Additional side effect
which is obvious when looking at the above code is that now it is impossible to set &lt;em&gt;BorderWidth&lt;/em&gt; property
in any way. The value will alwyas be &lt;em&gt;Unit.Pixel(0)&lt;/em&gt;. This is also a good thing
since we really shouldn't set any of the style properties inline but rather use a
css style sheets for this purpose.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b1300df8-1c7c-4a22-ae13-130995589c17" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=d7599449-1279-4afb-9174-c339b403bc76</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,d7599449-1279-4afb-9174-c339b403bc76.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
From time to time, there is a need to get something from a file system in our ASP.NET
applications. In those cases, we use the <em>Server.MapPath()</em> call on our pages
classes. But what about class libraries?
</p>
        <p>
Usually in class libraries in order to get the physical path of a file on disk we
can use something like <em>HttpContext.Current.Server.MapPath()</em> and it works
as long as there is a context i.e.: <em>HttpContext.Current</em> is not null.
Sometimes however it may happen that there is a task running on the server which does
some work without any request being processed. What then?
</p>
        <p>
Enter the <em><strong>HostingEnvironment.MapPath()</strong></em> method where <em>HostingEnvironment</em> is
a class defined in <em>System.Web.Hosting</em> namespace. It works the same way, as <em>Server.MapPath()</em> but
it is a static method requiring no context to be present.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=d7599449-1279-4afb-9174-c339b403bc76" />
      </body>
      <title>Static Version Of Server.MapPath</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,d7599449-1279-4afb-9174-c339b403bc76.aspx</guid>
      <link>http://VaultOfThoughts.net/StaticVersionOfServerMapPath.aspx</link>
      <pubDate>Sat, 14 Jul 2007 16:46:21 GMT</pubDate>
      <description>&lt;p&gt;
From time to time, there is a need to get something from a file system in our ASP.NET
applications. In those cases, we use the &lt;em&gt;Server.MapPath()&lt;/em&gt; call on our pages
classes. But what about class libraries?
&lt;/p&gt;
&lt;p&gt;
Usually in class libraries in order to get the physical path of a file on disk we
can use something like &lt;em&gt;HttpContext.Current.Server.MapPath()&lt;/em&gt; and it works
as long as there is a context&amp;nbsp;i.e.: &lt;em&gt;HttpContext.Current&lt;/em&gt; is not null.
Sometimes however it may happen that there is a task running on the server which does
some work without any request being processed. What then?
&lt;/p&gt;
&lt;p&gt;
Enter the &lt;em&gt;&lt;strong&gt;HostingEnvironment.MapPath()&lt;/strong&gt;&lt;/em&gt; method where &lt;em&gt;HostingEnvironment&lt;/em&gt; is
a class defined in &lt;em&gt;System.Web.Hosting&lt;/em&gt; namespace. It works the same way, as &lt;em&gt;Server.MapPath()&lt;/em&gt; but
it is a static method requiring no context to be present.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=d7599449-1279-4afb-9174-c339b403bc76" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=b0395d7c-2b8b-481e-94ab-93ada4a02ae4</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,b0395d7c-2b8b-481e-94ab-93ada4a02ae4.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
ASP.NET Pages offer us a declarative way to set some of the properties such as Theme,
MasterPageFile or Title in the Page directive. A nice feature indeed. The problem
is that the same ASP.NET or maybe the Web Forms framework and Visual Studio stand
in our way when we try to set our custom properties this way. It is possible but not
developer friendly in a least.
</p>
        <p>
To do it, we have to use the CodeFileBaseClass property and set it to the same base
class that our page inherits from. More on this on <a href="http://odetocode.com/Blogs/scott/archive/2006/07/03/4762.aspx">K.
Scott Allen's blog</a>. Although it is technically possible to set this attribute
to some class upper in the hierarchy I have encountered random weird behavior of Visual
Studio after doing so. To be safe it is better to use the same base class both in
the inheritance and in the CodeFileBaseClass attribute.
</p>
        <p>
What about generic types? The tool (Visual Studio) support for generic types has never
been great (not to say there is virtually no support). The fact that Visual Studio
makes it hard to use CodeFileBaseClass difficult does not make it easier when it comes
to generic base pages, but it can be done!
</p>
        <p>
Suppose we have a type in the App_Code directory:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">public</span>
          <span style="COLOR: rgb(0,0,255)">class</span>
          <span style="COLOR: rgb(43,145,175)">MyType </span>{
}</pre>
        <pre class="code">And also, defined in the same directory, we have a base page class:</pre>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">public</span>
          <span style="COLOR: rgb(0,0,255)">abstract</span>
          <span style="COLOR: rgb(0,0,255)">class</span>
          <span style="COLOR: rgb(43,145,175)">BasePage</span>&lt;T&gt;
: <span style="COLOR: rgb(43,145,175)">Page </span>{ }</pre>
        <a href="http://11011.net/software/vspaste">
          <a href="http://11011.net/software/vspaste">
          </a>
          <p>
And finally there is a Default.aspx page in the project that inherits from the our
BasePage class:
</p>
          <pre class="code">
            <span style="COLOR: rgb(0,0,255)">public</span>
            <span style="COLOR: rgb(0,0,255)">partial</span>
            <span style="COLOR: rgb(0,0,255)">class</span>
            <span style="COLOR: rgb(43,145,175)">_Default</span> : <span style="COLOR: rgb(43,145,175)">BasePage</span>&lt;<span style="COLOR: rgb(43,145,175)">MyType</span>&gt;
{ }</pre>
          <a href="http://11011.net/software/vspaste">
          </a>
          <p>
Obviously we cannot just put a type name as BasePage:
</p>
          <pre class="code">
            <span style="COLOR: rgb(255,0,0)">CodeFileBaseClass</span>
            <span style="COLOR: rgb(0,0,255)">="BasePage"</span>
          </pre>
          <a href="http://11011.net/software/vspaste">
          </a>
          <p>
It won't work, because there is no such type. The message is: "Could not load type
'BasePage'". Our type is generic so we have to use it's full name:
</p>
          <pre class="code">
            <span style="COLOR: rgb(255,0,0)">CodeFileBaseClass</span>
            <span style="COLOR: rgb(0,0,255)">="BasePage&lt;MyType&gt;"</span>
          </pre>
          <p>
            <a href="http://11011.net/software/vspaste">
            </a>But this also won't work. It will
fail with the same message. That's becuse it is a C# specific representation
of a generic type name. What CLR sees is something different. In order to make it
work with a generic class we have to lower ourselves to the CLR level and use it's
notation. This would mean using something like:
</p>
          <pre class="code">
            <span style="COLOR: rgb(255,0,0)">CodeFileBaseClass</span>
            <span style="COLOR: rgb(0,0,255)">="BasePage`1[[MyTypebecause]]"</span>
          </pre>
          <a href="http://11011.net/software/vspaste">
          </a>
          <p>
This thing actually works! If your types reside in some kind of namespace, has
many generic parameters or worse yet, is part of a strongly named assembly, the
whole CodeFileBaseClass starts to look scary and I don't think it is worth it. There
is a trick however that may civilize the thing you enter in CodeFileBaseClass that
allows to use any kind of base page class without making it complex. Just create an
intermediate class like this:
</p>
          <pre class="code">
            <span style="COLOR: rgb(0,0,255)">public</span>
            <span style="COLOR: rgb(0,0,255)">class</span>
            <span style="COLOR: rgb(43,145,175)">StandardBasePage</span> : <span style="COLOR: rgb(43,145,175)">BasePage</span>&lt;<span style="COLOR: rgb(43,145,175)">MyType</span>&gt;
{ }</pre>
          <a href="http://11011.net/software/vspaste">
          </a>
          <p>
and inherit your page from it. Than you can use this new type's name in CodeFileBaseClass
without having to deal with generic parameter issues.
</p>
          <p>
            <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/CodeFileBaseClassAndGenericTypes.aspx">
              <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/CodeFileBaseClassAndGenericTypes.aspx" border="0" />
            </a>
          </p>
          <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b0395d7c-2b8b-481e-94ab-93ada4a02ae4" />
        </a>
      </body>
      <title>CodeFileBaseClass And Generic Types</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,b0395d7c-2b8b-481e-94ab-93ada4a02ae4.aspx</guid>
      <link>http://VaultOfThoughts.net/CodeFileBaseClassAndGenericTypes.aspx</link>
      <pubDate>Thu, 12 Jul 2007 15:56:35 GMT</pubDate>
      <description>&lt;p&gt;
ASP.NET Pages offer us a declarative way to set some of the properties such as Theme,
MasterPageFile or Title in the Page directive. A nice feature indeed. The problem
is that the same ASP.NET or maybe the Web Forms framework and Visual Studio stand
in our way when we try to set our custom properties this way. It is possible but not
developer friendly in a least.
&lt;/p&gt;
&lt;p&gt;
To do it, we have to use the CodeFileBaseClass property and set it to the same base
class that our page inherits from. More on this on &lt;a href="http://odetocode.com/Blogs/scott/archive/2006/07/03/4762.aspx"&gt;K.
Scott Allen's blog&lt;/a&gt;. Although it is technically possible to set this attribute
to some class upper in the hierarchy I have encountered random weird behavior of Visual
Studio after doing so. To be safe it is better to use the same base class both in
the inheritance and in the CodeFileBaseClass attribute.
&lt;/p&gt;
&lt;p&gt;
What about generic types? The tool (Visual Studio) support for generic types has never
been great (not to say there is virtually no support). The fact that Visual Studio
makes it hard to use CodeFileBaseClass difficult does not make it easier when it comes
to generic base pages, but it can be done!
&lt;/p&gt;
&lt;p&gt;
Suppose we have a type in the App_Code directory:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;MyType &lt;/span&gt;{
}&lt;/pre&gt;
&lt;pre class=code&gt;And also, defined in the same directory, we have a base page class:&lt;/pre&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;abstract&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;BasePage&lt;/span&gt;&amp;lt;T&amp;gt;
: &lt;span style="COLOR: rgb(43,145,175)"&gt;Page &lt;/span&gt;{ }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
And finally there is a Default.aspx page in the project that inherits from the our
BasePage class:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;partial&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;_Default&lt;/span&gt; : &lt;span style="COLOR: rgb(43,145,175)"&gt;BasePage&lt;/span&gt;&amp;lt;&lt;span style="COLOR: rgb(43,145,175)"&gt;MyType&lt;/span&gt;&amp;gt;
{ }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Obviously we cannot just put a type name as BasePage:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;CodeFileBaseClass&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="BasePage"&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
It won't work, because there is no such type. The message is: "Could not load type
'BasePage'". Our type is generic so we have to use it's full name:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;CodeFileBaseClass&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="BasePage&amp;lt;MyType&amp;gt;"&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;But this also won't work. It&amp;nbsp;will
fail&amp;nbsp;with the same message.&amp;nbsp;That's becuse it is a C# specific representation
of a generic type name. What CLR sees is something different. In order to make it
work with a generic class we have to lower ourselves to the CLR level and use it's
notation. This would mean using something like:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;CodeFileBaseClass&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="BasePage`1[[MyTypebecause]]"&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
This thing actually works! If your types reside in some kind of namespace,&amp;nbsp;has
many generic parameters or worse yet, is part of a strongly&amp;nbsp;named assembly, the
whole CodeFileBaseClass starts to look scary and I don't think it is worth it. There
is a trick however that may civilize the thing you enter in CodeFileBaseClass that
allows to use any kind of base page class without making it complex. Just create an
intermediate class like this:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;StandardBasePage&lt;/span&gt; : &lt;span style="COLOR: rgb(43,145,175)"&gt;BasePage&lt;/span&gt;&amp;lt;&lt;span style="COLOR: rgb(43,145,175)"&gt;MyType&lt;/span&gt;&amp;gt;
{ }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
and inherit your page from it. Than you can use this new type's name in CodeFileBaseClass
without having to deal with generic parameter issues.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/CodeFileBaseClassAndGenericTypes.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/CodeFileBaseClassAndGenericTypes.aspx" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b0395d7c-2b8b-481e-94ab-93ada4a02ae4" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=03d99e62-88ed-4134-aac4-8238c57e33a0</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,03d99e62-88ed-4134-aac4-8238c57e33a0.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Following my last article on <a href="RegularExpressionValidatorDoNotRepeatYourself.aspx">creating
specialized validators</a>, below I present the EmailValidator control which validates
user input with a pattern of an email:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">public</span>
          <span style="COLOR: rgb(0,0,255)">class</span>
          <span style="COLOR: rgb(43,145,175)">EmailValidator</span> : <span style="COLOR: rgb(43,145,175)">RegularExpressionValidator </span>{ <span style="COLOR: rgb(0,0,255)">public</span> EmailValidator()
{ ValidationExpression = <span style="COLOR: rgb(163,21,21)">@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@
[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\. [a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"</span>; ErrorMessage
= <span style="COLOR: rgb(163,21,21)">"Email is incorrect"</span>; } }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>Note that ValidationExpression is
broken so it may be better displayed in the browser. As for how accurate, the expression
is. It was taken from the <a href="http://regexlib.com/">Regular Expression Library</a> page
and I have had no problems with it. 
<p>
Usage on a page after registration:
</p><pre class="code"><span style="COLOR: rgb(0,0,255)">&lt;</span><span style="COLOR: rgb(163,21,21)">my</span><span style="COLOR: rgb(0,0,255)">:</span><span style="COLOR: rgb(163,21,21)">EmailValidator</span><span style="COLOR: rgb(255,0,0)">runat</span><span style="COLOR: rgb(0,0,255)">="server"</span><span style="COLOR: rgb(255,0,0)">ID</span><span style="COLOR: rgb(0,0,255)">="EmailCorrect"</span><span style="COLOR: rgb(255,0,0)">ControlToValidate</span><span style="COLOR: rgb(0,0,255)">="Email"</span><span style="COLOR: rgb(0,0,255)">/&gt;</span></pre><img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=03d99e62-88ed-4134-aac4-8238c57e33a0" /></body>
      <title>EmailValidator ASP.NET Control</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,03d99e62-88ed-4134-aac4-8238c57e33a0.aspx</guid>
      <link>http://VaultOfThoughts.net/EmailValidatorASPNETControl.aspx</link>
      <pubDate>Wed, 04 Jul 2007 17:03:19 GMT</pubDate>
      <description>&lt;p&gt;
Following my last article on &lt;a href="RegularExpressionValidatorDoNotRepeatYourself.aspx"&gt;creating
specialized validators&lt;/a&gt;, below I present the EmailValidator control which validates
user input with a pattern of an email:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;EmailValidator&lt;/span&gt; : &lt;span style="COLOR: rgb(43,145,175)"&gt;RegularExpressionValidator &lt;/span&gt;{ &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; EmailValidator()
{ ValidationExpression = &lt;span style="COLOR: rgb(163,21,21)"&gt;@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@
[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\. [a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"&lt;/span&gt;; ErrorMessage
= &lt;span style="COLOR: rgb(163,21,21)"&gt;"Email is incorrect"&lt;/span&gt;; } }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Note that ValidationExpression is
broken so it may be better displayed in the browser. As for how accurate, the expression
is. It was taken from the &lt;a href="http://regexlib.com/"&gt;Regular Expression Library&lt;/a&gt; page
and I have had no problems with it. 
&lt;p&gt;
Usage on a page after registration:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;my&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;EmailValidator&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="EmailCorrect"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ControlToValidate&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Email"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=03d99e62-88ed-4134-aac4-8238c57e33a0" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=938ed19a-cb9d-410c-a7f3-4db859ef433b</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,938ed19a-cb9d-410c-a7f3-4db859ef433b.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Validation controls are one of the best features that ASP.NET has to offer. They allow
us to perform both client and server side validation and work very well well with
the Control model that is given to us by the Web Forms model. Here I will describe
how to use RegularExpressionValidator so that never again will you be searching for
that email matching regular expression on all of your pages.
</p>
        <p>
Usually when you use a RegularExpressionValidator you just drop it on a form (or code
it by hand) and set few properties. Among them, there is the ValidationExpression.
When done, it looks more or less like this:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">asp</span>
          <span style="COLOR: rgb(0,0,255)">:</span>
          <span style="COLOR: rgb(163,21,21)">RegularExpressionValidator</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(255,0,0)">ID</span>
          <span style="COLOR: rgb(0,0,255)">="OnlyNormalLetters"</span>
          <span style="COLOR: rgb(255,0,0)">ControlToValidate</span>
          <span style="COLOR: rgb(0,0,255)">="Login"</span>
          <span style="COLOR: rgb(255,0,0)">ErrorMessage</span>
          <span style="COLOR: rgb(0,0,255)">="Login
contains illegal characters"</span>
          <span style="COLOR: rgb(255,0,0)">ValidationExpression</span>
          <span style="COLOR: rgb(0,0,255)">="[a-zA-Z]"</span>
          <span style="COLOR: rgb(0,0,255)">/&gt;</span>
        </pre>
        <a href="http://11011.net/software/vspaste">
          <a href="http://11011.net/software/vspaste">
          </a>
          <p>
As I have described in my article on <a href="UsingInheritedASPNETControlsBestPractice.aspx">the
best practices of inheriting controls</a>, it is a good idea to limit the number of
times important things are repeated in the code. In case of RegularExpressionValidator,
the important thing that gets repeated is of course the ValidationExpression property. 
</p>
          <p>
To avoid repeating the same expression on each page that requires it, it is better
to create a new class that inherits from RegularExpressionValidator. In this class
set the RegularExpression property to some constant string like on the following example:
</p>
          <pre class="code">
            <span style="COLOR: rgb(0,0,255)">public</span>
            <span style="COLOR: rgb(0,0,255)">class</span>
            <span style="COLOR: rgb(43,145,175)">NormalLettersValidator</span> : <span style="COLOR: rgb(43,145,175)">RegularExpressionValidator </span>{ <span style="COLOR: rgb(0,0,255)">public</span> NormalLettersValidator()
{ ValidationExpression = <span style="COLOR: rgb(163,21,21)">"[a-zA-Z]"</span>; }
}</pre>
          <a href="http://11011.net/software/vspaste">
          </a>
          <p>
And that's it. Using this validator is the same as using the built in one, but you
don't need to provide the same ValidationExpression any more. Just register it in
either web.config file or on the page and you are good to go.
</p>
          <p>
Of course the example is very simple, but I hope it demonstrates a useful concept.
</p>
          <p>
            <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/RegularExpressionValidatorDoNotRepeatYourself.aspx">
              <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/RegularExpressionValidatorDoNotRepeatYourself.aspx" border="0" alt="kick it on DotNetKicks.com" />
            </a>
          </p>
          <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=938ed19a-cb9d-410c-a7f3-4db859ef433b" />
        </a>
      </body>
      <title>RegularExpressionValidator - Do Not Repeat Yourself</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,938ed19a-cb9d-410c-a7f3-4db859ef433b.aspx</guid>
      <link>http://VaultOfThoughts.net/RegularExpressionValidatorDoNotRepeatYourself.aspx</link>
      <pubDate>Wed, 04 Jul 2007 16:55:43 GMT</pubDate>
      <description>&lt;p&gt;
Validation controls are one of the best features that ASP.NET has to offer. They allow
us to perform both client and server side validation and work very well well with
the Control model that is given to us by the Web Forms model. Here I will describe
how to use RegularExpressionValidator so that never again will you be searching for
that email matching regular expression on all of your pages.
&lt;/p&gt;
&lt;p&gt;
Usually when you use a RegularExpressionValidator you just drop it on a form (or code
it by hand) and set few properties. Among them, there is the ValidationExpression.
When done, it looks more or less like this:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;asp&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;RegularExpressionValidator&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="OnlyNormalLetters"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ControlToValidate&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Login"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ErrorMessage&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Login
contains illegal characters"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ValidationExpression&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="[a-zA-Z]"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
As I have described in my article on &lt;a href="UsingInheritedASPNETControlsBestPractice.aspx"&gt;the
best practices of inheriting controls&lt;/a&gt;, it is a good idea to limit the number of
times important things are repeated in the code. In case of RegularExpressionValidator,
the important thing that gets repeated is of course the ValidationExpression property. 
&lt;/p&gt;
&lt;p&gt;
To avoid repeating the same expression on each page that requires it, it is better
to create a new class that inherits from RegularExpressionValidator. In this class
set the RegularExpression property to some constant string like on the following example:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;NormalLettersValidator&lt;/span&gt; : &lt;span style="COLOR: rgb(43,145,175)"&gt;RegularExpressionValidator &lt;/span&gt;{ &lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; NormalLettersValidator()
{ ValidationExpression = &lt;span style="COLOR: rgb(163,21,21)"&gt;"[a-zA-Z]"&lt;/span&gt;; }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
And that's it. Using this validator is the same as using the built in one, but you
don't need to provide the same ValidationExpression any more. Just register it in
either web.config file or on the page and you are good to go.
&lt;/p&gt;
&lt;p&gt;
Of course the example is very simple, but I hope&amp;nbsp;it demonstrates a useful concept.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/RegularExpressionValidatorDoNotRepeatYourself.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/RegularExpressionValidatorDoNotRepeatYourself.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=938ed19a-cb9d-410c-a7f3-4db859ef433b" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=bbcf32ac-4f62-44ec-b309-256d863eb9a5</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,bbcf32ac-4f62-44ec-b309-256d863eb9a5.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Almost every time I'm working on an ASP.NET project I create a Visual Studio solution
that consists of at least two projects: the web project and a class library. The first
one is obviously needed. As for the second, what I put there include all kinds of
ASP.NET related classes such as the BasePage class or just custom controls. This makes
it easy to use those controls since Visual Studio (usually) automatically adds them
to the Toolbox.
</p>
        <p>
There are times however, when I need to make something quick. Perhaps a small project
to test a new idea. In those cases, I rarely go for the 2+ projects solution layout
since Visual Studio is standing in my way a bit - i.e. by hiding the possibility of
adding new project to an already existing Web Site project (solution node is
not visible on the tree view in Solution Explorer, but it is still possible to add
the project using File menu). So I end up with only one project and the App_Code folder.
</p>
        <p>
Working with App_Code is not a problem as long as you don't need to make some configuration
in the Web.config file. Some elements require you to specify a type name. Normally
in such a case, you need to provide a valid type format name in a form of &lt;typename&gt;,&lt;assemblyname&gt;.
As long as you know the assembly name it's OK, but wait! There is no explicit
assembly for App_Code. There is however an assembly behind the App_Code directory.
It is simply called App_Code!
</p>
        <p>
That said, it is usually enough to just specify the type name, without assembly name,
for types defined in the App_Code directory.
</p>
        <p>
So what about controls? In order to work with a control defined in App_Code directory
we need to either register it on a page or in a Web.config file. There is however
one issue. In order to register a control in a Web.config we need a namespace. By
default however, neither pages nor classes added to an App_Code folder have a namespace.
This is only a minor inconvenience because ASP.NET does not prevent us from adding
a missing namespace declaration to a class in an App_Code directory. By adding such
a namespace to a class representing ASP.NET Control, we can then add a registration
in a Web.config file like the following one:
</p>
        <a href="http://11011.net/software/vspaste">
        </a>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">pages</span>
          <span style="COLOR: rgb(0,0,255)">&gt;
&lt;</span>
          <span style="COLOR: rgb(163,21,21)">controls</span>
          <span style="COLOR: rgb(0,0,255)">&gt;
&lt;</span>
          <span style="COLOR: rgb(163,21,21)">add</span>
          <span style="COLOR: rgb(0,0,255)">
          </span>
          <span style="COLOR: rgb(255,0,0)">namespace</span>
          <span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">MyControls</span>"<span style="COLOR: rgb(0,0,255)"></span><span style="COLOR: rgb(255,0,0)">tagPrefix</span><span style="COLOR: rgb(0,0,255)">=</span>"<span style="COLOR: rgb(0,0,255)">m</span>"<span style="COLOR: rgb(0,0,255)">/&gt;
&lt;/</span><span style="COLOR: rgb(163,21,21)">controls</span><span style="COLOR: rgb(0,0,255)">&gt;
&lt;/</span><span style="COLOR: rgb(163,21,21)">pages</span><span style="COLOR: rgb(0,0,255)">&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Notice that assembly attribute is not required.
</p>
        <p>
For working with code, there is also a special class that helps with issues arising
from the fact that there is no explicitly assembly nor a namespace for types in an
App_Code directory. If at any time, there is a need to work with a type object of a
class from the mentioned directory, .NET framework offers us a class called BuildManager
which defines a GetType. Using this method it is possible to get an instance of a
type object by providing just a name of a class defined in the App_Code directory.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/WorkingWithWebconfigAndAppCodeDirectory.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/WorkingWithWebconfigAndAppCodeDirectory.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=bbcf32ac-4f62-44ec-b309-256d863eb9a5" />
      </body>
      <title>Working With Web.config And App_Code Directory</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,bbcf32ac-4f62-44ec-b309-256d863eb9a5.aspx</guid>
      <link>http://VaultOfThoughts.net/WorkingWithWebconfigAndAppCodeDirectory.aspx</link>
      <pubDate>Mon, 25 Jun 2007 17:17:51 GMT</pubDate>
      <description>&lt;p&gt;
Almost every time I'm working on an ASP.NET project I create a Visual Studio solution
that consists of at least two projects: the web project and a class library. The first
one is obviously needed. As for the second, what I put there include all kinds of
ASP.NET related classes such as the BasePage class or just custom controls. This makes
it easy to use those controls since Visual Studio (usually) automatically adds them
to the Toolbox.
&lt;/p&gt;
&lt;p&gt;
There are times however, when I need to make something quick. Perhaps a small project
to test a new idea. In those cases, I rarely go for the 2+ projects solution layout
since Visual Studio is standing in my way a bit - i.e. by hiding the possibility of
adding new project to an already&amp;nbsp;existing Web Site project (solution node is
not visible on the tree view in Solution Explorer, but it is still possible to add
the project using File menu). So I end up with only one project and the App_Code folder.
&lt;/p&gt;
&lt;p&gt;
Working with App_Code is not a problem as long as you don't need to make some configuration
in the Web.config file. Some elements require you to specify a type name. Normally
in such a case, you need to provide a valid type format name in a form of &amp;lt;typename&amp;gt;,&amp;lt;assemblyname&amp;gt;.
As long as you know the assembly name it's OK,&amp;nbsp;but wait! There is no explicit
assembly for App_Code. There is however an assembly behind the App_Code directory.
It is simply called App_Code!
&lt;/p&gt;
&lt;p&gt;
That said, it is usually enough to just specify the type name, without assembly name,
for types defined in the App_Code directory.
&lt;/p&gt;
&lt;p&gt;
So what about controls? In order to work with a control defined in App_Code directory
we need to either register it on a page or in a Web.config file. There is however
one issue. In order to register a control in a Web.config we need a namespace. By
default however, neither pages nor classes added to an App_Code folder have a namespace.
This is only a minor inconvenience because ASP.NET does not prevent us from adding
a missing namespace declaration to a class in an App_Code directory. By adding such
a namespace to a class representing ASP.NET Control, we can then add a registration
in a Web.config file like the following one:
&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;pages&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;controls&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;add&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;namespace&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;MyControls&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="COLOR: rgb(255,0,0)"&gt;tagPrefix&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;=&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;m&lt;/span&gt;"&lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;controls&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;pages&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Notice that assembly attribute is not required.
&lt;/p&gt;
&lt;p&gt;
For working with code, there is also a special class that helps with issues arising
from the fact that there is no explicitly assembly nor a namespace for types in an
App_Code directory. If at any time, there is a need to work with a type object of&amp;nbsp;a
class from the mentioned directory, .NET framework offers us a class called BuildManager
which defines a GetType. Using this method it is possible to get an instance of a
type object by providing just a name of a class defined in the App_Code directory.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/WorkingWithWebconfigAndAppCodeDirectory.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/WorkingWithWebconfigAndAppCodeDirectory.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=bbcf32ac-4f62-44ec-b309-256d863eb9a5" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=80f8376a-8dfa-409e-b689-6970b97e1c5e</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,80f8376a-8dfa-409e-b689-6970b97e1c5e.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
After reading my article on <a href="RecursivePropertiesOfASPNETControls.aspx">recursive
properties of ASP.NET controls</a>, where I point out that not all properties are
recursive, I have decided to fix one annoying behavior. The problem is that one of
the most important property of all, the EnableViewState property is not recursive.
This of course makes it useless in most scenarios. Never will we be able to rely on
it while authoring our controls since ViewState can be disabled on any of the parent
controls thus preventing our control from saving its state.
</p>
        <p>
To solve this a very simple method can be used to check for the state of EnableViewState
property:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">public</span>
          <span style="COLOR: rgb(0,0,255)">bool</span> IsViewStateEnabled(<span style="COLOR: rgb(43,145,175)">Control</span> control)
{ <span style="COLOR: rgb(0,0,255)">if</span> (control.Parent == <span style="COLOR: rgb(0,0,255)">null</span>)
{ <span style="COLOR: rgb(0,0,255)">return</span> control.EnableViewState; } <span style="COLOR: rgb(0,0,255)">return</span> control.EnableViewState
&amp;&amp; IsViewStateEnabled(control.Parent); }</pre>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=80f8376a-8dfa-409e-b689-6970b97e1c5e" />
      </body>
      <title>Recursive EnableViewState</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,80f8376a-8dfa-409e-b689-6970b97e1c5e.aspx</guid>
      <link>http://VaultOfThoughts.net/RecursiveEnableViewState.aspx</link>
      <pubDate>Wed, 20 Jun 2007 18:44:05 GMT</pubDate>
      <description>&lt;p&gt;
After reading my article on &lt;a href="RecursivePropertiesOfASPNETControls.aspx"&gt;recursive
properties of ASP.NET controls&lt;/a&gt;, where I point out that not all properties are
recursive, I have decided to fix one annoying behavior. The problem is that one of
the most important property of all, the EnableViewState property is not recursive.
This of course makes it useless in most scenarios. Never will we be able to rely on
it while authoring our controls since ViewState can be disabled on any of the parent
controls thus preventing our control from saving its state.
&lt;/p&gt;
&lt;p&gt;
To solve this a very simple method can be used to check for the state of EnableViewState
property:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;bool&lt;/span&gt; IsViewStateEnabled(&lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt; control)
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;if&lt;/span&gt; (control.Parent == &lt;span style="COLOR: rgb(0,0,255)"&gt;null&lt;/span&gt;)
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; control.EnableViewState; } &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; control.EnableViewState
&amp;amp;&amp;amp; IsViewStateEnabled(control.Parent); }&lt;/pre&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=80f8376a-8dfa-409e-b689-6970b97e1c5e" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=91eebcec-d080-469b-83c5-275cd4ef91db</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,91eebcec-d080-469b-83c5-275cd4ef91db.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
ASP.NET, or more accurately Web Forms, offer us a completely new way to create web
pages. We can now work with control just like Windows developers. We even get to use
an event driven model. How make best use of this? Below I present a useful pattern
that I'm almost always following while working with ASP.NET. 
</p>
        <p>
Suppose that you are building a web application with lots of pages and forms for data
input. On those forms you typically have input fields in form of <em>TextBoxes</em>, <em>DropDownLists</em> etc.
Usually there are also some buttons that allow for invoking actions. Those
actions include things like "Add", "Update" or "Cancel". 
</p>
        <p>
The standard way to create a form for inputing data would be to use mentioned before standard
ASP.NET controls. That is fine. It gives us access to all that ASP.NET Web Forms
can offer in terms of Page structure and event model. There is however one issue. In
most cases there we will be several pages with the same buttons on them. Take for
example the "Cancel", "Add" or "Update" buttons. Almost every time there is a
data to be edited, there will be those buttons. If we use the "out of the box" approach,
we end up with code similar to the following, on lots of pages: 
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">asp</span>
          <span style="COLOR: rgb(0,0,255)">:</span>
          <span style="COLOR: rgb(163,21,21)">Button</span>
          <span style="COLOR: rgb(255,0,0)">ID</span>
          <span style="COLOR: rgb(0,0,255)">="Add"</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(255,0,0)">Text</span>
          <span style="COLOR: rgb(0,0,255)">="Add"</span>
          <span style="COLOR: rgb(0,0,255)">/&gt;</span>
        </pre>
        <a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;url=http%3a%2f%2f11011.net%2fsoftware%2fvspaste">
        </a>
        <p>
At first glance, there is not much wrong with it. If you look closer however, we can
see the potential problems. One of the is certainly the <em>Text</em> property. 
</p>
        <p>
Why is it a problem? Suppose that now we need to localize the application? In
this case, we will have to make a change in many places. Simple technique would
be to decorate the <em>Button</em> control with attributes used by the Localization
feature of ASP.NET like <em>meta:resourcekey</em>. But still we end up with one major
problem: every time a change is required (that cannot be dealt with using Localization,
Skins or other built in feature), we have to make it in many places. As we know: change
in many places is a BAD PRACTICE. 
</p>
        <p>
What if we find out, that our add buttons need to have some special <em>CssClass</em> attribute?
What if all our "Cancel" Buttons should have <em>CausesValidation</em> property set
to <em>false</em>? Once again: a change is required in many places - which is ...
BAD PRACTICE! 
</p>
        <p>
For localization, we can add an implicit localization attributes as mentioned above.
What about <em>CausesValidation</em> or <em>CssClass</em> properties? Some of them
we can try to set using Skin files, but as you will sooner or later discover, theming
support in <a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;url=http%3a%2f%2faspnetresources.com%2fblog%2fno_resource_expressions_in_skins.aspx">ASP.NET
is VERY limited, especially when it comes to localization</a>. You cannot localize
skin files, some of the properties are not themeable etc. There are many examples
of potential problems but what is a solution? It seams that there has to be a
better way. 
</p>
        <p>
In my practice I have found that it is usually good not to use the standard controls
that come with ASP.NET but rather make new ones that directly inherit from them. Doing
so we inherit all the functionality without loosing anything (almost). As
an example, take the aforementioned "Cancel" button. If we create a new control like
this: 
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">public</span>
          <span style="COLOR: rgb(0,0,255)">class</span>
          <span style="COLOR: rgb(43,145,175)">CancelButton</span> : <span style="COLOR: rgb(43,145,175)">Button </span>{ <span style="COLOR: rgb(0,0,255)">protected</span><span style="COLOR: rgb(0,0,255)">override</span><span style="COLOR: rgb(0,0,255)">void</span> OnInit(<span style="COLOR: rgb(43,145,175)">EventArgs</span> e)
{ Text = <span style="COLOR: rgb(163,21,21)">"Cancel"</span>; <span style="COLOR: rgb(0,128,0)">//
get value from resource </span> CausesValidation = <span style="COLOR: rgb(0,0,255)">false</span>; <span style="COLOR: rgb(0,0,255)">base</span>.OnInit(e);
} }</pre>
        <a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;url=http%3a%2f%2f11011.net%2fsoftware%2fvspaste">
        </a>
        <p>
And on pages, instead of the standard button, use this new control: 
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">my</span>
          <span style="COLOR: rgb(0,0,255)">:</span>
          <span style="COLOR: rgb(163,21,21)">CancelButton</span>
          <span style="COLOR: rgb(255,0,0)">ID</span>
          <span style="COLOR: rgb(0,0,255)">="Add"</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(0,0,255)">/&gt;</span>
        </pre>
        <a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;url=http%3a%2f%2f11011.net%2fsoftware%2fvspaste">
        </a>
        <p>
In doing we allow ourselves to make changes in one place that will affect the whole
application. In this way, without much work, we have gained a lot of flexibility for
the future and saved a lot of potential work. If some time in the future, we need
to localize the Text property for all Cancel buttons, we can do it in just one place
which is a GOOD PRACTICE. 
</p>
        <p>
Benefits do not end there! If some day, there is a requirement, that all
buttons should now be <em>LinkButtons</em> instead of normal <em>Buttons</em> we can
easily do it bu just changing the base class from which our <em>CancelButton</em> inherits
and the same technique can be used with other controls too (as long as their public
interfaces match at least in naming). 
</p>
        <p>
Now the question is if you should always use the inherited controls. As usual, there
is no simple answer to that question. There is a cost of having a lots of additional
classes in the project if we try to follow this rule. Imagine that just for standard
buttons, there will be quite a few: Add, Cancel, Remove, Back, Change, Edit just to
mention some of them. For small, fast'n'dirty projects it will of course be a huge
overkill (or will it?), but if you know that the project is not a small task, it is,
in my opinion, a thing that should be seriously considered. 
</p>
        <p>
          <a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;url=http%3a%2f%2fwww.dotnetkicks.com%2fkick%2f%3furl%3dhttp%3a%2f%2fvaultofthoughts.net%2fUsingInheritedASPNETControlsBestPractice.aspx">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/UsingInheritedASPNETControlsBestPractice.aspx" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=91eebcec-d080-469b-83c5-275cd4ef91db" />
      </body>
      <title>Using Inherited ASP.NET Controls - Best Practice</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,91eebcec-d080-469b-83c5-275cd4ef91db.aspx</guid>
      <link>http://VaultOfThoughts.net/UsingInheritedASPNETControlsBestPractice.aspx</link>
      <pubDate>Tue, 19 Jun 2007 19:32:37 GMT</pubDate>
      <description>&lt;p&gt;
ASP.NET, or more accurately Web Forms, offer us a completely new way to create web
pages. We can now work with control just like Windows developers. We even get to use
an event driven model. How make best use of this? Below I present a useful pattern
that I'm almost always following while working with ASP.NET. 
&lt;/p&gt;
&lt;p&gt;
Suppose that you are building a web application with lots of pages and forms for data
input. On those forms you typically&amp;nbsp;have input fields in form of &lt;em&gt;TextBoxes&lt;/em&gt;, &lt;em&gt;DropDownLists&lt;/em&gt; etc.
Usually there are&amp;nbsp;also some buttons that allow&amp;nbsp;for invoking actions. Those
actions include&amp;nbsp;things like "Add",&amp;nbsp;"Update" or&amp;nbsp;"Cancel". 
&lt;/p&gt;
&lt;p&gt;
The standard way to create a form for inputing data would be to use mentioned before&amp;nbsp;standard
ASP.NET controls. That is fine. It gives us access to all that ASP.NET&amp;nbsp;Web Forms
can offer in terms of&amp;nbsp;Page structure and event model. There is however one issue.&amp;nbsp;In
most cases there we will be several pages with the same buttons on them. Take for
example the "Cancel", "Add" or "Update"&amp;nbsp;buttons. Almost every time there is a
data to be edited, there will be those buttons. If we use the "out of the box" approach,
we end up with code similar to the following, on lots of pages: 
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;asp&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Button&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Add"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;Text&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Add"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;amp;url=http%3a%2f%2f11011.net%2fsoftware%2fvspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
At first glance, there is not much wrong with it. If you look closer however, we can
see the potential problems. One of the is certainly the &lt;em&gt;Text&lt;/em&gt; property. 
&lt;/p&gt;
&lt;p&gt;
Why is it a problem? Suppose that now&amp;nbsp;we need to localize the application? In
this case, we will have to make a change in&amp;nbsp;many places. Simple technique would
be to decorate the &lt;em&gt;Button&lt;/em&gt; control with attributes used by the Localization
feature of ASP.NET like &lt;em&gt;meta:resourcekey&lt;/em&gt;. But still we end up with one major
problem: every time a change is required (that cannot be dealt with using Localization,
Skins or other built in feature), we have to make it in many places. As we know: change
in many places is a BAD PRACTICE. 
&lt;/p&gt;
&lt;p&gt;
What if we find out, that our add buttons need to have some special &lt;em&gt;CssClass&lt;/em&gt; attribute?
What if all our "Cancel" Buttons should have &lt;em&gt;CausesValidation&lt;/em&gt; property set
to &lt;em&gt;false&lt;/em&gt;? Once again: a change is required in many places - which is ...
BAD PRACTICE! 
&lt;/p&gt;
&lt;p&gt;
For localization, we can add an implicit localization attributes as mentioned above.
What about &lt;em&gt;CausesValidation&lt;/em&gt; or &lt;em&gt;CssClass&lt;/em&gt; properties? Some of them
we can try to set using Skin files, but as you will sooner or later discover, theming
support in &lt;a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;amp;url=http%3a%2f%2faspnetresources.com%2fblog%2fno_resource_expressions_in_skins.aspx"&gt;ASP.NET
is VERY limited, especially when it comes to localization&lt;/a&gt;. You cannot localize
skin files, some of the properties are not themeable etc. There are many examples
of potential problems but&amp;nbsp;what is a solution? It seams that there has to be a
better way. 
&lt;/p&gt;
&lt;p&gt;
In my practice I have found that it is usually good not to use&amp;nbsp;the standard controls
that come with ASP.NET but rather make new ones that directly inherit from them. Doing
so&amp;nbsp;we inherit all the functionality without loosing anything (almost).&amp;nbsp;As
an example, take the aforementioned "Cancel" button. If we create a new control like
this: 
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;CancelButton&lt;/span&gt; : &lt;span style="COLOR: rgb(43,145,175)"&gt;Button &lt;/span&gt;{ &lt;span style="COLOR: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; OnInit(&lt;span style="COLOR: rgb(43,145,175)"&gt;EventArgs&lt;/span&gt; e)
{ Text = &lt;span style="COLOR: rgb(163,21,21)"&gt;"Cancel"&lt;/span&gt;; &lt;span style="COLOR: rgb(0,128,0)"&gt;//
get value from resource &lt;/span&gt; CausesValidation = &lt;span style="COLOR: rgb(0,0,255)"&gt;false&lt;/span&gt;; &lt;span style="COLOR: rgb(0,0,255)"&gt;base&lt;/span&gt;.OnInit(e);
} }&lt;/pre&gt;
&lt;a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;amp;url=http%3a%2f%2f11011.net%2fsoftware%2fvspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
And on pages, instead of the standard button, use this new control: 
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;my&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;CancelButton&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Add"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;amp;url=http%3a%2f%2f11011.net%2fsoftware%2fvspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
In doing we allow ourselves to make changes in one place that will affect the whole
application. In this way, without much work, we have gained a lot of flexibility for
the future and saved a lot of potential work. If some time in the future, we need
to localize the Text property for all Cancel buttons, we can do it in just one place
which is a GOOD PRACTICE. 
&lt;/p&gt;
&lt;p&gt;
Benefits do not end there!&amp;nbsp;If some day, there&amp;nbsp;is a requirement, that all
buttons should now be &lt;em&gt;LinkButtons&lt;/em&gt; instead of normal &lt;em&gt;Buttons&lt;/em&gt; we can
easily do&amp;nbsp;it bu just changing the base&amp;nbsp;class from which our &lt;em&gt;CancelButton&lt;/em&gt; inherits
and the same technique can be used with other controls too (as long as their public
interfaces match at least in naming). 
&lt;/p&gt;
&lt;p&gt;
Now the question is if you should always use the inherited controls. As usual, there
is no simple answer to that question. There is a cost of having a lots of additional
classes in the project if we try to follow this rule. Imagine that just for standard
buttons, there will be quite a few: Add, Cancel, Remove, Back, Change, Edit just to
mention some of them. For small, fast'n'dirty projects it will of course be a huge
overkill (or will it?), but if you know that the project is not a small task, it is,
in my opinion, a thing that should be seriously considered. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://vaultofthoughts.net/ct.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906&amp;amp;url=http%3a%2f%2fwww.dotnetkicks.com%2fkick%2f%3furl%3dhttp%3a%2f%2fvaultofthoughts.net%2fUsingInheritedASPNETControlsBestPractice.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/UsingInheritedASPNETControlsBestPractice.aspx" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=91eebcec-d080-469b-83c5-275cd4ef91db" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=ec0e6b69-52c1-48fa-8bcf-709b83793906</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,ec0e6b69-52c1-48fa-8bcf-709b83793906.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm a big ASP.NET lover mostly because of the fact that I can work with HTML in a
more abstract way presented to me in form of controls. While authoring controls, it
is always my goal to write as little HTML code as possible. To make it possible I
relay heavily on composite controls. Sometimes however it is not possible or simply
to complex to achieve a result using a composition of controls. That's where the <em>Render</em> method
comes in handy.
</p>
        <p>
Using <em>Render</em> method and accompanying <em>HtmlTextWriter</em> object we can
write plain HTML using <em>writer.Write()</em> method. This however forces us to write
HTML which I try to avoid. Here also the  <em>HtmlTextWriter</em> offers some
alternatives that include <em>writer.RenderBeginTag()</em> method and <em>HtmlTextWriterTag</em> enumeration.
Using them, we can generate HTML in a way that allows for some degree of compile time
checking and some enhancements. If we just used the following code:
</p>
        <pre class="code">writer.RenderBeginTag(<span style="COLOR: rgb(163,21,21)">"h1"</span>);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
there is little that protects us from misspelling the tag name. There is also
no way for a <em>RenderBeginTag</em> method to help us adjust the output to target
concrete platform. We are simply telling it to write "h1" element and so it does because
we expect it to. Consider however an alternative version:
</p>
        <pre class="code">writer.RenderBeginTag(<span style="COLOR: rgb(43,145,175)">HtmlTextWriterTag</span>.H1);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
This time, instead of passing "h1" string literal, we pass a value of a <em>HtmlTextWriterTag</em> enumeration.
Now we are suggesting that we need the H1 element, but we leave a space for interpretation.
What the writer object renders is up to it. Keep in mind however that <em>HtmlTextWriter</em> class
and moreover the <em>XhtmlTextWriter</em> class are there to allow for customization.
If there is a device that requires all tags to be uppercase, than it can be done using
a special writer object that knows and targets the concrete device. 
</p>
        <p>
One thing that I only recently discovered however is how to write a "&lt;br /&gt;"
without writing "&lt;br /&gt;". If we try to use the mentioned overload like this:
</p>
        <pre class="code">writer.RenderBeginTag(<span style="COLOR: rgb(43,145,175)">HtmlTextWriterTag</span>.Br);
writer.RenderEndTag();</pre>
        <p>
We will get an invalid HTML:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">br</span>
          <span style="COLOR: rgb(0,0,255)">&gt;
&lt;/</span>
          <span style="COLOR: rgb(163,21,21)">br</span>
          <span style="COLOR: rgb(0,0,255)">&gt; </span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
The solution is to use the <em>writer.WriteBreak()</em> method. It will output the
required form of "&lt;br /&gt;" and shield us from using plain text.
</p>
        <p>
You may ask if it really matters. True, it is a very thin line of abstraction here
but I like it this way.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/HowToWriteBrWithoutWritingBr.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/HowToWriteBrWithoutWritingBr.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906" />
      </body>
      <title>How To Write br Without Writing br</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,ec0e6b69-52c1-48fa-8bcf-709b83793906.aspx</guid>
      <link>http://VaultOfThoughts.net/HowToWriteBrWithoutWritingBr.aspx</link>
      <pubDate>Tue, 19 Jun 2007 18:22:24 GMT</pubDate>
      <description>&lt;p&gt;
I'm a big ASP.NET lover mostly because of the fact that I can work with HTML in a
more abstract way presented to me in form of controls. While authoring controls, it
is always my goal to write as little HTML code as possible. To make it possible I
relay heavily on composite controls. Sometimes however it is not possible or simply
to complex to achieve a result using a composition of controls. That's where the &lt;em&gt;Render&lt;/em&gt; method
comes in handy.
&lt;/p&gt;
&lt;p&gt;
Using &lt;em&gt;Render&lt;/em&gt; method and accompanying &lt;em&gt;HtmlTextWriter&lt;/em&gt; object we can
write plain HTML using &lt;em&gt;writer.Write()&lt;/em&gt; method. This however forces us to write
HTML which I try to avoid. Here also the&amp;nbsp; &lt;em&gt;HtmlTextWriter&lt;/em&gt; offers some
alternatives that include &lt;em&gt;writer.RenderBeginTag()&lt;/em&gt; method and &lt;em&gt;HtmlTextWriterTag&lt;/em&gt; enumeration.
Using them, we can generate HTML in a way that allows for some degree of compile time
checking&amp;nbsp;and&amp;nbsp;some enhancements. If we just used&amp;nbsp;the following code:
&lt;/p&gt;
&lt;pre class=code&gt;writer.RenderBeginTag(&lt;span style="COLOR: rgb(163,21,21)"&gt;"h1"&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
there is little that protects us from&amp;nbsp;misspelling the tag name. There is&amp;nbsp;also
no way for a &lt;em&gt;RenderBeginTag&lt;/em&gt; method to help us adjust the output to target
concrete platform. We are simply telling it to write "h1" element and so it does because
we expect it to. Consider however an alternative version:
&lt;/p&gt;
&lt;pre class=code&gt;writer.RenderBeginTag(&lt;span style="COLOR: rgb(43,145,175)"&gt;HtmlTextWriterTag&lt;/span&gt;.H1);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
This time, instead of passing "h1" string literal, we pass a value of a &lt;em&gt;HtmlTextWriterTag&lt;/em&gt; enumeration.
Now we are suggesting that we need the H1 element, but we leave a space for interpretation.
What the writer object renders is up to it. Keep in mind however that &lt;em&gt;HtmlTextWriter&lt;/em&gt; class
and moreover the &lt;em&gt;XhtmlTextWriter&lt;/em&gt; class are there to allow for customization.
If there is a device that requires all tags to be uppercase, than it can be done using
a special writer object that knows and targets the concrete device. 
&lt;/p&gt;
&lt;p&gt;
One thing that I only recently discovered however is how to write a "&amp;lt;br /&amp;gt;"
without writing "&amp;lt;br /&amp;gt;". If we try to use the mentioned overload like this:
&lt;/p&gt;
&lt;pre class=code&gt;writer.RenderBeginTag(&lt;span style="COLOR: rgb(43,145,175)"&gt;HtmlTextWriterTag&lt;/span&gt;.Br);
writer.RenderEndTag();&lt;/pre&gt;
&lt;p&gt;
We will get an invalid HTML:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;br&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;br&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt; &lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The solution is to use the &lt;em&gt;writer.WriteBreak()&lt;/em&gt; method. It will output the
required form of "&amp;lt;br /&amp;gt;" and shield us from using plain text.
&lt;/p&gt;
&lt;p&gt;
You may ask if it really matters. True, it is a very thin line of abstraction here
but I like it this way.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/HowToWriteBrWithoutWritingBr.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/HowToWriteBrWithoutWritingBr.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=ec0e6b69-52c1-48fa-8bcf-709b83793906" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=392171d3-8ab8-480f-9303-e1fd92666013</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,392171d3-8ab8-480f-9303-e1fd92666013.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
While working with ASP.NET, from time to time, I need to change some parameter that
is common to all controls of on a page. An example of such operation might be
setting <em>ReadOnly</em> property for all <em>TextBox</em> controls etc.
</p>
        <p>
Below is a sample method code I use to get a flattened hierarchy of controls on a
page:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">public</span>
          <span style="COLOR: rgb(0,0,255)">static</span>
          <span style="COLOR: rgb(43,145,175)">Control</span>[]
FlattenHierachy(<span style="COLOR: rgb(43,145,175)">Control</span> root) { <span style="COLOR: rgb(43,145,175)">List</span>&lt;<span style="COLOR: rgb(43,145,175)">Control</span>&gt;
list = <span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">List</span>&lt;<span style="COLOR: rgb(43,145,175)">Control</span>&gt;();
list.Add(root); <span style="COLOR: rgb(0,0,255)">if</span> (root.HasControls()) { <span style="COLOR: rgb(0,0,255)">foreach</span> (<span style="COLOR: rgb(43,145,175)">Control</span> control <span style="COLOR: rgb(0,0,255)">in</span> root.Controls)
{ list.AddRange(FlattenHierachy(control)); } } <span style="COLOR: rgb(0,0,255)">return</span> list.ToArray();
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
As you can see, it is pretty simple. The method recursively traverses control hierarchy
and collects each control it finds on its way to a collection which is finally returned
to the caller in a form of an array.
</p>
        <p>
As mentioned above we can use this method to set the <em>ReadOnly</em> property of
all <em>TextBox-</em>es on the page. To do this, a code similar to the one below can
be used:
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">private</span>
          <span style="COLOR: rgb(0,0,255)">void</span> MakeTextBoxesReadOnly()
{ <span style="COLOR: rgb(43,145,175)">Control</span>[] allControls = FlattenHierachy(Page); <span style="COLOR: rgb(0,0,255)">foreach</span> (<span style="COLOR: rgb(43,145,175)">Control</span> control <span style="COLOR: rgb(0,0,255)">in</span> allControls)
{ <span style="COLOR: rgb(43,145,175)">TextBox</span> textBox = control <span style="COLOR: rgb(0,0,255)">as</span><span style="COLOR: rgb(43,145,175)">TextBox</span>; <span style="COLOR: rgb(0,0,255)">if</span> (textBox
!= <span style="COLOR: rgb(0,0,255)">null</span>) { textBox.ReadOnly = <span style="COLOR: rgb(0,0,255)">true</span>;
} } } </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Using a combination of the above outlined methods it is easy to implement a kind of
ReadOnlyMode page filter. Just identify what controls need to be <em>ReadOnly</em> and
how to make them so (<em>RadioButton</em> for example doesn't have a <em>ReadOnly</em> property).
When you know how and what you want to make <em>ReadOnly</em>, just override Page's <em>OnPreRender</em> method
by adding a call a <em>MakeTextBoxesReadOnly</em> method (or whatever method you create).
</p>
        <p>
Of course all of this is possible thanks to the great ASP.NET control model which
is one of the things that I really like about ASP.NET.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/GettingAllControlsOnTheASPNETPage.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/GettingAllControlsOnTheASPNETPage.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=392171d3-8ab8-480f-9303-e1fd92666013" />
      </body>
      <title>Getting All Controls On The ASP.NET Page</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,392171d3-8ab8-480f-9303-e1fd92666013.aspx</guid>
      <link>http://VaultOfThoughts.net/GettingAllControlsOnTheASPNETPage.aspx</link>
      <pubDate>Mon, 18 Jun 2007 10:07:38 GMT</pubDate>
      <description>&lt;p&gt;
While working with ASP.NET, from time to time, I need to change some parameter that
is common to all controls of&amp;nbsp;on a page. An example of such operation might be
setting &lt;em&gt;ReadOnly&lt;/em&gt; property for all &lt;em&gt;TextBox&lt;/em&gt; controls etc.
&lt;/p&gt;
&lt;p&gt;
Below is a sample method code I use to get a flattened hierarchy of controls on a
page:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt;[]
FlattenHierachy(&lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt; root) { &lt;span style="COLOR: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt;&amp;gt;
list = &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt;&amp;gt;();
list.Add(root); &lt;span style="COLOR: rgb(0,0,255)"&gt;if&lt;/span&gt; (root.HasControls()) { &lt;span style="COLOR: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt; control &lt;span style="COLOR: rgb(0,0,255)"&gt;in&lt;/span&gt; root.Controls)
{ list.AddRange(FlattenHierachy(control)); } } &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; list.ToArray();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
As you can see, it is pretty simple. The method recursively traverses control hierarchy
and collects each control it finds on its way to a collection which is finally returned
to the caller in a form of an array.
&lt;/p&gt;
&lt;p&gt;
As mentioned above we can use this method to set the &lt;em&gt;ReadOnly&lt;/em&gt; property of
all &lt;em&gt;TextBox-&lt;/em&gt;es on the page. To do this, a code similar to the one below can
be used:
&lt;/p&gt;
&lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; MakeTextBoxesReadOnly()
{ &lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt;[] allControls = FlattenHierachy(Page); &lt;span style="COLOR: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt; control &lt;span style="COLOR: rgb(0,0,255)"&gt;in&lt;/span&gt; allControls)
{ &lt;span style="COLOR: rgb(43,145,175)"&gt;TextBox&lt;/span&gt; textBox = control &lt;span style="COLOR: rgb(0,0,255)"&gt;as&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;TextBox&lt;/span&gt;; &lt;span style="COLOR: rgb(0,0,255)"&gt;if&lt;/span&gt; (textBox
!= &lt;span style="COLOR: rgb(0,0,255)"&gt;null&lt;/span&gt;) { textBox.ReadOnly = &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;;
} } } &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Using a combination of the above outlined methods it is easy to implement a kind of
ReadOnlyMode page filter. Just identify what controls need to be &lt;em&gt;ReadOnly&lt;/em&gt; and
how to make them so (&lt;em&gt;RadioButton&lt;/em&gt; for example doesn't have a &lt;em&gt;ReadOnly&lt;/em&gt; property).
When you know how and what you want to make &lt;em&gt;ReadOnly&lt;/em&gt;, just override Page's &lt;em&gt;OnPreRender&lt;/em&gt; method
by adding a call a &lt;em&gt;MakeTextBoxesReadOnly&lt;/em&gt; method (or whatever method you create).
&lt;/p&gt;
&lt;p&gt;
Of course all of this is possible thanks to the great ASP.NET control model which
is one of the things that I really like about ASP.NET.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/GettingAllControlsOnTheASPNETPage.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/GettingAllControlsOnTheASPNETPage.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=392171d3-8ab8-480f-9303-e1fd92666013" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=f0c6b39a-a84b-44d4-ac74-5b272c398992</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,f0c6b39a-a84b-44d4-ac74-5b272c398992.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I've got a question if it is possible to make ASP.NET not to render the <em>div</em> tag
around it's special hidden fields (i.e.: "__EVENTTARGET"). I have never thought
about it before, so I didn't know the answer. I had to investigate. After a brief
overview of the state of things, I have come to believe that there is no easy way
to do achieve this. To confirm my suspicions I used the Reflector. 
</p>
        <p>
I have found that it is indeed possible to make ASP.NET not render the div tag if <em>RenderDivAroundHiddenInputs</em> property
of <em>HtmlTextWriter</em> class returns false or if <em>LegacyRendering</em> is enabled.
You can enable the later by setting <em>xhtmlConformance mode</em> to <em>Legacy</em> in <em>Web.config</em> file.
Unfortunately the <em>RenderDivAroundHiddenInputs</em> property of <em>HtmlTextWriter</em> class
always returns true and you cannot change it because it is internal. Additional investigation
has shown that there is a class that overrides that property to return false. The
class was: <em>XhtmlTextWriter</em>. 
</p>
        <p>
How to make ASP.NET use <em>XhtmlTextWriter</em> instead of normal <em>HtmlTextWriter</em>?
I was unable to find a way to configure ASP.NET to do it. Setting <em>xhtmlConformance</em><em>mode</em> to <em>strict</em> doesn't
help. Google also is not very helpful. The only way seams to be either override <em>Render</em> method
of a <em>Page</em> class like this: 
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">protected</span>
          <span style="COLOR: rgb(0,0,255)">override</span>
          <span style="COLOR: rgb(0,0,255)">void</span> Render(<span style="COLOR: rgb(43,145,175)">HtmlTextWriter</span> writer)
{ <span style="COLOR: rgb(0,0,255)">base</span>.Render(<span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">XhtmlTextWriter</span>(writer));
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Or better yet to override the <em>CreateHtmlTextWriter</em> like this: 
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">protected</span>
          <span style="COLOR: rgb(0,0,255)">override</span>
          <span style="COLOR: rgb(43,145,175)">HtmlTextWriter</span> CreateHtmlTextWriter(System.IO.<span style="COLOR: rgb(43,145,175)">TextWriter</span> tw)
{ <span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">XhtmlTextWriter</span>(<span style="COLOR: rgb(0,0,255)">base</span>.CreateHtmlTextWriter(tw));
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
This however requires you to use a kind of BasePage class for each page in your project
(which is good, but not everyone likes it). Another concern I have is the fact that
there is very little information on <em>XhtmlTextWriter</em> class, and the one that
is available seams to suggest that the class is meant rather for mobile devices. 
</p>
        <p>
There is one more way you can try to solve the problem. You can use <em>Response.Filter</em> to
do the job, but I would not recommend it unless you really, REALLY need to get rid
of the div. Creating such a filter can really hurt performance. If you decide you
REALLY need to, <a href="http://blog.madskristensen.dk/">Mads Kristensen</a> has an
example of how to make a filter. He is using <em>Response.Filter</em> to <a href="http://blog.madskristensen.dk/post/An-HttpModule-that-moves-ViewState-to-the-bottom.aspx">move
ViewState to the bottom of the page</a>.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/TheDIVTagAroundHiddenASPNETInputFields.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/TheDIVTagAroundHiddenASPNETInputFields.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=f0c6b39a-a84b-44d4-ac74-5b272c398992" />
      </body>
      <title>The DIV Tag Around Hidden ASP.NET Input Fields</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,f0c6b39a-a84b-44d4-ac74-5b272c398992.aspx</guid>
      <link>http://VaultOfThoughts.net/TheDIVTagAroundHiddenASPNETInputFields.aspx</link>
      <pubDate>Tue, 12 Jun 2007 19:53:08 GMT</pubDate>
      <description>&lt;p&gt;
Today I've got a question if it is possible to make ASP.NET not to render the&amp;nbsp;&lt;em&gt;div&lt;/em&gt; tag
around it's special hidden fields&amp;nbsp;(i.e.: "__EVENTTARGET"). I have never thought
about it before, so I didn't know the answer. I had to investigate.&amp;nbsp;After a brief
overview of the state of things, I have come to believe that there is no easy way
to do achieve this. To confirm my suspicions I used the Reflector. 
&lt;p&gt;
I have found that it is indeed possible to make ASP.NET not render the div tag if &lt;em&gt;RenderDivAroundHiddenInputs&lt;/em&gt; property
of &lt;em&gt;HtmlTextWriter&lt;/em&gt; class returns false or if &lt;em&gt;LegacyRendering&lt;/em&gt; is enabled.
You can enable the later by setting &lt;em&gt;xhtmlConformance mode&lt;/em&gt; to &lt;em&gt;Legacy&lt;/em&gt; in &lt;em&gt;Web.config&lt;/em&gt; file.
Unfortunately the &lt;em&gt;RenderDivAroundHiddenInputs&lt;/em&gt; property of &lt;em&gt;HtmlTextWriter&lt;/em&gt; class
always returns true and you cannot change it because it is internal. Additional investigation
has shown that there is a class that overrides that property to return false. The
class was: &lt;em&gt;XhtmlTextWriter&lt;/em&gt;. 
&lt;p&gt;
How to make ASP.NET use &lt;em&gt;XhtmlTextWriter&lt;/em&gt; instead of normal &lt;em&gt;HtmlTextWriter&lt;/em&gt;?
I was unable to find a way to configure ASP.NET to do it. Setting &lt;em&gt;xhtmlConformance&lt;/em&gt; &lt;em&gt;mode&lt;/em&gt; to &lt;em&gt;strict&lt;/em&gt; doesn't
help. Google also is not very helpful. The only way seams to be either override &lt;em&gt;Render&lt;/em&gt; method
of a &lt;em&gt;Page&lt;/em&gt; class like this: &lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; Render(&lt;span style="COLOR: rgb(43,145,175)"&gt;HtmlTextWriter&lt;/span&gt; writer)
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;base&lt;/span&gt;.Render(&lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;XhtmlTextWriter&lt;/span&gt;(writer));
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Or better yet to override the &lt;em&gt;CreateHtmlTextWriter&lt;/em&gt; like this: &lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;HtmlTextWriter&lt;/span&gt; CreateHtmlTextWriter(System.IO.&lt;span style="COLOR: rgb(43,145,175)"&gt;TextWriter&lt;/span&gt; tw)
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;XhtmlTextWriter&lt;/span&gt;(&lt;span style="COLOR: rgb(0,0,255)"&gt;base&lt;/span&gt;.CreateHtmlTextWriter(tw));
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
This however requires you to use a kind of BasePage class for each page in your project
(which is good, but not everyone likes it). Another concern I have is the fact that
there is very little information on &lt;em&gt;XhtmlTextWriter&lt;/em&gt; class, and the one that
is available&amp;nbsp;seams to suggest that the class is meant rather for mobile devices. 
&lt;p&gt;
There is one more way you can try to solve the problem. You can use &lt;em&gt;Response.Filter&lt;/em&gt; to
do the job, but I would not recommend it unless you really, REALLY need to get rid
of the div. Creating such a filter can really hurt performance. If you decide you
REALLY need to, &lt;a href="http://blog.madskristensen.dk/"&gt;Mads Kristensen&lt;/a&gt; has an
example&amp;nbsp;of how to make a filter. He is using&amp;nbsp;&lt;em&gt;Response.Filter&lt;/em&gt;&amp;nbsp;to &lt;a href="http://blog.madskristensen.dk/post/An-HttpModule-that-moves-ViewState-to-the-bottom.aspx"&gt;move
ViewState to the bottom of the page&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/TheDIVTagAroundHiddenASPNETInputFields.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/TheDIVTagAroundHiddenASPNETInputFields.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=f0c6b39a-a84b-44d4-ac74-5b272c398992" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=a843178a-1a6c-4827-9e95-78660527d7d9</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,a843178a-1a6c-4827-9e95-78660527d7d9.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my <a href="RecursivePropertiesOfASPNETControls.aspx">last article</a>, I have
described a "feature" of some of the properties of ASP.NET controls: recursive properties.
It is obvious why it is important to know about it, but how does it impact the development? 
</p>
        <p>
Imagine that you need the following functionality: Page with on it controls and among
them, there is a panel containing buttons such as Save, Cancel etc. This panel is
inside some kind of container control - like div tag. The goal is to detect if
there is at least one visible button inside the container. If so, render the container,
otherwise, containing div should not be rendered at all. 
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">div</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(255,0,0)">id</span>
          <span style="COLOR: rgb(0,0,255)">="ActionPanel"&gt; </span>
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">asp</span>
          <span style="COLOR: rgb(0,0,255)">:</span>
          <span style="COLOR: rgb(163,21,21)">Button</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(255,0,0)">ID</span>
          <span style="COLOR: rgb(0,0,255)">="Save"</span>
          <span style="COLOR: rgb(0,0,255)">/&gt; </span>
          <span style="COLOR: rgb(0,0,255)">&lt;</span>
          <span style="COLOR: rgb(163,21,21)">asp</span>
          <span style="COLOR: rgb(0,0,255)">:</span>
          <span style="COLOR: rgb(163,21,21)">Button</span>
          <span style="COLOR: rgb(255,0,0)">runat</span>
          <span style="COLOR: rgb(0,0,255)">="server"</span>
          <span style="COLOR: rgb(255,0,0)">ID</span>
          <span style="COLOR: rgb(0,0,255)">="Cancel"</span>
          <span style="COLOR: rgb(0,0,255)">/&gt;
&lt;/</span>
          <span style="COLOR: rgb(163,21,21)">div</span>
          <span style="COLOR: rgb(0,0,255)">&gt;</span>
        </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
The code to accomplish this is "easy". Get the reference to the div control (which
was made to runat="server"). Iterate over its child controls and for each control,
that was a Button, check its Visible property. If at least one of the property returns
true, the div should also be visible. Sample code to achieve this: 
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">protected</span>
          <span style="COLOR: rgb(0,0,255)">override</span>
          <span style="COLOR: rgb(0,0,255)">void</span> OnPreRender(<span style="COLOR: rgb(43,145,175)">EventArgs</span> e)
{ ActionPanel.Visible = GetActionPanelVisibleStatus(); } <span style="COLOR: rgb(0,0,255)">private</span><span style="COLOR: rgb(0,0,255)">bool</span> GetActionPanelVisibleStatus()
{ <span style="COLOR: rgb(0,0,255)">foreach</span> (<span style="COLOR: rgb(43,145,175)">Control</span> control <span style="COLOR: rgb(0,0,255)">in</span> ActionPanel.Controls)
{ <span style="COLOR: rgb(0,0,255)">if</span> ((control <span style="COLOR: rgb(0,0,255)">is</span><span style="COLOR: rgb(43,145,175)">Button</span>)
&amp;&amp; (control.Visible)) { <span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(0,0,255)">true</span>;
} } <span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(0,0,255)">false</span>;
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
I'm using a loop over all child controls to make it more generic. 
</p>
        <p>
The only problem is that above implementation does not work in some cases. The
problematic situation only occurs if the div's Visible property is set to false. Once
invisible, it stays that way with no way of going back to visible state! Why is that?
Of course it is because of the <a href="RecursivePropertiesOfASPNETControls.aspx">recursive
nature of Visible property</a>. With this in mind it is easy to fix
the problem by setting Visible property of the container control to true, before processing
it's child controls: 
</p>
        <pre class="code">
          <span style="COLOR: rgb(0,0,255)">protected</span>
          <span style="COLOR: rgb(0,0,255)">override</span>
          <span style="COLOR: rgb(0,0,255)">void</span> OnPreRender(<span style="COLOR: rgb(43,145,175)">EventArgs</span> e)
{ ActionPanel.Visible = <span style="COLOR: rgb(0,0,255)">true</span>; ActionPanel.Visible
= GetActionPanelVisibleStatus(); } </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Makes sense?
</p>
        <p>
Gathering it all up, it is now easy to build a self-hiding ASP.NET control (or UserControl).
Just remember about recursion!
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/SelfHidingASPNETControl.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/SelfHidingASPNETControl.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=a843178a-1a6c-4827-9e95-78660527d7d9" />
      </body>
      <title>Self-Hiding ASP.NET Control</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,a843178a-1a6c-4827-9e95-78660527d7d9.aspx</guid>
      <link>http://VaultOfThoughts.net/SelfHidingASPNETControl.aspx</link>
      <pubDate>Tue, 12 Jun 2007 19:33:37 GMT</pubDate>
      <description>&lt;p&gt;
In my &lt;a href="RecursivePropertiesOfASPNETControls.aspx"&gt;last article&lt;/a&gt;, I have
described a "feature" of some of the properties of ASP.NET controls: recursive properties.
It is obvious why it is important to know about it, but how does it impact the development? 
&lt;p&gt;
Imagine that you need the following functionality: Page with on it controls and among
them, there is a panel containing buttons such as Save, Cancel etc. This panel is
inside some kind of container control - like div tag. The goal&amp;nbsp;is to detect if
there is at least one visible button inside the container. If so, render the container,
otherwise, containing div should not be rendered at all. &lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;div&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;id&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="ActionPanel"&amp;gt; &lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;asp&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Button&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Save"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt; &lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;asp&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;:&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Button&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;runat&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/span&gt; &lt;span style="COLOR: rgb(255,0,0)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;="Cancel"&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;div&lt;/span&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The code to accomplish this is "easy". Get the reference to the div control (which
was made to runat="server"). Iterate over its child controls and for each control,
that was a Button, check its Visible property. If at least one of the property returns
true, the div should also be visible. Sample code to achieve this: &lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; OnPreRender(&lt;span style="COLOR: rgb(43,145,175)"&gt;EventArgs&lt;/span&gt; e)
{ ActionPanel.Visible = GetActionPanelVisibleStatus(); } &lt;span style="COLOR: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;bool&lt;/span&gt; GetActionPanelVisibleStatus()
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt; control &lt;span style="COLOR: rgb(0,0,255)"&gt;in&lt;/span&gt; ActionPanel.Controls)
{ &lt;span style="COLOR: rgb(0,0,255)"&gt;if&lt;/span&gt; ((control &lt;span style="COLOR: rgb(0,0,255)"&gt;is&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;Button&lt;/span&gt;)
&amp;amp;&amp;amp; (control.Visible)) { &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;;
} } &lt;span style="COLOR: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;false&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
I'm using a loop over all child controls to make it more generic. 
&lt;p&gt;
The only problem&amp;nbsp;is that above implementation does not work in some cases. The
problematic situation only occurs if the div's Visible property is set to false. Once
invisible, it stays that way with no way of going back to visible state! Why is that?
Of course it is because of the &lt;a href="RecursivePropertiesOfASPNETControls.aspx"&gt;recursive
nature of Visible property&lt;/a&gt;.&amp;nbsp;With this in mind&amp;nbsp;it&amp;nbsp;is easy to fix
the problem by setting Visible property of the container control to true, before processing
it's child controls: &lt;pre class=code&gt;&lt;span style="COLOR: rgb(0,0,255)"&gt;protected&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="COLOR: rgb(0,0,255)"&gt;void&lt;/span&gt; OnPreRender(&lt;span style="COLOR: rgb(43,145,175)"&gt;EventArgs&lt;/span&gt; e)
{ ActionPanel.Visible = &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;; ActionPanel.Visible
= GetActionPanelVisibleStatus(); } &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Makes sense?
&lt;/p&gt;
&lt;p&gt;
Gathering it all up, it is now easy to build a self-hiding ASP.NET control (or UserControl).
Just remember about recursion!
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/SelfHidingASPNETControl.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/SelfHidingASPNETControl.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=a843178a-1a6c-4827-9e95-78660527d7d9" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=b40e7c0f-a829-4fa2-9ff7-13a0fd4f3468</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,b40e7c0f-a829-4fa2-9ff7-13a0fd4f3468.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of the other days I encountered a strange problem. I was unable to set the
Visible property of one of built in ASP.NET controls to true. I had verified
that it is indeed <em>true</em> that I'm passing as a new value for the property.
I had even used the Watch and Immediate Windows to set that value - nothing worked.
I had decided to see what is going on and so I looked at the source code of the Control's
Visible property... 
</p>
        <p>
What I have discovered is that the Visible property is in fact a recursive property!
What does it mean? In short, the Visible property will only be true if the Parent's
Visible property is true which in turn will be true if... and so on, up to the top
of the control hierarchy. No a bad idea after if you think about it. 
</p>
        <p>
This feature obviously has some impact on how everything in ASP.NET works. As an example
take a look at the following code: 
</p>
        <pre class="code">
          <span style="COLOR: rgb(43,145,175)">Control</span> parent = <span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">Control</span>(); <span style="COLOR: rgb(43,145,175)">Control</span> child
= <span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">Control</span>();
child.Visible = <span style="COLOR: rgb(0,0,255)">false</span>; Response.Write(child.Visible
+ <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>); child.Visible = <span style="COLOR: rgb(0,0,255)">true</span>;
Response.Write(child.Visible + <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>);
parent.Controls.Add(child); parent.Visible = <span style="COLOR: rgb(0,0,255)">true</span>;
Response.Write(child.Visible + <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>);
parent.Visible = <span style="COLOR: rgb(0,0,255)">false</span>; Response.Write(child.Visible
+ <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>); child.Visible = <span style="COLOR: rgb(0,0,255)">true</span>;
Response.Write(child.Visible + <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>);
parent.Visible = <span style="COLOR: rgb(0,0,255)">true</span>; Response.Write(child.Visible
+ <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>); </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
What would you expect the output to be? Below is the output generated by the above
code. 
</p>
        <p>
False<br />
True<br />
True<br /><strong>False<br />
False</strong><br />
True 
</p>
        <p>
The marked values are not so obvious, but if we take into consideration, the
fact that the Visible property is recursive, everything starts to make sense... or
does it? 
</p>
        <p>
At first I thought that there should be more of those type of properties. It
makes sense for a Visible property to be recursive since if we do not render a parent,
we won't render a child. Why then, wouldn't the other, well known and widely used
property: <em>EnableViewState</em> work in a similar way? If we repeat the same experiment
we will get a different set of results: 
</p>
        <pre class="code">
          <span style="COLOR: rgb(43,145,175)">Control</span> parent = <span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">Control</span>(); <span style="COLOR: rgb(43,145,175)">Control</span> child
= <span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(43,145,175)">Control</span>();
child.EnableViewState = <span style="COLOR: rgb(0,0,255)">false</span>; Response.Write(child.EnableViewState
+ <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>); child.EnableViewState
= <span style="COLOR: rgb(0,0,255)">true</span>; Response.Write(child.EnableViewState
+ <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>); parent.Controls.Add(child);
parent.EnableViewState = <span style="COLOR: rgb(0,0,255)">true</span>; Response.Write(child.EnableViewState
+ <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>); parent.EnableViewState
= <span style="COLOR: rgb(0,0,255)">false</span>; Response.Write(child.EnableViewState
+ <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>); child.EnableViewState
= <span style="COLOR: rgb(0,0,255)">true</span>; Response.Write(child.EnableViewState
+ <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>); parent.EnableViewState
= <span style="COLOR: rgb(0,0,255)">true</span>; Response.Write(child.EnableViewState
+ <span style="COLOR: rgb(163,21,21)">"&lt;br /&gt;"</span>);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
And results: 
</p>
        <p>
False<br />
True<br />
True<br /><strong>True<br />
True</strong><br />
True 
</p>
        <p>
Notice, that this time we get what we set. Does it make sense? How is EnableViewState
different from Visible? The functionality that depends on both of those properties
only works if Parent works. 
</p>
        <p>
I'm not sure if there are more properties that work in a recursive way. I believe
that it would be better if all properties worked that way which would make development
more predictable. Unfortunately Visible is the only property that I know of that works
in a recursive way, but there may be more. ASP.NET never stops to surprise me.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/RecursivePropertiesOfASPNETControls.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/RecursivePropertiesOfASPNETControls.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b40e7c0f-a829-4fa2-9ff7-13a0fd4f3468" />
      </body>
      <title>Recursive Properties Of ASP.NET Controls</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,b40e7c0f-a829-4fa2-9ff7-13a0fd4f3468.aspx</guid>
      <link>http://VaultOfThoughts.net/RecursivePropertiesOfASPNETControls.aspx</link>
      <pubDate>Tue, 12 Jun 2007 19:10:07 GMT</pubDate>
      <description>&lt;p&gt;
One of the other days&amp;nbsp;I encountered a strange problem. I was unable to set the
Visible property of one of built in ASP.NET controls to true. I&amp;nbsp;had verified
that it is indeed &lt;em&gt;true&lt;/em&gt; that I'm passing as a new value&amp;nbsp;for the property.
I&amp;nbsp;had even used the Watch and Immediate Windows to set that value - nothing worked.
I had decided to see what is going on and so I looked at the source code of the Control's
Visible property... 
&lt;p&gt;
What I have discovered is that the Visible property is in fact a recursive property!
What does it mean? In short, the Visible property will only be true if the Parent's
Visible property is true which in turn will be true if... and so on, up to the top
of the control hierarchy. No a bad idea after if you think about it. 
&lt;p&gt;
This feature obviously has some impact on how everything in ASP.NET works. As an example
take a look at the following code: &lt;pre class=code&gt;&lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt; parent
= &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt;(); &lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt; child
= &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt;();
child.Visible = &lt;span style="COLOR: rgb(0,0,255)"&gt;false&lt;/span&gt;; Response.Write(child.Visible
+ &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;); child.Visible = &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;;
Response.Write(child.Visible + &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;);
parent.Controls.Add(child); parent.Visible = &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;;
Response.Write(child.Visible + &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;);
parent.Visible = &lt;span style="COLOR: rgb(0,0,255)"&gt;false&lt;/span&gt;; Response.Write(child.Visible
+ &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;); child.Visible = &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;;
Response.Write(child.Visible + &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;);
parent.Visible = &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;; Response.Write(child.Visible
+ &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;); &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
What would you expect the output to be? Below is the output generated by the above
code. 
&lt;p&gt;
False&lt;br&gt;
True&lt;br&gt;
True&lt;br&gt;
&lt;strong&gt;False&lt;br&gt;
False&lt;/strong&gt;
&lt;br&gt;
True 
&lt;p&gt;
The marked&amp;nbsp;values are not so obvious, but if we take into consideration, the
fact that the Visible property is recursive, everything starts to make sense... or
does it? 
&lt;p&gt;
At first I thought that there should be more of those type of properties.&amp;nbsp;It
makes sense for a Visible property to be recursive since if we do not render a parent,
we won't render a child. Why then, wouldn't the other, well known and widely used
property: &lt;em&gt;EnableViewState&lt;/em&gt; work in a similar way? If we repeat the same experiment
we will get a different set of results: &lt;pre class=code&gt;&lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt; parent
= &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt;(); &lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt; child
= &lt;span style="COLOR: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="COLOR: rgb(43,145,175)"&gt;Control&lt;/span&gt;();
child.EnableViewState = &lt;span style="COLOR: rgb(0,0,255)"&gt;false&lt;/span&gt;; Response.Write(child.EnableViewState
+ &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;); child.EnableViewState
= &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;; Response.Write(child.EnableViewState
+ &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;); parent.Controls.Add(child);
parent.EnableViewState = &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;; Response.Write(child.EnableViewState
+ &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;); parent.EnableViewState
= &lt;span style="COLOR: rgb(0,0,255)"&gt;false&lt;/span&gt;; Response.Write(child.EnableViewState
+ &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;); child.EnableViewState
= &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;; Response.Write(child.EnableViewState
+ &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;); parent.EnableViewState
= &lt;span style="COLOR: rgb(0,0,255)"&gt;true&lt;/span&gt;; Response.Write(child.EnableViewState
+ &lt;span style="COLOR: rgb(163,21,21)"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
And results: 
&lt;p&gt;
False&lt;br&gt;
True&lt;br&gt;
True&lt;br&gt;
&lt;strong&gt;True&lt;br&gt;
True&lt;/strong&gt;
&lt;br&gt;
True 
&lt;p&gt;
Notice, that this time we get what we set. Does it make sense? How is EnableViewState
different from Visible? The functionality that depends on both of those properties
only works if Parent works. 
&lt;p&gt;
I'm not sure if there are more properties that work in a recursive way. I believe
that it would be better if all properties worked that way which would make development
more predictable. Unfortunately Visible is the only property that I know of that works
in a recursive way, but there may be more. ASP.NET never stops to surprise me.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/RecursivePropertiesOfASPNETControls.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/RecursivePropertiesOfASPNETControls.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b40e7c0f-a829-4fa2-9ff7-13a0fd4f3468" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=e8b4ab5b-0268-4548-8bc0-f7ec3d5bcc09</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,e8b4ab5b-0268-4548-8bc0-f7ec3d5bcc09.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've been playing with ASP.NET Localization features lately and I have mixed feelings
about it. I mean, the Localization features of ASP.NET are great, but you have to
be very careful not to mess up. 
</p>
        <p>
Visual Studio 2005 allows for easy localization of a page or a control via the Tools-&gt;Generate
Local Resource menu item. I have tried this one on the source view of the page (the
view that I most often work with). The problem is that it does not work from this
view!!! The error message is:
</p>
        <p>
          <img src="http://vaultofthoughts.net/content/binary/GenerateLocalResource.jpg" border="0" />
        </p>
        <p>
---------------------------<br />
Microsoft Visual Studio<br />
---------------------------<br />
Source view does not support resource generation. Switch to design view to generate
resources.<br />
---------------------------<br />
OK 
<br />
---------------------------
</p>
        <p>
Ooook. I thought that we have to separate presentation from logic but I suppose this
is an exception from the rule. No wonder it was so hard to remove "the feature" of
reformating the code after switching from source view to design view in Visual Studio
2003 if they have a code with such high quality. There is simply no separation between
the model and the view so problems are to be expected.
</p>
        <p>
So I have switched to the design view and on my sample page, with only a GridView
with few collumns, everything worked fine. I've got the App_LocalResources folder
and a Default.aspx.resx file in it and every localizable property was already in there.
The aspx markup was also modified - meta:resourcekey attributes have been added to
all relevant places. Nice? Not so, unfortunately :-(.
</p>
        <p>
I have tried the same technique on a real page from a real project. On the page, there
were a couple of my custom controls. Invoking the Generate Local Resource function
on such a page made a lot of mess with the html/aspx code. Some controls have been
added as inner controls etc. This is probably the very reason why cybercrypt15/glenn
writes that <a href="http://forums.asp.net/1702834/ShowThread.aspx">you should not
use the Generate Local Resource feature</a>.
</p>
        <p>
If you do all the work manually (wich is not such a big task after all - just add
a meta:resourcekey attribute where necessary), you will find that Localization is
really a nice feature.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/ASPNETLocalizationPitfalls.aspx">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/ASPNETLocalizationPitfalls.aspx" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=e8b4ab5b-0268-4548-8bc0-f7ec3d5bcc09" />
      </body>
      <title>ASP.NET Localization - Pitfalls</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,e8b4ab5b-0268-4548-8bc0-f7ec3d5bcc09.aspx</guid>
      <link>http://VaultOfThoughts.net/ASPNETLocalizationPitfalls.aspx</link>
      <pubDate>Mon, 14 May 2007 08:40:29 GMT</pubDate>
      <description>&lt;p&gt;
I've been playing with ASP.NET Localization features lately and I have mixed feelings
about it. I mean, the Localization features of ASP.NET are great, but you have to
be very careful not to mess up. 
&lt;/p&gt;
&lt;p&gt;
Visual Studio 2005 allows for easy localization of a page or a control via the Tools-&amp;gt;Generate
Local Resource menu item. I have tried this one on the source view of the page (the
view that I most often work with). The problem is that it does not work from this
view!!! The error message is:
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://vaultofthoughts.net/content/binary/GenerateLocalResource.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
---------------------------&lt;br&gt;
Microsoft Visual Studio&lt;br&gt;
---------------------------&lt;br&gt;
Source view does not support resource generation. Switch to design view to generate
resources.&lt;br&gt;
---------------------------&lt;br&gt;
OK 
&lt;br&gt;
---------------------------
&lt;/p&gt;
&lt;p&gt;
Ooook. I thought that we have to separate presentation from logic but I suppose this
is an exception from the rule. No wonder it was so hard to remove "the feature" of
reformating the code after switching from source view to design view in Visual Studio
2003 if they have a code with such high quality. There is simply no separation between
the model and the view so problems are to be expected.
&lt;/p&gt;
&lt;p&gt;
So I have switched to the design view and on my sample page, with only a GridView
with few collumns, everything worked fine. I've got the App_LocalResources folder
and a Default.aspx.resx file in it and every localizable property was already in there.
The aspx markup was also modified - meta:resourcekey attributes have been added to
all relevant places. Nice? Not so, unfortunately :-(.
&lt;/p&gt;
&lt;p&gt;
I have tried the same technique on a real page from a real project. On the page, there
were a couple of my custom controls. Invoking the Generate Local Resource function
on such a page made a lot of mess with the html/aspx code. Some controls have been
added as inner controls etc. This is probably the very reason why cybercrypt15/glenn
writes that &lt;a href="http://forums.asp.net/1702834/ShowThread.aspx"&gt;you should not
use the Generate Local Resource feature&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
If you do all the work manually (wich is not such a big task after all - just add
a meta:resourcekey attribute where necessary), you will find that Localization is
really a nice feature.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/ASPNETLocalizationPitfalls.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/ASPNETLocalizationPitfalls.aspx" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=e8b4ab5b-0268-4548-8bc0-f7ec3d5bcc09" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=6e102099-3c07-48a0-8b04-e3048f146419</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,6e102099-3c07-48a0-8b04-e3048f146419.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
ASP.NET comes with a set of controls called "Validation Controls". Useful as they
are, there are few features missing to make the picture complete. Some of the shortcomings
have been fixed with the ASP.NET 2.0. This includes the ValidationGroup property.
Some of the missing features I have described in my other articles: <a href="http://ValidationControlsAsImagesTake2.aspx">Validation
Controls As Images - Take 2</a>, <a href="http://ValidationControlsAsImagesWorkaround.aspx">Validation
Control As Images - Workaround</a> and <a href="http://ValidationControlsVsControlAdapters.aspx">Validation
Controls Vs Control Adapters</a>.
</p>
        <p>
One thing that I have recently discovered missing from the set of features ASP.NET
validation infrastructure provides was the way to perform initial validation of a
page right after it has been downloaded by the browser for the first time. Imagine
for example a form on a page where you instantly know which fields are required. Normally
to do it, you would emphasize the labels of those fields in some way - probably by
using a stronger/bigger font. I like not to repeat myself so I've tried to use the
Validation Controls just for this. Instead of a bold label I wanted to have an ErrorMessage
of each ValidationControl displayed for each field that requires validation.
</p>
        <p>
After a bit of searching I have found no solution to my problem so, as usual, I have
used my old mantra: "use the source Luk". What I have found is that it should be possible
to do it so after few failed approaches I have found the way.
</p>
        <p>
In order to perform initial validation on an ASP.NET page I have used a following
JavaScript function:
</p>
        <p>
          <span style="COLOR: blue">function</span> initialValidation()<br />
{<br />
    <span style="COLOR: blue">if</span> (<span style="COLOR: blue">typeof</span>(Page_Validators)
== <span style="COLOR: maroon">"undefined"</span>) 
<br />
    {<br />
        <span style="COLOR: blue">return</span><span style="COLOR: maroon">true</span>;<br />
    }<br />
    <span style="COLOR: blue">var</span> i;<br />
    <span style="COLOR: blue">for</span> (i = <span style="COLOR: maroon">0</span>;
i &lt; Page_Validators.length; i++) 
<br />
    {<br />
        ValidatorValidate(Page_Validators[i], <span style="COLOR: maroon">""</span>, <span style="COLOR: blue">null</span>);<br />
    }<br />
}
</p>
        <p>
I have put the call to this function in a body.onload event of an HTML page (the only
place that I have found it to work).
</p>
        <p>
The interesting thing is that if you are using ValidationSummary to display a summary
of all errors on a page, it will not be shown after the initial validation. It will
be shown if a user despite having the error message displayed near the input, tries
to submit the page.
</p>
        <p>
As a side note: don't forget to validate everything on the server-side! Do not trust
the data that comes from the user!
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/ASPNETInitialValidation.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/ASPNETInitialValidation.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=6e102099-3c07-48a0-8b04-e3048f146419" />
      </body>
      <title>ASP.NET Initial Validation</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,6e102099-3c07-48a0-8b04-e3048f146419.aspx</guid>
      <link>http://VaultOfThoughts.net/ASPNETInitialValidation.aspx</link>
      <pubDate>Fri, 11 May 2007 12:58:04 GMT</pubDate>
      <description>&lt;p&gt;
ASP.NET comes with a set of controls called "Validation Controls". Useful as they
are, there are few features missing to make the picture complete. Some of the shortcomings
have been fixed with the ASP.NET 2.0. This includes the ValidationGroup property.
Some of the missing features I have described in my other articles: &lt;a href="http://ValidationControlsAsImagesTake2.aspx"&gt;Validation
Controls As Images - Take 2&lt;/a&gt;, &lt;a href="http://ValidationControlsAsImagesWorkaround.aspx"&gt;Validation
Control As Images - Workaround&lt;/a&gt; and &lt;a href="http://ValidationControlsVsControlAdapters.aspx"&gt;Validation
Controls Vs Control Adapters&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
One thing that I have recently discovered missing from the set of features ASP.NET
validation infrastructure provides was the way to perform initial validation of a
page right after it has been downloaded by the browser for the first time. Imagine
for example a form on a page where you instantly know which fields are required. Normally
to do it, you would emphasize the labels of those fields in some way - probably by
using a stronger/bigger font. I like not to repeat myself so I've tried to use the
Validation Controls just for this. Instead of a bold label I wanted to have an ErrorMessage
of each ValidationControl displayed for each field that requires validation.
&lt;/p&gt;
&lt;p&gt;
After a bit of searching I have found no solution to my problem so, as usual, I have
used my old mantra: "use the source Luk". What I have found is that it should be possible
to do it so after few failed approaches I have found the way.
&lt;/p&gt;
&lt;p&gt;
In order to perform initial validation on an ASP.NET page I have used a following
JavaScript function:
&lt;/p&gt;
&lt;p&gt;
&lt;span style="COLOR: blue"&gt;function&lt;/span&gt; initialValidation()&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; (&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(Page_Validators)
== &lt;span style="COLOR: maroon"&gt;"undefined"&lt;/span&gt;) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: maroon"&gt;true&lt;/span&gt;;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="COLOR: blue"&gt;var&lt;/span&gt; i;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="COLOR: blue"&gt;for&lt;/span&gt; (i = &lt;span style="COLOR: maroon"&gt;0&lt;/span&gt;;
i &amp;lt; Page_Validators.length; i++) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ValidatorValidate(Page_Validators[i], &lt;span style="COLOR: maroon"&gt;""&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
}
&lt;/p&gt;
&lt;p&gt;
I have put the call to this function in a body.onload event of an HTML page (the only
place that I have found it to work).
&lt;/p&gt;
&lt;p&gt;
The interesting thing is that if you are using ValidationSummary to display a summary
of all errors on a page, it will not be shown after the initial validation. It will
be shown if a user despite having the error message displayed near the input, tries
to submit the page.
&lt;/p&gt;
&lt;p&gt;
As a side note: don't forget to validate everything on the server-side! Do not trust
the data that comes from the user!
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/ASPNETInitialValidation.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/ASPNETInitialValidation.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=6e102099-3c07-48a0-8b04-e3048f146419" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=56cf1f06-cae7-4bcd-890f-2b5ea79b17ab</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,56cf1f06-cae7-4bcd-890f-2b5ea79b17ab.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Every once in a while while working with ASP.NET there comes a time when you
need to have a DropDownList with a fixed set of items. Or maybe the items are retrieved
from the database, but still every instance of the control will have exactly the same
items. How to do it efficiently?
</p>
        <p>
Lets look at the simple example of a DropDownList with country names - the one you
see on many pages. Wouldn't it be nice to have a reusable control for this?
</p>
        <p>
The simplest way to create a Self Populating DropDownList is to inherit from the standard
one, set the DataSource somewhere and call DataBind(). That is the tricky part.
</p>
        <p>
If you have been working with ASP.NET for a while you are probably aware of the ViewState
thingy. As good as it is, it can often make your page too large. This is specially
true for DataBound controls. A simple Counties DropDownList would make your ViewState
huge! So what can be done about it? Just disabling ViewState won't help because usually
disabling it breaks some of the other parts of control's functionality.
</p>
        <p>
Dealing with ViewState is simple once you get to know one very important thing. Only
changes made after the Init phase of the Life Cycle are persisted to the ViewState!
Or more precisely, changes only changes made after the control has started tracking
ViewState (TrackViewState method has been called). It means that everything you
put to the ViewState before that, will not be persisted. By default controls will
have their TrackViewState method called right after the Init phase.
</p>
        <p>
We can use this knowledge to our advantage. If we populate our DropDownList before
or during the Init phase we will not affect ViewState. Doing so however has one drawback:
we have to perform this operation on each request and not only when IsPostBack is
false. This is not an issue if we use some kind of cache and we populate a lots of
controls with the same data.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/SelfPopulatingDropDownList.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/SelfPopulatingDropDownList.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=56cf1f06-cae7-4bcd-890f-2b5ea79b17ab" />
      </body>
      <title>Self Populating DropDownList</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,56cf1f06-cae7-4bcd-890f-2b5ea79b17ab.aspx</guid>
      <link>http://VaultOfThoughts.net/SelfPopulatingDropDownList.aspx</link>
      <pubDate>Tue, 13 Mar 2007 08:35:32 GMT</pubDate>
      <description>&lt;p&gt;
Every once in a while while working with ASP.NET&amp;nbsp;there comes a time when you
need to have a DropDownList with a fixed set of items. Or maybe the items are retrieved
from the database, but still every instance of the control will have exactly the same
items. How to do it efficiently?
&lt;/p&gt;
&lt;p&gt;
Lets look at the simple example of a DropDownList with country names - the one you
see on many pages. Wouldn't it be nice to have a reusable control for this?
&lt;/p&gt;
&lt;p&gt;
The simplest way to create a Self Populating DropDownList is to inherit from the standard
one, set the DataSource somewhere and call DataBind(). That is the tricky part.
&lt;/p&gt;
&lt;p&gt;
If you have been working with ASP.NET for a while you are probably aware of the ViewState
thingy. As good as it is, it can often make your page too large. This is specially
true for DataBound controls. A simple Counties DropDownList would make your ViewState
huge! So what can be done about it? Just disabling ViewState won't help because usually
disabling it breaks some of the other parts of control's functionality.
&lt;/p&gt;
&lt;p&gt;
Dealing with ViewState is simple once you get to know one very important thing. Only
changes made after the Init phase of the Life Cycle are persisted to the ViewState!
Or more precisely, changes&amp;nbsp;only changes made after the control has started tracking
ViewState (TrackViewState method has been called).&amp;nbsp;It means that everything you
put to the ViewState before that, will not be persisted. By default controls will
have their TrackViewState method called right after the Init phase.
&lt;/p&gt;
&lt;p&gt;
We can use this knowledge to our advantage. If we populate our DropDownList before
or during the Init phase we will not affect ViewState. Doing so however has one drawback:
we have to perform this operation on each request and not only when IsPostBack is
false. This is not an issue if we use some kind of cache and we populate a lots of
controls with the same data.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/SelfPopulatingDropDownList.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/SelfPopulatingDropDownList.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=56cf1f06-cae7-4bcd-890f-2b5ea79b17ab" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=c477b293-e063-4125-b027-aa88dd0befd1</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,c477b293-e063-4125-b027-aa88dd0befd1.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Every ASP.NET developer knows (or at least should know) about the Life Cycle. Page
has it's Life Cycle, controls have theirs Life Cycle (more or less the same as the
one of the Page). There is a good article on MSDN regarding the <a href="http://msdn2.microsoft.com/en-us/library/ms178472.aspx">Page
Life Cycle</a>. From it, you will learn that at any given moment, the Page and all
controls are in the same "phase" of the Life Cycle, be it Init, Load, PreRender
or Unload.
</p>
        <p>
The interesting part about the Life Cycle is when it comes to dynamically added controls.
The controls you add in code using Controls.Add call. Since controls also have the
Init, Load and PreRender and Unload phases, what happens when we add a new control
during a PreRender phase? As stated above, Page and all controls are always in the
same phase. So what happens?
</p>
        <p>
The same article explains this phenomenon and calls it "Catch-up". What it basically
means is that at the time of adding a control to a Controls collection, the new control
"walks" through all it's phases until it "catches-up" with the current phase of it's
parent control (i.e. Page).
</p>
        <p>
In order to better understand this, you can use the following example. On your page
override an OnPreRender method and add a following code:
</p>
        <pre>
          <span style="COLOR: blue">protected</span>
          <span style="COLOR: blue">override</span>
          <span style="COLOR: blue">void</span> OnPreRender(EventArgs
e) { <span style="COLOR: blue">base</span>.OnPreRender(e); Button b = <span style="COLOR: blue">new</span> Button();
b.Load += <span style="COLOR: blue">new</span> EventHandler(b_Load); b.Init += <span style="COLOR: blue">new</span> EventHandler(b_Init);
b.PreRender += <span style="COLOR: blue">new</span> EventHandler(b_PreRender); b.Unload
+= <span style="COLOR: blue">new</span> EventHandler(b_Unload); Controls.Add(b); }</pre>
        <p>
You will also need to create the event handler methods. As the next step, put a break
point on each of those handler methods and run the debugger to see what
happens. You will notice that when control is added to a collection of Controls, it's
events are fired immediately to catch up with the phase of its parent.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/CatchupEvents.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/CatchupEvents.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=c477b293-e063-4125-b027-aa88dd0befd1" />
      </body>
      <title>Catch-up Events</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,c477b293-e063-4125-b027-aa88dd0befd1.aspx</guid>
      <link>http://VaultOfThoughts.net/CatchupEvents.aspx</link>
      <pubDate>Wed, 07 Mar 2007 15:19:03 GMT</pubDate>
      <description>&lt;p&gt;
Every ASP.NET developer knows (or at least should know) about the Life Cycle. Page
has it's Life Cycle, controls have theirs Life Cycle (more or less the same as the
one of the Page). There is a good article on MSDN regarding&amp;nbsp;the &lt;a href="http://msdn2.microsoft.com/en-us/library/ms178472.aspx"&gt;Page
Life Cycle&lt;/a&gt;. From it, you will learn that at any given moment, the Page and all
controls are in the same "phase" of&amp;nbsp;the Life Cycle, be it Init, Load, PreRender
or Unload.
&lt;/p&gt;
&lt;p&gt;
The interesting part about the Life Cycle is when it comes to dynamically added controls.
The controls you add in code using Controls.Add call. Since controls also have the
Init, Load and PreRender and Unload phases, what happens when we add a new control
during a PreRender phase? As stated above, Page and all controls are always in the
same phase. So what happens?
&lt;/p&gt;
&lt;p&gt;
The same article explains this phenomenon and calls it "Catch-up". What it basically
means is that at the time of adding a control to a Controls collection, the new control
"walks" through all it's phases until it "catches-up" with the current phase of it's
parent control (i.e. Page).
&lt;/p&gt;
&lt;p&gt;
In order to better understand this, you can use the following example. On your page
override an OnPreRender method and add a following code:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: blue"&gt;protected&lt;/span&gt; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; OnPreRender(EventArgs
e) { &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.OnPreRender(e); Button b = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; Button();
b.Load += &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; EventHandler(b_Load); b.Init += &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; EventHandler(b_Init);
b.PreRender += &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; EventHandler(b_PreRender); b.Unload
+= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; EventHandler(b_Unload); Controls.Add(b); }&lt;/pre&gt;
&lt;p&gt;
You will also need to create the event handler methods. As the next step, put a break
point on each of those handler&amp;nbsp;methods and run&amp;nbsp;the debugger to see what
happens. You will notice that when control is added to a collection of Controls, it's
events are fired immediately to catch up with the phase of its parent.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/CatchupEvents.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/CatchupEvents.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=c477b293-e063-4125-b027-aa88dd0befd1" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=d2fdeed1-d47a-4dcd-a212-64b93c3bcd3d</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,d2fdeed1-d47a-4dcd-a212-64b93c3bcd3d.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
After a while of thinking and <a href="validationcontrolsvscontroladapters.aspx">struggling
with Control Adapters</a> I was unable to find a good solution to get my Validation
Controls render as images. I have came to the following conclusions:
</p>
        <ol>
          <li>
Control Adapters will not help 
</li>
          <li>
Overriding each Validator is not a good solution 
</li>
          <li>
Creating my own Validation Controls is not a good solution</li>
        </ol>
        <p>
Given the above, I have once again looked at everything I have learned. I have looked
at the html code rendered by the controls and then... solution came to me! It turns
out that we don't need ASP.NET to get things working. ASP.NET stands in our way when
it comes to handling this task, so why not use a plain old CSS? So I did.
</p>
        <p>
When Validaton Control:
</p>
        <p>
&lt;asp:CompareValidator runat="server" ID="V" ControlToValidate="c1" ControlToCompare="c2"
Text="Error Text" ToolTip="Title Text"&gt;&lt;/asp:CompareValidator&gt;
</p>
        <p>
is rendered on a page it looks more or less like this:
</p>
        <p>
&lt;span id="ctl00_V" title="Title Text" style="color:Red;visibility:hidden;"&gt;Error
Text&lt;/span&gt;
</p>
        <p>
To make a nice image out of it, we need few things. First we need one tradeoff: we
have to remove the Text attribute, otherwise it will mess the looks a bit. Next we
decorate the server control with a CssClass attribute with a value such as "error".
Next we create a style:
</p>
        <p>
span.error<br />
{<br />
  display: -moz-inline-stack;<br />
  display: inline-block;<br />
  background-image: url(error.gif);<br />
  background-repeat: no-repeat;<br />
  height: 16px;<br />
  width: 16px;<br />
}
</p>
        <p>
Notice in the style few things. First, we need the &lt;span&gt; element to be "inline-block".
That is because we need it to have a height and width. Furthermore, FireFox does not
support "inline-block" (!!!) but we can use a simple workaround with "-moz-inline-stack"
value as described by Nicholas in <a href="http://www.nczonline.net/archive/2006/10/382">Pain
with inline-block article</a> (also see the comments). By default span (inline
element) will only be as large as the text it contain. Since in the proposed
solution, there will be no text in the &lt;span&gt; this is needed. Next, we set the
background-image to a desired url and set the width and height of an element. That's
it! 
</p>
        <p>
The important thing to note here is the "inline-block" value for a display property.
It is a CSS 2.1 introduced value and browser support may vary. After analyzing the <a href="http://www.quirksmode.org/css/display.html">display
declaration article on quirksmode</a>, I've came to a conclusion that it should be
safe to use this technique. Internet explorer "supports" it. Firefox "supports"
it. Opera supports it. 
</p>
        <p>
Yet again. ASP.NET stands in the way... If you try to use a Display="Dynamic" property,
you will find that the proposed solution does not work with FireFox. After a close
analysis I came to a conclusion that it works with IE only by chance. The problem
is that the validation scripts used by Validation Controls set elements display property
to "inline" in case Display property was set to Dynamic on the server control. Internet
Explorer somehow manages to use the value from the style which is "inline-block" and
so it works. FireFox uses the "inline" value and so we have an &lt;span&gt; element
with no content - an invisible element.
</p>
        <p>
I have currently few ideas of how to overcome this problem. One way it could be done
is to put an absolutely positioned Validation Control inside a relatively
positioned &lt;div&gt; element like this. Remove the Display="dynamic" attribute and
mark the containing div with a class="error" and change the styles as follow:
</p>
        <p>
div.error<br />
{<br />
  position: relative;<br />
  display: inline;<br />
}<br />
span.error<br />
{<br />
  position: absolute;<br />
  left: 0; top 0;<br />
}
</p>
        <p>
The positioning of elements needs some work, but hey at least it works!
</p>
        <p>
Complete code here: <a href="http://vaultofthoughts.net/content/binary/ValidationControlsAsImages.txt">ValidationControlsAsImages.txt
(.54 KB)</a></p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/ValidationControlsAsImagesTake2.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/ValidationControlsAsImagesTake2.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=d2fdeed1-d47a-4dcd-a212-64b93c3bcd3d" />
      </body>
      <title>Validation Controls As Images - Take 2</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,d2fdeed1-d47a-4dcd-a212-64b93c3bcd3d.aspx</guid>
      <link>http://VaultOfThoughts.net/ValidationControlsAsImagesTake2.aspx</link>
      <pubDate>Tue, 20 Feb 2007 08:57:25 GMT</pubDate>
      <description>&lt;p&gt;
After a while of thinking and &lt;a href="validationcontrolsvscontroladapters.aspx"&gt;struggling
with Control Adapters&lt;/a&gt; I was unable to find a good solution to get my Validation
Controls render as images. I have came to the following conclusions:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Control Adapters will not help 
&lt;li&gt;
Overriding each Validator is not a good solution 
&lt;li&gt;
Creating my own Validation Controls is not a good solution&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Given the above, I have once again looked at everything I have learned. I have looked
at the html code rendered by the controls and then... solution came to me! It turns
out that we don't need ASP.NET to get things working. ASP.NET stands in our way when
it comes to handling this task, so why not use a plain old CSS? So I did.
&lt;/p&gt;
&lt;p&gt;
When Validaton Control:
&lt;/p&gt;
&lt;p&gt;
&amp;lt;asp:CompareValidator runat="server" ID="V" ControlToValidate="c1" ControlToCompare="c2"
Text="Error Text" ToolTip="Title Text"&amp;gt;&amp;lt;/asp:CompareValidator&amp;gt;
&lt;/p&gt;
&lt;p&gt;
is rendered on a page it looks more or less like this:
&lt;/p&gt;
&lt;p&gt;
&amp;lt;span id="ctl00_V" title="Title Text" style="color:Red;visibility:hidden;"&amp;gt;Error
Text&amp;lt;/span&amp;gt;
&lt;/p&gt;
&lt;p&gt;
To make a nice image out of it, we need few things. First we need one tradeoff: we
have to remove the Text attribute, otherwise it will mess the looks a bit. Next&amp;nbsp;we
decorate the server control with a CssClass attribute with a value such as "error".
Next we create a style:
&lt;/p&gt;
&lt;p&gt;
span.error&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; display: -moz-inline-stack;&lt;br&gt;
&amp;nbsp; display: inline-block;&lt;br&gt;
&amp;nbsp; background-image: url(error.gif);&lt;br&gt;
&amp;nbsp; background-repeat: no-repeat;&lt;br&gt;
&amp;nbsp; height: 16px;&lt;br&gt;
&amp;nbsp; width: 16px;&lt;br&gt;
}
&lt;/p&gt;
&lt;p&gt;
Notice in the style few things. First, we need the &amp;lt;span&amp;gt; element to be "inline-block".
That is because we need it to have a height and width. Furthermore, FireFox does not
support "inline-block" (!!!) but we&amp;nbsp;can use a&amp;nbsp;simple workaround with "-moz-inline-stack"
value as described by Nicholas in &lt;a href="http://www.nczonline.net/archive/2006/10/382"&gt;Pain
with inline-block article&lt;/a&gt; (also see the comments).&amp;nbsp;By default span (inline
element) will only be as large as the text it contain.&amp;nbsp;Since in the proposed
solution, there will be no text in the &amp;lt;span&amp;gt; this is needed. Next, we set the
background-image to a desired url and set the width and height of an element. That's
it! 
&lt;p&gt;
The important thing to note here is the "inline-block" value for a display property.
It is a CSS 2.1 introduced value and browser support may vary. After analyzing the &lt;a href="http://www.quirksmode.org/css/display.html"&gt;display
declaration article on quirksmode&lt;/a&gt;, I've came to a conclusion that it should be
safe to use this technique.&amp;nbsp;Internet explorer "supports" it. Firefox "supports"
it. Opera supports it. 
&lt;/p&gt;
&lt;p&gt;
Yet again. ASP.NET stands in the way... If you try to use a Display="Dynamic" property,
you will find that the proposed solution does not work with FireFox. After a close
analysis I came to a conclusion that it works with IE only by chance. The problem
is that the validation scripts used by Validation Controls set elements display property
to "inline" in case Display property was set to Dynamic on the server control. Internet
Explorer somehow manages to use the value from the style which is "inline-block" and
so it works. FireFox uses the "inline" value and so we have an &amp;lt;span&amp;gt; element
with no content - an invisible element.
&lt;/p&gt;
&lt;p&gt;
I have currently few ideas of how to overcome this problem. One way it could be done
is to put&amp;nbsp;an absolutely positioned&amp;nbsp;Validation Control inside a relatively
positioned &amp;lt;div&amp;gt; element like this. Remove the Display="dynamic" attribute and
mark the containing div with a class="error" and change the styles as follow:
&lt;/p&gt;
&lt;p&gt;
div.error&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; position: relative;&lt;br&gt;
&amp;nbsp; display: inline;&lt;br&gt;
}&lt;br&gt;
span.error&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; position: absolute;&lt;br&gt;
&amp;nbsp; left: 0; top 0;&lt;br&gt;
}
&lt;/p&gt;
&lt;p&gt;
The positioning of elements needs some work, but hey at least it works!
&lt;/p&gt;
&lt;p&gt;
Complete code here: &lt;a href="http://vaultofthoughts.net/content/binary/ValidationControlsAsImages.txt"&gt;ValidationControlsAsImages.txt
(.54 KB)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/ValidationControlsAsImagesTake2.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/ValidationControlsAsImagesTake2.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=d2fdeed1-d47a-4dcd-a212-64b93c3bcd3d" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=61bb48e5-9411-4858-ab2d-30d7bd2690c7</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,61bb48e5-9411-4858-ab2d-30d7bd2690c7.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Once again I have gone through the process of customizing the way the ValidationControls
are rendered in particular I need them to render an image. I have described a <a href="validationcontrolsasimagesworkaround.aspx">workaround
of how to get them to render as images</a>. The problem is that you have to tweak
each validator that you want to use. In the same workaround, I have described, why
it is not possible to use use Control Adapters to achieve the goal - the really important
part that makes validation controls work is hidden in the Render method which is not
called by the WebControlAdapter. Today I have learned that Render is called by the
ControlAdapter class which is a base class for a WebControlAdapter. If we forget recommendation
to use a WebControlAdapter for controls that inherit from WebControl we are almost
there.
</p>
        <p>
Almost... It makes a big difference. It turns out, that you cannot customize the element
name if you use the ControlAdapter so you cannot change the default &lt;span&gt; element
to an &lt;img&gt; element, but at least you can still get the Render method of BaseValidator
to be called.
</p>
        <p>
What is my opinion on the whole Control Adapters feature of ASP.NET 2.0? The idea
was great. This is only a step in a direction where we should have begin and not end.
I mean: why the a control needs to know how to render itself? Differently on different
browsers. Differently, depending on the configuration, etc. Adapters make it all right.
No longer controls have more then one responsibility. But that is only a try. Nice,
but still only a try. The example of Validation Controls shows that once something
is done wrong, it is very hard to make it right again. The same holds true for most
of the existing controls. Just take a look at he amount of code in the CSS Control
Adapters.
</p>
        <p>
I only hope that in v.next some of the issues will be fixed. I know unfortunately,
that it can only be a hope. There is a lots of code that would have to be rewritten
and I don't suppose Microsoft will do it anytime soon :-(
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=61bb48e5-9411-4858-ab2d-30d7bd2690c7" />
      </body>
      <title>Validation Controls Vs. Control Adapters</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,61bb48e5-9411-4858-ab2d-30d7bd2690c7.aspx</guid>
      <link>http://VaultOfThoughts.net/ValidationControlsVsControlAdapters.aspx</link>
      <pubDate>Mon, 19 Feb 2007 12:45:43 GMT</pubDate>
      <description>&lt;p&gt;
Once again I have gone through the process of customizing the way the ValidationControls
are rendered in particular I need them to render an image. I have described a &lt;a href="validationcontrolsasimagesworkaround.aspx"&gt;workaround
of how to get them to render as images&lt;/a&gt;. The problem is that you have to tweak
each validator that you want to use. In the same workaround, I have described, why
it is not possible to use use Control Adapters to achieve the goal - the really important
part that makes validation controls work is hidden in the Render method which is not
called by the WebControlAdapter. Today I have learned that Render is called by the
ControlAdapter class which is a base class for a WebControlAdapter. If we forget recommendation
to use a WebControlAdapter for controls that inherit from WebControl we are almost
there.
&lt;/p&gt;
&lt;p&gt;
Almost... It makes a big difference. It turns out, that you cannot customize the element
name if you use the ControlAdapter so you cannot change the default &amp;lt;span&amp;gt; element
to an &amp;lt;img&amp;gt; element, but at least you can still get the Render method of BaseValidator
to be called.
&lt;/p&gt;
&lt;p&gt;
What is my opinion on the whole Control Adapters feature of ASP.NET 2.0? The idea
was great. This is only a step in a direction where we should have begin and not end.
I mean: why the a control needs to know how to render itself? Differently on different
browsers. Differently, depending on the configuration, etc. Adapters make it all right.
No longer controls have more then one responsibility. But that is only a try. Nice,
but still only a try. The example of Validation Controls shows that once something
is done wrong, it is very hard to make it right again. The same holds true for most
of the existing controls. Just take a look at he amount of code in the CSS Control
Adapters.
&lt;/p&gt;
&lt;p&gt;
I only hope that in v.next some of the issues will be fixed. I know unfortunately,
that it can only be a hope. There is a lots of code that would have to be rewritten
and I don't suppose Microsoft will do it anytime soon :-(
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=61bb48e5-9411-4858-ab2d-30d7bd2690c7" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=6c1606ec-1e00-4b28-b83a-684f3ad6037b</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,6c1606ec-1e00-4b28-b83a-684f3ad6037b.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
        </p>
        <p>
There are couple of folders that have a special meaning in ASP.NET. Those folders
include folders like App_Code where you usually put your code or App_Themes where
you put themes. There is also an App_Data folder which "Contains application data
files including MDF files, XML files, as well as other data store files" (MSDN). 
</p>
        <p>
From App_Data folder description it is obvious that it is where you should put your
data. But how to do it? I have searched high and low for a methods or properties that
help interact with this directory. The reason is because I don't like hard-coding
strings in my code and I would have to hard-code at least one: "~/App_Data". I thought
that since there are method to <a href="http://www.madskristensen.dk/blog/Let+Windows+Place+Your+Temp+Files.aspx">get
the location of temp directory</a>: 
</p>
        <pre>System.IO.Path.GetTempPath(); </pre>
        <p>
Or even to get the system direcotry: 
</p>
        <p>
          <em>
          </em>
        </p>
        <pre>Environment.SystemDirectory </pre>
        <p>
There should be a way to get the App_Data directory. 
</p>
        <p>
Apparently I was wrong. Unfortunately. There is no Strongly Typed method or property
to get to the location of App_Data directory! 
</p>
        <p>
I have found 2 places in code where "App_Data" appears as a string (that's why I'm
pretty sure there are no properties or methods). One place is a GetDataDirectory
method in an internal System.Web.DataAccess.SqlConnectionHelper class and the other
is in a SetUpDataDirectory method in a System.Web.HttpRuntime class. 
</p>
        <p>
My guess is that the first one is used for Membership Provider that by default uses
Microsoft SQL Express database and App_Data directory to store the mdf files. The
other one is probably not used at all - I have found no code that calls that
method!  
</p>
        <p>
When I have looked inside those methods I have found one thing that they have in common.
Both set some data object in an AppDomain via: 
</p>
        <p>
        </p>
        <pre>AppDomain.CurrentDomain.SetData(<span style="COLOR: maroon">"DataDirectory"</span>...) </pre>
        <p>
So I have made a test and used on one of my pages: 
</p>
        <p>
        </p>
        <pre>
          <span style="COLOR: blue">string</span> dataDirectory = AppDomain.CurrentDomain.GetData(<span style="COLOR: maroon">"DataDirectory"</span>) <span style="COLOR: blue">as</span><span style="COLOR: blue">string</span>; </pre>
        <p>
And of course it worked as expected. Unfortunately we cannot (or should not) use this
kind of code since it is a dirty hack that relies on internal implementation of some
internal classes and it also requires us to hard-code a string. So what's the benefit?
The only one is that now I'm pretty sure, that there is no way to interact with App_Data
folder using BCL so I will no longer have to search for it on the Internet -
thing that I have done countless times.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/GettingTheLocationOfAppDataFolder.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/GettingTheLocationOfAppDataFolder.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=6c1606ec-1e00-4b28-b83a-684f3ad6037b" />
      </body>
      <title>Getting The Location of App_Data Folder</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,6c1606ec-1e00-4b28-b83a-684f3ad6037b.aspx</guid>
      <link>http://VaultOfThoughts.net/GettingTheLocationOfAppDataFolder.aspx</link>
      <pubDate>Sat, 03 Feb 2007 14:18:21 GMT</pubDate>
      <description>&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
There are couple of folders that have a special meaning in ASP.NET. Those folders
include folders like App_Code where you usually put your code or App_Themes where
you put themes. There is also an App_Data folder which "Contains application data
files including MDF files, XML files, as well as other data store files" (MSDN). 
&lt;p&gt;
From App_Data folder description it is obvious that it is where you should put your
data. But how to do it? I have searched high and low for a methods or properties that
help interact with this directory.&amp;nbsp;The reason is because I don't like hard-coding
strings in my code and I would have to hard-code at least one: "~/App_Data". I thought
that since there are method to &lt;a href="http://www.madskristensen.dk/blog/Let+Windows+Place+Your+Temp+Files.aspx"&gt;get
the location of temp directory&lt;/a&gt;: &lt;pre&gt;System.IO.Path.GetTempPath(); &lt;/pre&gt;
&lt;p&gt;
Or even to get the system direcotry: 
&lt;p&gt;
&lt;em&gt;&lt;/em&gt;&lt;pre&gt;Environment.SystemDirectory &lt;/pre&gt;
&lt;p&gt;
There should be a way to get the App_Data directory. 
&lt;p&gt;
Apparently I was wrong. Unfortunately. There is no Strongly Typed method or property
to get to the location of App_Data directory! 
&lt;p&gt;
I have found 2 places in code where "App_Data" appears as a string (that's why I'm
pretty sure&amp;nbsp;there&amp;nbsp;are no properties or methods). One place is a GetDataDirectory
method in an internal System.Web.DataAccess.SqlConnectionHelper class and the other
is in a SetUpDataDirectory method in a System.Web.HttpRuntime class. 
&lt;p&gt;
My guess is that the first one is used for Membership Provider that by default uses
Microsoft SQL Express database and App_Data directory to store the mdf files. The
other&amp;nbsp;one is probably not used at all - I have found no code that calls that
method!&amp;nbsp; 
&lt;p&gt;
When I have looked inside those methods I have found one thing that they have in common.
Both set some data object in an AppDomain via: 
&lt;p&gt;
&lt;pre&gt;AppDomain.CurrentDomain.SetData(&lt;span style="COLOR: maroon"&gt;"DataDirectory"&lt;/span&gt;...) &lt;/pre&gt;
&lt;p&gt;
So I have made a test and used on one of my pages: 
&lt;p&gt;
&lt;pre&gt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; dataDirectory = AppDomain.CurrentDomain.GetData(&lt;span style="COLOR: maroon"&gt;"DataDirectory"&lt;/span&gt;) &lt;span style="COLOR: blue"&gt;as&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;; &lt;/pre&gt;
&lt;p&gt;
And of course it worked as expected. Unfortunately we cannot (or should not) use this
kind of code since it is a dirty hack that relies on internal implementation of some
internal classes and it also requires us to hard-code a string. So what's the benefit?
The only one is that now I'm pretty sure, that there is no way to interact with App_Data
folder using BCL&amp;nbsp;so I will no longer have to search for it on the Internet -
thing that I have done countless times.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/GettingTheLocationOfAppDataFolder.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/GettingTheLocationOfAppDataFolder.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=6c1606ec-1e00-4b28-b83a-684f3ad6037b" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=884e3b42-90e5-4400-9d7d-22e4bca62d55</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,884e3b42-90e5-4400-9d7d-22e4bca62d55.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my last <a href="http://vaultofthoughts.net/IsBindableTypeMysteryMethod.aspx">article
on IsBindableType</a>, I have desribed, why AutoGenerateColumns does not work for
Nullable types. Now I present a solution for this problem.
</p>
        <p>
In order for your GridView, FormView, or DetailsView to autogenerate the field for
your Nullable&lt;T&gt; property you have to subclass the control you are using: say
GridView and override the IsBindableType method. In this overriden method, you have
to add a check to see if a type is Nullable. This can be done in a following way according
to <a href="http://blogs.wdevs.com/Gootvilig/archive/2006/01/04/11792.aspx">Yitzhak
Gootvilig's post</a>:
</p>
        <pre>
          <span style="COLOR: blue">public</span>
          <span style="COLOR: blue">static</span>
          <span style="COLOR: blue">bool</span> IsNullableType(Type
type) { <span style="COLOR: blue">return</span> type.IsGenericType &amp;&amp; 
<br />
      type.GetGenericTypeDefinition() == <span style="COLOR: blue">typeof</span>(Nullable&lt;&gt;);
}</pre>
        <p>
Just put this code in some kind of reusable library since I have a feeling that ASP.NET
controls will not be the last place you will need it :-)
</p>
        <p>
A full implementation of IsBindableType after the additional check will then look
more or less like this:
</p>
        <pre>
          <span style="COLOR: blue">public</span>
          <span style="COLOR: blue">override</span>
          <span style="COLOR: blue">bool</span> IsBindableType(Type
type) { <span style="COLOR: blue">return</span> NullableHelper.IsNullableType(type) 
<br />
      || <span style="COLOR: blue">base</span>.IsBindableType(type);
}</pre>
        <p>
This simple hack has to be repeated for each of the *View controls. Fortunately it
is not a common scenario where you have the AutoGenerateColumns property set to true
:-).
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/MakingGridViewAutoGenerateColumnsForNullableTypes.aspx">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/MakingGridViewAutoGenerateColumnsForNullableTypes.aspx" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=884e3b42-90e5-4400-9d7d-22e4bca62d55" />
      </body>
      <title>Making GridView Auto Generate Columns For Nullable Types</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,884e3b42-90e5-4400-9d7d-22e4bca62d55.aspx</guid>
      <link>http://VaultOfThoughts.net/MakingGridViewAutoGenerateColumnsForNullableTypes.aspx</link>
      <pubDate>Wed, 31 Jan 2007 12:50:07 GMT</pubDate>
      <description>&lt;p&gt;
In my last &lt;a href="http://vaultofthoughts.net/IsBindableTypeMysteryMethod.aspx"&gt;article
on IsBindableType&lt;/a&gt;, I have desribed, why AutoGenerateColumns does not work for
Nullable types. Now I present a solution for this problem.
&lt;/p&gt;
&lt;p&gt;
In order for your GridView, FormView, or DetailsView to autogenerate the field for
your Nullable&amp;lt;T&amp;gt; property you have to subclass the control you are using: say
GridView and override the IsBindableType method. In this overriden method, you have
to add a check to see if a type is Nullable. This can be done in a following way according
to &lt;a href="http://blogs.wdevs.com/Gootvilig/archive/2006/01/04/11792.aspx"&gt;Yitzhak
Gootvilig's post&lt;/a&gt;:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; IsNullableType(Type
type) { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; type.IsGenericType &amp;amp;&amp;amp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;type.GetGenericTypeDefinition() == &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(Nullable&amp;lt;&amp;gt;);
}&lt;/pre&gt;
&lt;p&gt;
Just put this code in some kind of reusable library since I have a feeling that ASP.NET
controls will not be the last place you will need it :-)
&lt;/p&gt;
&lt;p&gt;
A full implementation of IsBindableType after the additional check will then look
more or less like this:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; IsBindableType(Type
type) { &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; NullableHelper.IsNullableType(type) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|| &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.IsBindableType(type);
}&lt;/pre&gt;
&lt;p&gt;
This simple hack has to be repeated for each of the *View controls. Fortunately it
is not a common scenario where you have the AutoGenerateColumns property set to true
:-).
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/MakingGridViewAutoGenerateColumnsForNullableTypes.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/MakingGridViewAutoGenerateColumnsForNullableTypes.aspx" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=884e3b42-90e5-4400-9d7d-22e4bca62d55" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=485558ee-e223-4e2b-9f5f-74496016675f</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,485558ee-e223-4e2b-9f5f-74496016675f.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I have encountered a strange problem with GridView's AutoGenerateColumns property.
The problem was that not all columns were generated automatically. It would not be
a surprise if it happened for complex types such as my own, but I have noticed that
simple (in my opinion) types such as Nullable&lt;Guid&gt; were also not generated!
So I went investigating.
</p>
        <p>
After a while of hacking and slashing through the GridView's code I have found the
reason. It appears that columns are only auto-generated for properties whose type
is a bindable type. What does that mean? Who decides if a type is bindable or not?
I have continued my investigation and...
</p>
        <p>
After a while I have found that there is a method on a GridView control called IsBindableType
which looks like this:
</p>
        <pre>
          <span style="COLOR: blue">public</span>
          <span style="COLOR: blue">virtual</span>
          <span style="COLOR: blue">bool</span> IsBindableType(Type
type) { <span style="COLOR: blue">if</span> ((!type.IsPrimitive &amp;&amp; (type != <span style="COLOR: blue">typeof</span>(<span style="COLOR: blue">string</span>)))
&amp;&amp; 
<br />
      ((type != <span style="COLOR: blue">typeof</span>(DateTime))
&amp;&amp; (type != <span style="COLOR: blue">typeof</span>(<span style="COLOR: blue">decimal</span>))))
{ <span style="COLOR: blue">return</span> (type == <span style="COLOR: blue">typeof</span>(Guid));
} <span style="COLOR: blue">return</span><span style="COLOR: maroon">true</span>;
}</pre>
        <p>
Apparently this method decides that Nullable&lt;Guid&gt; (or Nullable&lt;int&gt; for
that matter) is not a bindable type! Because of this, you will not see properties
of such a type automatically rendered in a GridView!
</p>
        <p>
Being curious I have asked myself: why such a method is defined on a GridView control?
Is it not a more general issue to decide if a type is bindable or not? So I went investigating
even further.
</p>
        <p>
I have found that there are 5 IsBindableType methods defined in System.Web.UI.WebControls
namespace:
</p>
        <ul>
          <li>
AdRotator 
</li>
          <li>
BaseDataList 
</li>
          <li>
DetailsView 
</li>
          <li>
FormView 
</li>
          <li>
GridView</li>
        </ul>
        <p>
Of those 5, DetailsView, FormView and GridView have exactly the same implementation.
BaseDataList has slightly different:
</p>
        <pre>
          <span style="COLOR: blue">public</span>
          <span style="COLOR: blue">
            <strong>static</strong>
          </span>
          <span style="COLOR: blue">bool</span> IsBindableType(Type
type) { <span style="COLOR: blue">if</span> ((!type.IsPrimitive &amp;&amp; (type != <span style="COLOR: blue">typeof</span>(<span style="COLOR: blue">string</span>))) 
<br />
      &amp;&amp; (type != <span style="COLOR: blue">typeof</span>(DateTime)))
{ <span style="COLOR: blue">return</span> (type == <span style="COLOR: blue">typeof</span>(<span style="COLOR: blue">decimal</span>));
} <span style="COLOR: blue">return</span><span style="COLOR: maroon">true</span>;
}</pre>
        <p>
Notice the <strong>static</strong> modifier! AdRotator on the other hand has exactly
the same implementation as the BaseDataList but here the method is an instance method
and is private!
</p>
        <p>
So, we have basically 5 places in code, each of which implements the same logic, some
do it differently and none does it correctly :-(.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/IsBindableTypeMysteryMethod.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/IsBindableTypeMysteryMethod.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=485558ee-e223-4e2b-9f5f-74496016675f" />
      </body>
      <title>IsBindableType - Mystery Method</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,485558ee-e223-4e2b-9f5f-74496016675f.aspx</guid>
      <link>http://VaultOfThoughts.net/IsBindableTypeMysteryMethod.aspx</link>
      <pubDate>Wed, 31 Jan 2007 12:28:45 GMT</pubDate>
      <description>&lt;p&gt;
Today I have encountered a strange problem with GridView's AutoGenerateColumns property.
The problem was that not all columns were generated automatically. It would not be
a surprise if it happened for complex types such as my own, but I have noticed that
simple (in my opinion) types such as Nullable&amp;lt;Guid&amp;gt; were also not generated!
So I went investigating.
&lt;/p&gt;
&lt;p&gt;
After a while of hacking and slashing through the GridView's code I have found the
reason. It appears that columns are only auto-generated for properties whose type
is a bindable type. What does that mean? Who decides if a type is bindable or not?
I have continued my investigation and...
&lt;/p&gt;
&lt;p&gt;
After a while I have found that there is a method on a GridView control called IsBindableType
which looks like this:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;virtual&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; IsBindableType(Type
type) { &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; ((!type.IsPrimitive &amp;amp;&amp;amp; (type != &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;)))
&amp;amp;&amp;amp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;((type != &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(DateTime))
&amp;amp;&amp;amp; (type != &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;decimal&lt;/span&gt;))))
{ &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; (type == &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(Guid));
} &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: maroon"&gt;true&lt;/span&gt;;
}&lt;/pre&gt;
&lt;p&gt;
Apparently this method decides that Nullable&amp;lt;Guid&amp;gt; (or Nullable&amp;lt;int&amp;gt; for
that matter) is not a bindable type! Because of this, you will not see properties
of such a type automatically rendered in a GridView!
&lt;/p&gt;
&lt;p&gt;
Being curious I have asked myself: why such a method is defined on a GridView control?
Is it not a more general issue to decide if a type is bindable or not? So I went investigating
even further.
&lt;/p&gt;
&lt;p&gt;
I have found that there are 5 IsBindableType methods defined in System.Web.UI.WebControls
namespace:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
AdRotator 
&lt;li&gt;
BaseDataList 
&lt;li&gt;
DetailsView 
&lt;li&gt;
FormView 
&lt;li&gt;
GridView&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Of those 5, DetailsView, FormView and GridView have exactly the same implementation.
BaseDataList has slightly different:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;&lt;strong&gt;static&lt;/strong&gt;&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; IsBindableType(Type
type) { &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; ((!type.IsPrimitive &amp;amp;&amp;amp; (type != &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;))) 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;amp;&amp;amp; (type != &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(DateTime)))
{ &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; (type == &lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;decimal&lt;/span&gt;));
} &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: maroon"&gt;true&lt;/span&gt;;
}&lt;/pre&gt;
&lt;p&gt;
Notice the &lt;strong&gt;static&lt;/strong&gt; modifier! AdRotator on the other hand has exactly
the same implementation as the BaseDataList but here the method is an instance method
and is private!
&lt;/p&gt;
&lt;p&gt;
So, we have basically 5 places in code, each of which implements the same logic, some
do it differently and none does it correctly :-(.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/IsBindableTypeMysteryMethod.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/IsBindableTypeMysteryMethod.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=485558ee-e223-4e2b-9f5f-74496016675f" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=2c1f6c8a-ce5a-47ea-97c9-84b7edf490f0</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,2c1f6c8a-ce5a-47ea-97c9-84b7edf490f0.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In most of the ASP.NET projects I have been working on, I have needed a way to easily
navigate to any page in the solution. To accomplish this I usually created some kind
of User Control with links to all pages. If the project was small I usually entered
the links by hand. For bigger projects I have used a Repeater control and filled it
with data from the file system.<br />
The method used to get the data from the file system looks more or less like this:
</p>
        <pre>
          <span style="COLOR: blue">public</span>
          <span style="COLOR: blue">string</span>[]
GetAllPages() { <span style="COLOR: blue">int</span> applicationFolderNumberOfCharacters
= HttpRuntime.AppDomainAppPath.Length; List&lt;<span style="COLOR: blue">string</span>&gt;
pages = <span style="COLOR: blue">new</span> List&lt;<span style="COLOR: blue">string</span>&gt;(); <span style="COLOR: blue">string</span>[]
files = Directory.GetFiles(<br />
      HttpRuntime.AppDomainAppPath, <span style="COLOR: maroon">"*.aspx"</span>,
SearchOption.AllDirectories); <span style="COLOR: blue">return</span> Array.ConvertAll&lt;<span style="COLOR: blue">string</span>, <span style="COLOR: blue">string</span>&gt;(files, <span style="COLOR: blue">delegate</span>(<span style="COLOR: blue">string</span> file)
{ <span style="COLOR: blue">return</span><span style="COLOR: maroon">"~/"</span> + 
<br />
         file.Substring(applicationFolderNumberOfCharacters).Replace(<span style="COLOR: maroon">@"\"</span>, <span style="COLOR: maroon">"/"</span>);
}); }</pre>
        <p>
On the Page or User Control the Repeater I use looks as follows:
</p>
        <font color="#0000ff" size="2">
          <p>
&lt;
</p>
        </font>
        <font color="#a31515" size="2">asp</font>
        <font color="#0000ff" size="2">:</font>
        <font color="#a31515" size="2">Repeater</font>
        <font size="2">
        </font>
        <font color="#ff0000" size="2">ID</font>
        <font color="#0000ff" size="2">="Links"</font>
        <font size="2">
        </font>
        <font color="#ff0000" size="2">runat</font>
        <font color="#0000ff" size="2">="server"&gt;<br />
   </font>
        <font color="#0000ff" size="2">&lt;</font>
        <font color="#a31515" size="2">ItemTemplate</font>
        <font color="#0000ff" size="2">&gt;<br />
      </font>
        <font color="#0000ff" size="2">&lt;</font>
        <font color="#a31515" size="2">a</font>
        <font size="2">
        </font>
        <font color="#ff0000" size="2">runat</font>
        <font color="#0000ff" size="2">="server"</font>
        <font size="2">
        </font>
        <font color="#ff0000" size="2">href</font>
        <font color="#0000ff" size="2">='</font>
        <font size="2">&lt;%#
Container.DataItem %&gt;</font>
        <font color="#0000ff" size="2">'&gt;</font>
        <font size="2">&lt;%</font>
        <font color="#0000ff" size="2">#</font>
        <font size="2"> Container.DataItem%&gt;</font>
        <font color="#0000ff" size="2">&lt;/</font>
        <font color="#a31515" size="2">a</font>
        <font color="#0000ff" size="2">&gt;&lt;</font>
        <font color="#a31515" size="2">br</font>
        <font size="2">
        </font>
        <font color="#0000ff" size="2">/&gt;<br />
   </font>
        <font color="#0000ff" size="2">&lt;/</font>
        <font color="#a31515" size="2">ItemTemplate</font>
        <font color="#0000ff" size="2">&gt;<br />
&lt;/</font>
        <font color="#a31515" size="2">asp</font>
        <font color="#0000ff" size="2">:</font>
        <font color="#a31515" size="2">Repeater</font>
        <font color="#0000ff" size="2">&gt;
</font>
        <p>
If you put it on the Master Page, you will get access to every page from every page,
which is great during development. Just don't forget to remove it from the production
code.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/GettingTheListOfAllPagesInTheApplication.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/GettingTheListOfAllPagesInTheApplication.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=2c1f6c8a-ce5a-47ea-97c9-84b7edf490f0" />
      </body>
      <title>Getting The List Of All Pages In The Application</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,2c1f6c8a-ce5a-47ea-97c9-84b7edf490f0.aspx</guid>
      <link>http://VaultOfThoughts.net/GettingTheListOfAllPagesInTheApplication.aspx</link>
      <pubDate>Tue, 16 Jan 2007 18:38:57 GMT</pubDate>
      <description>&lt;p&gt;
In most of the ASP.NET projects I have been working on, I have needed a way to easily
navigate to any page in the solution. To accomplish this I usually created some kind
of User Control with links to all pages. If the project was small I usually entered
the links by hand. For bigger projects I have used a Repeater control and filled it
with data from the file system.&lt;br&gt;
The method used to get the data from the file system looks more or less like this:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[]
GetAllPages() { &lt;span style="COLOR: blue"&gt;int&lt;/span&gt; applicationFolderNumberOfCharacters
= HttpRuntime.AppDomainAppPath.Length; List&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&amp;gt;
pages = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&amp;gt;(); &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[]
files = Directory.GetFiles(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;HttpRuntime.AppDomainAppPath, &lt;span style="COLOR: maroon"&gt;"*.aspx"&lt;/span&gt;,
SearchOption.AllDirectories); &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; Array.ConvertAll&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&amp;gt;(files, &lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; file)
{ &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: maroon"&gt;"~/"&lt;/span&gt; + 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;file.Substring(applicationFolderNumberOfCharacters).Replace(&lt;span style="COLOR: maroon"&gt;@"\"&lt;/span&gt;, &lt;span style="COLOR: maroon"&gt;"/"&lt;/span&gt;);
}); }&lt;/pre&gt;
&lt;p&gt;
On the Page or User Control the Repeater I use looks as follows:
&lt;/p&gt;
&lt;font color=#0000ff size=2&gt; 
&lt;p&gt;
&amp;lt;
&lt;/font&gt;&lt;font color=#a31515 size=2&gt;asp&lt;/font&gt;&lt;font color=#0000ff size=2&gt;:&lt;/font&gt;&lt;font color=#a31515 size=2&gt;Repeater&lt;/font&gt;&lt;font size=2&gt; &lt;/font&gt;&lt;font color=#ff0000 size=2&gt;ID&lt;/font&gt;&lt;font color=#0000ff size=2&gt;="Links"&lt;/font&gt;&lt;font size=2&gt; &lt;/font&gt;&lt;font color=#ff0000 size=2&gt;runat&lt;/font&gt;&lt;font color=#0000ff size=2&gt;="server"&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color=#0000ff size=2&gt;&amp;lt;&lt;/font&gt;&lt;font color=#a31515 size=2&gt;ItemTemplate&lt;/font&gt;&lt;font color=#0000ff size=2&gt;&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color=#0000ff size=2&gt;&amp;lt;&lt;/font&gt;&lt;font color=#a31515 size=2&gt;a&lt;/font&gt;&lt;font size=2&gt; &lt;/font&gt;&lt;font color=#ff0000 size=2&gt;runat&lt;/font&gt;&lt;font color=#0000ff size=2&gt;="server"&lt;/font&gt;&lt;font size=2&gt; &lt;/font&gt;&lt;font color=#ff0000 size=2&gt;href&lt;/font&gt;&lt;font color=#0000ff size=2&gt;='&lt;/font&gt;&lt;font size=2&gt;&amp;lt;%#
Container.DataItem %&amp;gt;&lt;/font&gt;&lt;font color=#0000ff size=2&gt;'&amp;gt;&lt;/font&gt;&lt;font size=2&gt;&amp;lt;%&lt;/font&gt;&lt;font color=#0000ff size=2&gt;#&lt;/font&gt;&lt;font size=2&gt; Container.DataItem%&amp;gt;&lt;/font&gt;&lt;font color=#0000ff size=2&gt;&amp;lt;/&lt;/font&gt;&lt;font color=#a31515 size=2&gt;a&lt;/font&gt;&lt;font color=#0000ff size=2&gt;&amp;gt;&amp;lt;&lt;/font&gt;&lt;font color=#a31515 size=2&gt;br&lt;/font&gt;&lt;font size=2&gt; &lt;/font&gt;&lt;font color=#0000ff size=2&gt;/&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color=#0000ff size=2&gt;&amp;lt;/&lt;/font&gt;&lt;font color=#a31515 size=2&gt;ItemTemplate&lt;/font&gt;&lt;font color=#0000ff size=2&gt;&amp;gt;&lt;br&gt;
&amp;lt;/&lt;/font&gt;&lt;font color=#a31515 size=2&gt;asp&lt;/font&gt;&lt;font color=#0000ff size=2&gt;:&lt;/font&gt;&lt;font color=#a31515 size=2&gt;Repeater&lt;/font&gt;&lt;font color=#0000ff size=2&gt;&amp;gt;&gt;
&lt;/font&gt; 
&lt;p&gt;
If you put it on the Master Page, you will get access to every page from every page,
which is great during development. Just don't forget to remove it from the production
code.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/GettingTheListOfAllPagesInTheApplication.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/GettingTheListOfAllPagesInTheApplication.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=2c1f6c8a-ce5a-47ea-97c9-84b7edf490f0" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=1194f88a-b6d4-414a-a5d8-4dc457ed2b1d</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,1194f88a-b6d4-414a-a5d8-4dc457ed2b1d.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today <a href="http://www.madskristensen.dk/blog/">Mads</a> shows us a <a href="http://www.madskristensen.dk/blog/The+Class+You+Didnt+Know+Existed+.aspx">not
well known class that is called a Triplet</a>. He also asks a question: how you would
use it. Fortunately I have seen this class before so here is what I have learned:
</p>
        <p>
Triplet is used in the same way as a Pair class - mostly in the LoadViewState
and SaveViewState methods. Generally if you are saving view state in your control
you not only have to store your data, but also the data returned by the call to the
base.SaveViewState method. This is where the Pair class or the Triplet come in handy
since given two (three) fields, one is used for the base control's state and
the other can be used by your state. Similarly, when loading the state in the LoadViewState
method you get your own state from one of the fields, and pass the other to the base
class'es LoadViewState method. You have to remember to pass the same field you have
used in the SaveViewState method or the things will go nasty ;-). 
</p>
        <p>
Triplet is used when Pair is not enough. If Triplet is not enough, you have to create
your own class.
</p>
        <p>
I hope this sheds some light on the topic.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=1194f88a-b6d4-414a-a5d8-4dc457ed2b1d" />
      </body>
      <title>What is a Pair and Triplet Class?</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,1194f88a-b6d4-414a-a5d8-4dc457ed2b1d.aspx</guid>
      <link>http://VaultOfThoughts.net/WhatIsAPairAndTripletClass.aspx</link>
      <pubDate>Tue, 16 Jan 2007 17:55:47 GMT</pubDate>
      <description>&lt;p&gt;
Today &lt;a href="http://www.madskristensen.dk/blog/"&gt;Mads&lt;/a&gt; shows us a &lt;a href="http://www.madskristensen.dk/blog/The+Class+You+Didnt+Know+Existed+.aspx"&gt;not
well known class that is called a Triplet&lt;/a&gt;. He also asks a question: how you would
use it. Fortunately I have seen this class before so here is what I have learned:
&lt;/p&gt;
&lt;p&gt;
Triplet&amp;nbsp;is used in the same way as a Pair class - mostly in the LoadViewState
and SaveViewState methods. Generally if you are saving view state in your control
you not only have to store your data, but also the data returned by the call to the
base.SaveViewState method. This is where the Pair class or the Triplet come in handy
since given two (three)&amp;nbsp;fields, one is used for the base control's state and
the other can be used by your state. Similarly, when loading the state in the LoadViewState
method you get your own state from one of the fields, and pass the other to the base
class'es LoadViewState method. You have to remember to pass the same field you have
used in the SaveViewState method or the things will go nasty ;-). 
&lt;/p&gt;
&lt;p&gt;
Triplet is used when Pair is not enough. If Triplet is not enough, you have to create
your own class.
&lt;/p&gt;
&lt;p&gt;
I hope this sheds some light on the topic.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=1194f88a-b6d4-414a-a5d8-4dc457ed2b1d" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=85bff9bc-b97f-4915-a981-b6d8133952ef</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,85bff9bc-b97f-4915-a981-b6d8133952ef.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It is a common practice in ASP.NET development to use controls provided by third parties.
Such controls are usually distributed in form of an assmebly .dll files which you
put into the Bin folder of your ASP.NET application. When using Visual Studio, you
just add a reference to a file and the file is copied to that directory for you. What
you don't get are new icons on the Toolbox representing newly added controls.
You have to have to go to the Toolbox, select Choose Items... from context menu and
point to the same dll file you have just used when adding a reference. 
</p>
        <p>
A much easier way to accomplish the same thing is to just do this second step though.
By adding items to a Toolbox, Visual Studio automatically copies the required files
into the Bin folder and by doing so, creates a reference to it so you don't
have to.
</p>
        <p>
When adding adding items to the Toolbox I usually prefer to create a separate Tab
and add items there. For each group of controls I create a new Tab. The reason for
this is that it is much easier to remove a whole Tab from the Toolbox than each individual
item at a time. (You can also use the Choose Items... window to remove unneeded items,
but I have found removing the Tab a lot easier and faster).
</p>
        <p>
The third and in my opinion the best way of adding third party controls to a project
is to create a separate project in the solution. In this project, for each control
you want in your Toolbox, create a class and inherit it from this control. I have
found that Visual Studio 2005 automatically adds to the Toolbox, controls that it
finds in the Solution. An advantage of such an approach (other than that it is generally
a good idea to always do so) is that now you can go and reset the Toolbox and your
controls will still be there - after Visual Studio decides it is time :-) which usually
happens when switching from html/aspx view do design view.<br /></p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/MakingYourCustomASPNETControlsAppearInTheToolbox.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/MakingYourCustomASPNETControlsAppearInTheToolbox.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=85bff9bc-b97f-4915-a981-b6d8133952ef" />
      </body>
      <title>Making Your Custom ASP.NET Controls Appear In The Toolbox</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,85bff9bc-b97f-4915-a981-b6d8133952ef.aspx</guid>
      <link>http://VaultOfThoughts.net/MakingYourCustomASPNETControlsAppearInTheToolbox.aspx</link>
      <pubDate>Thu, 11 Jan 2007 22:58:42 GMT</pubDate>
      <description>&lt;p&gt;
It is a common practice in ASP.NET development to use controls provided by third parties.
Such controls are usually distributed in form of an assmebly .dll files which you
put into the Bin folder of your ASP.NET application. When using Visual Studio, you
just add a reference to a file and the file is copied to that directory for you. What
you don't get&amp;nbsp;are new icons on the Toolbox representing newly added controls.
You have to have to go to the Toolbox, select Choose Items... from context menu and
point to the same dll file you have just used when adding a reference. 
&lt;/p&gt;
&lt;p&gt;
A much easier way to accomplish the same thing is to just do this second step though.
By adding items to a Toolbox, Visual Studio automatically copies the required files
into the Bin folder&amp;nbsp;and by doing so, creates a reference to it&amp;nbsp;so you don't
have to.
&lt;/p&gt;
&lt;p&gt;
When adding adding items to the Toolbox&amp;nbsp;I usually prefer to create&amp;nbsp;a separate&amp;nbsp;Tab
and add items there. For each group of controls I create a new Tab. The reason for
this is that it is much easier to remove a whole Tab from the Toolbox than each individual
item at a time. (You can also use the Choose Items... window to remove unneeded items,
but I have found removing the Tab a lot easier and faster).
&lt;/p&gt;
&lt;p&gt;
The third and in my opinion the best way of adding third party controls to a project
is to create a separate project in the solution. In this project, for each control
you want in your Toolbox, create a class and inherit it from this control. I have
found that Visual Studio 2005 automatically adds to the Toolbox, controls that it
finds in the Solution. An advantage of such an approach (other than that it is generally
a good idea to always do so) is that now you can go and reset the Toolbox and your
controls will still be there - after Visual Studio decides it is time :-) which usually
happens when switching from html/aspx view do design view.&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/MakingYourCustomASPNETControlsAppearInTheToolbox.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/MakingYourCustomASPNETControlsAppearInTheToolbox.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=85bff9bc-b97f-4915-a981-b6d8133952ef" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=b5955828-5c9c-4b0d-ab45-6e9d48b08263</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,b5955828-5c9c-4b0d-ab45-6e9d48b08263.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The standard procedure when working with ASP.NET Web Projects looks more or less like
this: 
</p>
        <p>
1) Open/Create a solution in Visual Studio<br />
2) Add/Change some of the pages/code in the solution<br />
3) Press F5 to build and run the project to see the effect of the changes 
</p>
        <p>
This procedure has its pros and cons. The main advantage is that we get a debugger
support from the IDE, so in case of an exception, we have the possibility to see what
and where went wrong. We can also step through the code and see what is being done
step by step. Those are only the few of many good things the Visual Studio gives you.  
</p>
        <p>
So what are the drawbacks of this standard procedure? Take for example a page which
requires you to login before you can use it. Using the Visual Studio to run the browser
you have to login every time you start the debug session. You can somehow work around
this problem by creating a persistent cookie (remember me checkbox) so that you will
not have to login every time. But what if you have to login using different user name
and password? Providing logout button returns you to the original problem. The bigger
problem is however with multi-step, wizard-like operations. If you have a 5 steps
in your Wizard and something goes wrong on the last step, you have to close the browser
to stop debugging (ASP.NET does not support the edit and continue feature with the
standard Web Project). Then after making the required changes you have to start the
wizard all over again. 
<br />
Keep in mind that those are only simple examples to illustrate the problems and benefits
of the standard approach. They are in no way complete. 
</p>
        <p>
Now, We know that there are problems and there are benefits of the "standard way"
but there is another way that I'm sure most ASP.NET developers coming from backgrounds
such as PHP know about and use a lot. This solution was the only possible way to test
and debug your web applications if you had no debugger support from your IDE - like
was the case few years ago and I'm not sure if it is still the case. Basically what
you do is run your own browser and navigate to the page that Visual Studio would otherwise
navigate to using the browser it has started. Now you can browse through the pages
and test whatever you want and in case something goes wrong, you can just make the
changes in the Visual Studio and recompile. It is possible since the IDE is not in
the debugging state while you do this. (The scope of the changes may cause the running
application to reload causing you to relogin, but this will not always be the case) 
</p>
        <p>
What we don't get with the alternative way of working with our pages is the debugger
support, so now if something goes wrong we will get the error page instead of the
break in the debugger. Fortunately you can always attach Visual Studio to a running
process hosting ASP.NET and debug the application as usual. After you have found the
error you can safely stop the debugger and you browse window will not be closed. Also
you will not suffer from the startup time of required fro Visual Studio to start the
browser and the debugger. This way instead of the edit and continue you can work with
a save and refresh style. 
</p>
        <p>
I'm not saying that the second approach is better or worse. I'm only pointing out
that in some cases there is a faster way to do things, especially when you are getting
irritated by the Internet Explorer's address bar always remembering the wrong addresses
:-).
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/TipForWorkingWithASPNETWebProjects.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/TipForWorkingWithASPNETWebProjects.aspx" border="0" alt="kick it on DotNetKicks.com" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b5955828-5c9c-4b0d-ab45-6e9d48b08263" />
      </body>
      <title>Tip For Working With ASP.NET Web Projects</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,b5955828-5c9c-4b0d-ab45-6e9d48b08263.aspx</guid>
      <link>http://VaultOfThoughts.net/TipForWorkingWithASPNETWebProjects.aspx</link>
      <pubDate>Thu, 09 Nov 2006 07:56:04 GMT</pubDate>
      <description>&lt;p&gt;
The standard procedure when working with ASP.NET Web Projects looks more or less like
this: 
&lt;p&gt;
1) Open/Create a solution in Visual Studio&lt;br&gt;
2) Add/Change some of the pages/code in the solution&lt;br&gt;
3) Press F5 to build and run the project to see the effect of the changes 
&lt;p&gt;
This procedure has its pros and cons. The main advantage is that we get a debugger
support from the IDE, so in case of an exception, we have the possibility to see what
and where went wrong. We can also step through the code and see what is being done
step by step. Those are only the few of many good things the Visual Studio gives you.&amp;nbsp; 
&lt;p&gt;
So what are the drawbacks of this standard procedure? Take for example a page which
requires you to login before you can use it. Using the Visual Studio to run the browser
you have to login every time you start the debug session. You can somehow work around
this problem by creating a persistent cookie (remember me checkbox) so that you will
not have to login every time. But what if you have to login using different user name
and password? Providing logout button returns you to the original problem. The bigger
problem is however with multi-step, wizard-like operations. If you have a 5 steps
in your Wizard and something goes wrong on the last step, you have to close the browser
to stop debugging (ASP.NET does not support the edit and continue feature with the
standard Web Project). Then after making the required changes you have to start the
wizard all over again. 
&lt;br&gt;
Keep in mind that those are only simple examples to illustrate the problems and benefits
of the standard approach. They are in no way complete. 
&lt;p&gt;
Now, We know that there are problems and there are benefits of the "standard way"
but there is another way that I'm sure most ASP.NET developers coming from backgrounds
such as PHP know about and use a lot. This solution was the only possible way to test
and debug your web applications if you had no debugger support from your IDE - like
was the case few years ago and I'm not sure if it is still the case. Basically what
you do is run your own browser and navigate to the page that Visual Studio would otherwise
navigate to using the browser it has started. Now you can browse through the pages
and test whatever you want and in case something goes wrong, you can just make the
changes in the Visual Studio and recompile. It is possible since the IDE is not in
the debugging state while you do this. (The scope of the changes may cause the running
application to reload causing you to relogin, but this will not always be the case) 
&lt;p&gt;
What we don't get with the alternative way of working with our pages is the debugger
support, so now if something goes wrong we will get the error page instead of the
break in the debugger. Fortunately you can always attach Visual Studio to a running
process hosting ASP.NET and debug the application as usual. After you have found the
error you can safely stop the debugger and you browse window will not be closed. Also
you will not suffer from the startup time of required fro Visual Studio to start the
browser and the debugger. This way instead of the edit and continue you can work with
a save and refresh style. 
&lt;p&gt;
I'm not saying that the second approach is better or worse. I'm only pointing out
that in some cases there is a faster way to do things, especially when you are getting
irritated by the Internet Explorer's address bar always remembering the wrong addresses
:-).
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/TipForWorkingWithASPNETWebProjects.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/TipForWorkingWithASPNETWebProjects.aspx" border="0" alt="kick it on DotNetKicks.com"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b5955828-5c9c-4b0d-ab45-6e9d48b08263" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=12e89f01-fd76-44aa-afc9-fb507a93b869</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,12e89f01-fd76-44aa-afc9-fb507a93b869.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I have recieved a request for a sample on how to use MyObjectDataSource control.
So without delay I have prepared a very simple web project which allows you to see
the ease of use of the my control. 
</p>
        <p>
          <a href="http://vaultofthoughts.net/content/binary/ObjectDataSourceExample.zip">ObjectDataSourceExample.zip
(19.05 KB)</a>
        </p>
        <p>
Sample is divided into 4 pages explaining how to use some of the features of the control.
Descrption is added on each page.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=12e89f01-fd76-44aa-afc9-fb507a93b869" />
      </body>
      <title>How To Use MyObjectDataSource</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,12e89f01-fd76-44aa-afc9-fb507a93b869.aspx</guid>
      <link>http://VaultOfThoughts.net/HowToUseMyObjectDataSource.aspx</link>
      <pubDate>Wed, 04 Oct 2006 17:38:47 GMT</pubDate>
      <description>&lt;p&gt;
Today I have recieved a request for a sample on how to use MyObjectDataSource control.
So without delay I have prepared a very simple web project which allows you to see
the ease of use of the my control. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://vaultofthoughts.net/content/binary/ObjectDataSourceExample.zip"&gt;ObjectDataSourceExample.zip
(19.05 KB)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Sample is divided into 4 pages explaining how to use some of the features of the control.
Descrption is added on each page.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=12e89f01-fd76-44aa-afc9-fb507a93b869" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=f68d20b3-2835-4f59-8e3c-50831130e849</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,f68d20b3-2835-4f59-8e3c-50831130e849.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
There is a feature in Visual Studio 2005 called Code Snippets. Basically what this
feature allows you to do is type in a code editor, some special string such as "prop":
</p>
        <p>
          <img src="http://vaultofthoughts.net/content/binary/prop.jpg" border="0" />
        </p>
        <p>
And then by pressing the TAB key once (or twice if using the provided intellisense),
Visual Studio generates some code for you as on the picture below:
</p>
        <p>
          <img src="http://vaultofthoughts.net/content/binary/prop2.jpg" border="0" />
        </p>
        <p>
Notice that there are reqions highlited witha green color. You can tab through those
regions with a TAB key. On the above example, If you eddit the type of the field,
the type of the property is automaticaly updated for you. The same holds true for
the field name which is used inside the property.
</p>
        <p>
Visual Studio 2005 comes boundled with a couple of useful code snippets of which I
use the "prop" the most. However, If you do mostly ASP.NET applications you know that
the standart property with underlying field is not so widely used here. What we need
is a property which uses ViewState as a storage medium. Fortunatelly it is very easy
to create your own snippets. Just go to the folder where VS is installed - for me
it was: <i>"c:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#"</i>.
There you will find all the available snippets. When you look any one of them what
you will find is a simple xml defining the snippet behavior. From there it should
be no problem to create your own snippets.
</p>
        <p>
Being ASP.NET developer myself I have created few snippets that help me doing my work
faster and those are: propv, propvd, props and test. First two generate a property
that uses the ViewState, the third one generates a static property and the third one
a method that has a Test attribute of an NUnit testing framework. You can download
the snippets here: <a href="http://vaultofthoughts.net/content/binary/Snippets.zip">Snippets.zip
(2,23 KB)</a>. Just put them together with the rest of the snippets.
</p>
        <p>
As a side note I must say that I'm mostly a C# developer, but I have written my share
of the code in VB.NET so I know what I'm missing using C#. One of the things that
I miss is the snippet support. VB.NET comes with WAY more standard snippets and those
snippets can do a lot more such as add a using statement or a reference to an assembly.
There are even application that support creating snippets for VB.NET. Given the limitations
of C# snippets I don't think we need such a tool at the moment :-(. Additionaly I
recommend browsing the internet for more information as this article is just an overview
of what else Visual Stidio can do for you if you just ask :-).
</p>
        <p>
          <hr />
        </p>
        <p>
Do you own a <a href="http://www.math.dartmouth.edu/~mqed/NLA/BarCodes/BarCodes.phtml">business</a> that
requires the latest in <a href="http://www.barcodesinc.com/cats/portable-data-terminals/">wireless
barcode scanner</a> technology? Find this as well as a wide selection of <a href="http://www.barcodesinc.com/">barcode
printers</a> or a the best brands of <a href="http://www.barcodesinc.com/cats/credit-card-readers/">credit
card reader</a> at the <a href="http://www.loc.gov/issn/barcodes.html">barcode</a> experts. 
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/VisualStudioCodeSnippets.aspx">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/VisualStudioCodeSnippets.aspx" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=f68d20b3-2835-4f59-8e3c-50831130e849" />
      </body>
      <title>Visual Studio Code Snippets</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,f68d20b3-2835-4f59-8e3c-50831130e849.aspx</guid>
      <link>http://VaultOfThoughts.net/VisualStudioCodeSnippets.aspx</link>
      <pubDate>Thu, 10 Aug 2006 09:51:58 GMT</pubDate>
      <description>&lt;p&gt;
There is a feature in Visual Studio 2005 called Code Snippets. Basically what this
feature allows you to do is type in a code editor, some special string such as "prop":
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://vaultofthoughts.net/content/binary/prop.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
And then by pressing the TAB key once (or twice if using the provided intellisense),
Visual Studio generates some code for you as on the picture below:
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://vaultofthoughts.net/content/binary/prop2.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
Notice that there are reqions highlited witha green color. You can tab through those
regions with a TAB key. On the above example, If you eddit the type of the field,
the type of the property is automaticaly updated for you. The same holds true for
the field name which is used inside the property.
&lt;/p&gt;
&lt;p&gt;
Visual Studio 2005 comes boundled with a couple of useful code snippets of which I
use the "prop" the most. However, If you do mostly ASP.NET applications you know that
the standart property with underlying field is not so widely used here. What we need
is a property which uses ViewState as a storage medium. Fortunatelly it is very easy
to create your own snippets. Just go to the folder where VS is installed - for me
it was: &lt;i&gt;"c:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#"&lt;/i&gt;.
There you will find all the available snippets. When you look any one of them what
you will find is a simple xml defining the snippet behavior. From there it should
be no problem to create your own snippets.
&lt;/p&gt;
&lt;p&gt;
Being ASP.NET developer myself I have created few snippets that help me doing my work
faster and those are: propv, propvd, props and test. First two generate a property
that uses the ViewState, the third one generates a static property and the third one
a method that has a Test attribute of an NUnit testing framework. You can download
the snippets here: &lt;a href="http://vaultofthoughts.net/content/binary/Snippets.zip"&gt;Snippets.zip
(2,23 KB)&lt;/a&gt;. Just put them together with the rest of the snippets.
&lt;/p&gt;
&lt;p&gt;
As a side note I must say that I'm mostly a C# developer, but I have written my share
of the code in VB.NET so I know what I'm missing using C#. One of the things that
I miss is the snippet support. VB.NET comes with WAY more standard snippets and those
snippets can do a lot more such as add a using statement or a reference to an assembly.
There are even application that support creating snippets for VB.NET. Given the limitations
of C# snippets I don't think we need such a tool at the moment :-(. Additionaly I
recommend browsing the internet for more information as this article is just an overview
of what else Visual Stidio can do for you if you just ask :-).
&lt;/p&gt;
&lt;p&gt;
&lt;hr&gt;
&lt;/p&gt;
&lt;p&gt;
Do you own a &lt;a href="http://www.math.dartmouth.edu/~mqed/NLA/BarCodes/BarCodes.phtml"&gt;business&lt;/a&gt; that
requires the latest in &lt;a href="http://www.barcodesinc.com/cats/portable-data-terminals/"&gt;wireless
barcode scanner&lt;/a&gt;&amp;nbsp;technology? Find this as well as a wide selection of &lt;a href="http://www.barcodesinc.com/"&gt;barcode
printers&lt;/a&gt; or a the best brands of &lt;a href="http://www.barcodesinc.com/cats/credit-card-readers/"&gt;credit
card reader&lt;/a&gt; at the &lt;a href="http://www.loc.gov/issn/barcodes.html"&gt;barcode&lt;/a&gt; experts. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/VisualStudioCodeSnippets.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/VisualStudioCodeSnippets.aspx" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=f68d20b3-2835-4f59-8e3c-50831130e849" /&gt;</description>
      <category>.NET</category>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=3a66070f-3fdc-4393-94bb-a12e94153eae</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,3a66070f-3fdc-4393-94bb-a12e94153eae.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the first article about <a href="http://vaultofthoughts.net/OptimizingRenderedPageSize.aspx">optimizing
rendered page size</a> I have pointed out that you should consider using short names
for content placeholders on your master pages because each control's id inside
those placeholders is prefixed with the id of the placeholder. But when you look inside
the page there is of course another field where you can make some improvements - the
ViewState.
</p>
        <p>
There are lot of articles about what you can do to make the ViewState smaller. Mainly
- disable it everywhere where it isn't needed. This is a first step you can take.
Amongst other things you can also use the <i>SavePageStateToPersistenceMedium</i> and <i>LoadPageStateFromPersistenceMedium</i> methods
of the Page class. The purpose of those methods according to MSDN is to save and load
any view state information from and to a page object. And that is true. But how to
use those methods?
</p>
        <p>
Browsing the results returned by google you can find plenty of solutions out there
each unique in its way. Why they are different? The problem with those methods is
not saving the state. To save the state you can use anything from the Session object,
flat files or even database. The real problem is making sure that when a form is submited
to a server, its ViewState is properly loaded - the concrete ViewState that was saved
for that concrete page. A solution to just use Session["ViewState"] as a storage medium
won't do, because then we can only have one ViewState for each user - no multiple
windows/frames allowed. 
</p>
        <p>
My solution would be to include a hidden field on each page, name it somehow and use
its value as a key for identifying which ViewState to load. Such a hidden field may
be added dynamicaly in a base page so no additional work is required when adding new
pages to a project. This field of course shoud hold some unique value (such as guid)
in order to distinguish one page request from the other.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/OptimizingRenderedPageSizePart2.aspx">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/OptimizingRenderedPageSizePart2.aspx" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=3a66070f-3fdc-4393-94bb-a12e94153eae" />
      </body>
      <title>Optimizing Rendered Page Size - Part 2</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,3a66070f-3fdc-4393-94bb-a12e94153eae.aspx</guid>
      <link>http://VaultOfThoughts.net/OptimizingRenderedPageSizePart2.aspx</link>
      <pubDate>Tue, 08 Aug 2006 18:41:13 GMT</pubDate>
      <description>&lt;p&gt;
In the first article about &lt;a href="http://vaultofthoughts.net/OptimizingRenderedPageSize.aspx"&gt;optimizing
rendered page size&lt;/a&gt; I have pointed out that you should consider using short names
for content placeholders on your master pages because each control's id&amp;nbsp;inside
those placeholders is prefixed with the id of the placeholder. But when you look inside
the page there is of course another field where you can make some improvements - the
ViewState.
&lt;/p&gt;
&lt;p&gt;
There are lot of articles about what you can do to make the ViewState smaller. Mainly
- disable it everywhere where it isn't needed. This is a first step you can take.
Amongst other things you can also use the &lt;i&gt;SavePageStateToPersistenceMedium&lt;/i&gt; and &lt;i&gt;LoadPageStateFromPersistenceMedium&lt;/i&gt; methods
of the Page class. The purpose of those methods according to MSDN is to save and load
any view state information from and to a page object. And that is true. But how to
use those methods?
&lt;/p&gt;
&lt;p&gt;
Browsing the results returned by google you can find plenty of solutions out there
each unique in its way. Why they are different? The problem with those methods is
not saving the state. To save the state you can use anything from the Session object,
flat files or even database. The real problem is making sure that when a form is submited
to a server, its ViewState is properly loaded - the concrete ViewState that was saved
for that concrete page. A solution to just use Session["ViewState"] as a storage medium
won't do, because then we can only have one ViewState for each user - no multiple
windows/frames allowed. 
&lt;/p&gt;
&lt;p&gt;
My solution would be to include a hidden field on each page, name it somehow and use
its value as a key for identifying which ViewState to load. Such a hidden field may
be added dynamicaly in a base page so no additional work is required when adding new
pages to a project. This field of course shoud hold some unique value (such as guid)
in order to distinguish one page request from the other.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/OptimizingRenderedPageSizePart2.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/OptimizingRenderedPageSizePart2.aspx" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=3a66070f-3fdc-4393-94bb-a12e94153eae" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=93082370-ddf4-46d9-8a4e-48768eeedf16</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,93082370-ddf4-46d9-8a4e-48768eeedf16.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have created an ASP.NET controls pack that I'm using on a daily basis. The pack
includes:
</p>
        <table style="margin: 1em;">
          <tbody>
            <tr>
              <th>
Control Name</th>
              <th>
Description</th>
            </tr>
            <tr>
              <td>
Button</td>
              <td>
A Button that supports a Confirmation message before submitting a form.</td>
            </tr>
            <tr>
              <td>
Image</td>
              <td>
An Image that allows you to set the MouseOverImageUrl property.</td>
            </tr>
            <tr>
              <td>
ImageButton</td>
              <td>
An ImageButton that supports a Confirmation message before submitting a form and a
MouseOverImageUrl.</td>
            </tr>
            <tr>
              <td>
ImageCheckBox</td>
              <td>
A CheckBox that renders itself as an image either checked or unchecked. It also supports
MouseOverImageUrl for both states.</td>
            </tr>
            <tr>
              <td>
MyObjectDataSource</td>
              <td>
An ObjectDataSource that supports build in sorting and filtering of returned objects.</td>
            </tr>
            <tr>
              <td>
CompareValidator</td>
              <td>
A CompareValidator that supports displaying error messages as an image.</td>
            </tr>
            <tr>
              <td>
RequiredFieldValidator</td>
              <td>
A RequiredFieldValidator that supports displaying error messages as an image.</td>
            </tr>
            <tr>
              <td>
PageParameter</td>
              <td>
A parameter that can be used in DataSource controls parameter collections such as
SelectParameters. It allows you to get parameters from properties of a page.</td>
            </tr>
            <tr>
              <td>
TemplateControlParameter</td>
              <td>
A parameter that can be used in DataSource controls parameter collections such as
SelectParameters. It allows you to get parameters from properties of a nearest TemplateControl.</td>
            </tr>
          </tbody>
        </table>
        <p>
I find those control very helpful on a daily basis and hope you will like it also.
Note also that some of them I have described earlier but now I provide them as a one
download with a full source code. It is available here: <a href="http://vaultofthoughts.net/content/binary/Controls.zip">Controls.zip
(50.7 KB)</a>.
</p>
        <p>
Using most of the controls should be obvious. The most complicated one is the MyObjectDataSource.
Mostly I have described it in my previous articles about this control. I have added
a filtering feature by specifying FilterParameters on the data source control. There
are two options: you can either use the internal filter mechanism which will filter
values for you or by providing an additional parameter to a select method that is
used for selecting objects. This additional parameter should be of type Dictionary&lt;string,
object&gt; where the key is the name of the property to filter. Working with internal
filter requires almost no code for example using ControlParameter:
</p>
        <p>
          <code>&lt;asp:ControlParameter Name="Role.Id" ControlID="Role" PropertyName="SelectedValue"
/&gt;</code>
        </p>
        <p>
Where Name attribute corresponds to a name of the property by which you want to filter
and PropertyName corresponds to a property on a control that provides a value to filter
by. (I know it my not be very intuitive, but that is what ASP.NET provides and I have
been building on top of that). Filtering currently works similar to SQL LIKE command
with wildcards before and after the expression.
</p>
        <p>
If you work without internal filter you have the responsibility to filter the collection
yourself given the filter parameter to passed to a select method.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/ASPNETControlsPack.aspx">
            <img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/ASPNETControlsPack.aspx" alt="kick it on DotNetKicks.com" border="0" />
          </a>
        </p>
        <p>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=93082370-ddf4-46d9-8a4e-48768eeedf16" />
      </body>
      <title>ASP.NET Controls pack</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,93082370-ddf4-46d9-8a4e-48768eeedf16.aspx</guid>
      <link>http://VaultOfThoughts.net/ASPNETControlsPack.aspx</link>
      <pubDate>Tue, 08 Aug 2006 11:06:14 GMT</pubDate>
      <description>&lt;p&gt;
I have created an ASP.NET controls pack that I'm using on a daily basis. The pack
includes:
&lt;/p&gt;
&lt;table style="margin: 1em;"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;
Control Name&lt;/th&gt;
&lt;th&gt;
Description&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
Button&lt;/td&gt;
&lt;td&gt;
A Button that supports a Confirmation message before submitting a form.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
Image&lt;/td&gt;
&lt;td&gt;
An Image that allows you to set the MouseOverImageUrl property.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
ImageButton&lt;/td&gt;
&lt;td&gt;
An ImageButton that supports a Confirmation message before submitting a form and a
MouseOverImageUrl.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
ImageCheckBox&lt;/td&gt;
&lt;td&gt;
A CheckBox that renders itself as an image either checked or unchecked. It also supports
MouseOverImageUrl for both states.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
MyObjectDataSource&lt;/td&gt;
&lt;td&gt;
An ObjectDataSource that supports build in sorting and filtering of returned objects.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
CompareValidator&lt;/td&gt;
&lt;td&gt;
A CompareValidator that supports displaying error messages as an image.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
RequiredFieldValidator&lt;/td&gt;
&lt;td&gt;
A RequiredFieldValidator that supports displaying error messages as an image.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
PageParameter&lt;/td&gt;
&lt;td&gt;
A parameter that can be used in DataSource controls parameter collections such as
SelectParameters. It allows you to get parameters from properties of a page.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
TemplateControlParameter&lt;/td&gt;
&lt;td&gt;
A parameter that can be used in DataSource controls parameter collections such as
SelectParameters. It allows you to get parameters from properties of a nearest TemplateControl.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
I find those control very helpful on a daily basis and hope you will like it also.
Note also that some of them I have described earlier but now I provide them as a one
download with a full source code. It is available here: &lt;a href="http://vaultofthoughts.net/content/binary/Controls.zip"&gt;Controls.zip
(50.7 KB)&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Using most of the controls should be obvious. The most complicated one is the MyObjectDataSource.
Mostly I have described it in my previous articles about this control. I have added
a filtering feature by specifying FilterParameters on the data source control. There
are two options: you can either use the internal filter mechanism which will filter
values for you or by providing an additional parameter to a select method that is
used for selecting objects. This additional parameter should be of type Dictionary&amp;lt;string,
object&amp;gt; where the key is the name of the property to filter. Working with internal
filter requires almost no code for example using ControlParameter:
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;&amp;lt;asp:ControlParameter Name="Role.Id" ControlID="Role" PropertyName="SelectedValue"
/&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Where Name attribute corresponds to a name of the property by which you want to filter
and PropertyName corresponds to a property on a control that provides a value to filter
by. (I know it my not be very intuitive, but that is what ASP.NET provides and I have
been building on top of that). Filtering currently works similar to SQL LIKE command
with wildcards before and after the expression.
&lt;/p&gt;
&lt;p&gt;
If you work without internal filter you have the responsibility to filter the collection
yourself given the filter parameter to passed to a select method.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://vaultofthoughts.net/ASPNETControlsPack.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://vaultofthoughts.net/ASPNETControlsPack.aspx" alt="kick it on DotNetKicks.com" border="0"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=93082370-ddf4-46d9-8a4e-48768eeedf16" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=3b17319d-4b8b-4d64-9e49-e898638801a7</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,3b17319d-4b8b-4d64-9e49-e898638801a7.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
All of us who have been using any kind of ASP.NET Templated Controls such as FormView,
sooner or later come to a problem of extracting value from a field inside a template.
For example when you are using a FormView control and want to programatically access
a control inside one of the templates. What are the options? You have to resort to
some kind of FindControl calls which are way from perfect. I have been thinking about
this issue for some time now and I have an idea.
</p>
        <p>
The idea is to change tha way the PageBuildProvider constructs the part of class that
represents the ASPX file of your page. (the other one is of course the code behind).
So what could be changed? Imagine a situation where you have a page and there is just
one FormView on it. The FormView has its templates defined in an ASPX file. What happens
now is the ASP.NET framework builds the part of the class given this ASPX file and
what we get is a nice intellisense in the code behind telling us that the our class
has a field FormView1 of type FormView which of course does not have any knowledge
about controls inside the templates. But what if...
</p>
        <p>
But what if instead of the FormView1 being of type FormView, the part of the class
constructed from ASPX file provided us with a field FormView1 of type derived from
the FormView but with few additional properties which represent controls found inside
the templates - in a strongly typed manner.
</p>
        <p>
Now. The benefits of such a solution are obvious mainly compile time type checking.
</p>
        <p>
Is it possible to make ASP.NET 2.0 to provide such a solution? This is a hard question.
I have checked the major components that are responsible for the way the classes are
constructed now. I haven't gone deep into details but it it looks like some changes
inside the PageBuildProvider (or some base classes) would be required. Also a ControlBuilder
responsible for building a templated control would have to constuct a whole new class
representing our control. I suppose that the major problem lies in constructing such
a derived class with all those properties representing inner controls.
</p>
        <p>
So basically I hope MS will/is thinking about making our lives easier and more type
safe :-) or maybe some brave developer is willing to try to implement my concept?
Either way it would be nice to have such a feature some time in the future.
</p>
        <p>
Some additional considerations include the problem with accessing controls from ItemTemplate
of a FormView control when in Edit mode. Another issou would be with controls such
as GridView where some of the templates represent a repeating content and not a single
data etc...
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/08/strongly-typed-templated-controls.html">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/08/strongly-typed-templated-controls.html" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=3b17319d-4b8b-4d64-9e49-e898638801a7" />
      </body>
      <title>Strongly Typed Templated Controls</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,3b17319d-4b8b-4d64-9e49-e898638801a7.aspx</guid>
      <link>http://VaultOfThoughts.net/StronglyTypedTemplatedControls.aspx</link>
      <pubDate>Mon, 07 Aug 2006 09:11:13 GMT</pubDate>
      <description>&lt;p&gt;
All of us who have been using any kind of ASP.NET Templated Controls such as FormView,
sooner or later come to a problem of extracting value from a field inside a template.
For example when you are using a FormView control and want to programatically access
a control inside one of the templates. What are the options? You have to resort to
some kind of FindControl calls which are way from perfect. I have been thinking about
this issue for some time now and I have an idea.
&lt;/p&gt;
&lt;p&gt;
The idea is to change tha way the PageBuildProvider constructs the part of class that
represents the ASPX file of your page. (the other one is of course the code behind).
So what could be changed? Imagine a situation where you have a page and there is just
one FormView on it. The FormView has its templates defined in an ASPX file. What happens
now is the ASP.NET framework builds the part of the class given this ASPX file and
what we get is a nice intellisense in the code behind telling us that the our class
has a field FormView1 of type FormView which of course does not have any knowledge
about controls inside the templates. But what if...
&lt;/p&gt;
&lt;p&gt;
But what if instead of the FormView1 being of type FormView, the part of the class
constructed from ASPX file provided us with a field FormView1 of type derived from
the FormView but with few additional properties which represent controls found inside
the templates - in a strongly typed manner.
&lt;/p&gt;
&lt;p&gt;
Now. The benefits of such a solution are obvious mainly compile time type checking.
&lt;/p&gt;
&lt;p&gt;
Is it possible to make ASP.NET 2.0 to provide such a solution? This is a hard question.
I have checked the major components that are responsible for the way the classes are
constructed now. I haven't gone deep into details but it it looks like some changes
inside the PageBuildProvider (or some base classes) would be required. Also a ControlBuilder
responsible for building a templated control would have to constuct a whole new class
representing our control. I suppose that the major problem lies in constructing such
a derived class with all those properties representing inner controls.
&lt;/p&gt;
&lt;p&gt;
So basically I hope MS will/is thinking about making our lives easier and more type
safe :-) or maybe some brave developer is willing to try to implement my concept?
Either way it would be nice to have such a feature some time in the future.
&lt;/p&gt;
&lt;p&gt;
Some additional considerations include the problem with accessing controls from ItemTemplate
of a FormView control when in Edit mode. Another issou would be with controls such
as GridView where some of the templates represent a repeating content and not a single
data etc...
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/08/strongly-typed-templated-controls.html"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/08/strongly-typed-templated-controls.html" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=3b17319d-4b8b-4d64-9e49-e898638801a7" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=4984ee55-6e86-40e0-bc2b-c982c0f9372e</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,4984ee55-6e86-40e0-bc2b-c982c0f9372e.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
ASP.NET 2.0 comes with a mechanism called two way databinding which allows us to declaratively
bind a form element to a property of an object in such way that updates are supported.
For the readonly display you typically use the Eval method in your aspx page. For
the two way mechanism, you use the Bind keyword (or method?).
</p>
        <p>
Using the Bind keyword is really simple in most scenarios that you encounter, especially
if you are using the SqlDataSource control. Problems start to come up when you try
to use a more object oriented approach and use the ObjectDataSource control.
</p>
        <p>
Now, still in most cases using ObjectDataSource will not cause you any problems when
what u bind is a simple property such as a name of a client i.e.: you have a collection
of clients that you bind to a GridView and one of the columns displays the name of
the client. To do so you use something like Bind("Name"). The problem arises when
you need to bind to a subproperty such as in Bind("Address.StreetName"). This won't
work unfortunatelly...
</p>
        <p>
Why it is not possible to perform a two way databinding in such a way, there are discussions
over the Internet. BTW: Eval("Address.StreetName") works as expected. So what can
we do about it? Of course there is a solution for this problem and it has came to
me when I was thinking why there is no such a problem when you use the SqlDataSource
control.
</p>
        <p>
The reason why there is no such problem when you are working with relational data
is that you are creating an SQL query that is a <i>VIEW</i> of the data that you want
to display i.e.: it has all the columns you need and there are no "sub-columns". So
what we need to do is to translate this to an object world.
</p>
        <p>
So basically what we need to do is create an object that will serv as a view on our
objects and bind to this view object. For example given the aforementioned client
object with a name and an address we create a ClientView object that encapsulates
our client object and exposes the needed properties such as Name or StreetName. Those
properties in turn call on the properties of the client object either directly as
in Name or to some nested property as in Address.StreetName.
</p>
        <p>
Making a select method used by the DataSourceControl to return the collection of ClientView
object should not pose any problem. When using such a technique I would suggest putting
the view classes somewhere outside the domain model since the only reason they exist
is to allow for the two way databinding to work so there is no reason to pollute the
model.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/07/using-bind-with-nested-properties.html">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/07/using-bind-with-nested-properties.html" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=4984ee55-6e86-40e0-bc2b-c982c0f9372e" />
      </body>
      <title>Using Bind with nested properties</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,4984ee55-6e86-40e0-bc2b-c982c0f9372e.aspx</guid>
      <link>http://VaultOfThoughts.net/UsingBindWithNestedProperties.aspx</link>
      <pubDate>Mon, 07 Aug 2006 09:08:44 GMT</pubDate>
      <description>&lt;p&gt;
ASP.NET 2.0 comes with a mechanism called two way databinding which allows us to declaratively
bind a form element to a property of an object in such way that updates are supported.
For the readonly display you typically use the Eval method in your aspx page. For
the two way mechanism, you use the Bind keyword (or method?).
&lt;/p&gt;
&lt;p&gt;
Using the Bind keyword is really simple in most scenarios that you encounter, especially
if you are using the SqlDataSource control. Problems start to come up when you try
to use a more object oriented approach and use the ObjectDataSource control.
&lt;/p&gt;
&lt;p&gt;
Now, still in most cases using ObjectDataSource will not cause you any problems when
what u bind is a simple property such as a name of a client i.e.: you have a collection
of clients that you bind to a GridView and one of the columns displays the name of
the client. To do so you use something like Bind("Name"). The problem arises when
you need to bind to a subproperty such as in Bind("Address.StreetName"). This won't
work unfortunatelly...
&lt;/p&gt;
&lt;p&gt;
Why it is not possible to perform a two way databinding in such a way, there are discussions
over the Internet. BTW: Eval("Address.StreetName") works as expected. So what can
we do about it? Of course there is a solution for this problem and it has came to
me when I was thinking why there is no such a problem when you use the SqlDataSource
control.
&lt;/p&gt;
&lt;p&gt;
The reason why there is no such problem when you are working with relational data
is that you are creating an SQL query that is a &lt;i&gt;VIEW&lt;/i&gt; of the data that you want
to display i.e.: it has all the columns you need and there are no "sub-columns". So
what we need to do is to translate this to an object world.
&lt;/p&gt;
&lt;p&gt;
So basically what we need to do is create an object that will serv as a view on our
objects and bind to this view object. For example given the aforementioned client
object with a name and an address we create a ClientView object that encapsulates
our client object and exposes the needed properties such as Name or StreetName. Those
properties in turn call on the properties of the client object either directly as
in Name or to some nested property as in Address.StreetName.
&lt;/p&gt;
&lt;p&gt;
Making a select method used by the DataSourceControl to return the collection of ClientView
object should not pose any problem. When using such a technique I would suggest putting
the view classes somewhere outside the domain model since the only reason they exist
is to allow for the two way databinding to work so there is no reason to pollute the
model.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/07/using-bind-with-nested-properties.html"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/07/using-bind-with-nested-properties.html" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=4984ee55-6e86-40e0-bc2b-c982c0f9372e" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=c88c1717-cf2c-41a9-b97f-dfb5e6a0d9da</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,c88c1717-cf2c-41a9-b97f-dfb5e6a0d9da.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I have been struggling with a typical table layed out page. It was a MasterPage
with few ContentPlaceHolder controls - one for content, one for menu and few for other
things. The problem was that the place holders were inside the td elements and some
of the elements were made in such a way that they dropped shadow (background-image)
and it was not possible from the content page to override this behavior (at least
not in a clean way). First I have tried to override the style which made the cell
to have an image at the bottom by adding the style element inside one the contents.
Unfortunatelly it is not valid html to do so. Then I have thought about adding a ContentPlaceHolder
inside the head element. Unfortunatelly Visual Studio does not provide the ContentPlaceHolder
control in the intellisense so I thought that it will not wokr... But it did!!!
</p>
        <p>
What is a very good thing to know is that despite the VS not telling you about it,
you can put a ContentPlaceHolder inside the head element. You can work with it just
like with any other ContentPlaceHolder by providing default content for example. It
can also be used as a place for additional page level style elements. One great feature
that VS DOES provide is the fact that in the content page, when you edit the head
ContentPlaceHolder, you get the intellisense for the head element so you wont see
any divs or other body elements there.
</p>
        <p>
After this discovery I have made a search on google to find if anyone had the same
problem and I have found the following: <a href="http://odetocode.com/Blogs/scott/archive/2006/04/10/3258.aspx">artcle</a>.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/07/master-page-contentplaceholder-inside.html">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/07/master-page-contentplaceholder-inside.html" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=c88c1717-cf2c-41a9-b97f-dfb5e6a0d9da" />
      </body>
      <title>Master Page ContentPlaceHolder inside head element</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,c88c1717-cf2c-41a9-b97f-dfb5e6a0d9da.aspx</guid>
      <link>http://VaultOfThoughts.net/MasterPageContentPlaceHolderInsideHeadElement.aspx</link>
      <pubDate>Mon, 07 Aug 2006 08:59:46 GMT</pubDate>
      <description>&lt;p&gt;
Today I have been struggling with a typical table layed out page. It was a MasterPage
with few ContentPlaceHolder controls - one for content, one for menu and few for other
things. The problem was that the place holders were inside the td elements and some
of the elements were made in such a way that they dropped shadow (background-image)
and it was not possible from the content page to override this behavior (at least
not in a clean way). First I have tried to override the style which made the cell
to have an image at the bottom by adding the style element inside one the contents.
Unfortunatelly it is not valid html to do so. Then I have thought about adding a ContentPlaceHolder
inside the head element. Unfortunatelly Visual Studio does not provide the ContentPlaceHolder
control in the intellisense so I thought that it will not wokr... But it did!!!
&lt;/p&gt;
&lt;p&gt;
What is a very good thing to know is that despite the VS not telling you about it,
you can put a ContentPlaceHolder inside the head element. You can work with it just
like with any other ContentPlaceHolder by providing default content for example. It
can also be used as a place for additional page level style elements. One great feature
that VS DOES provide is the fact that in the content page, when you edit the head
ContentPlaceHolder, you get the intellisense for the head element so you wont see
any divs or other body elements there.
&lt;/p&gt;
&lt;p&gt;
After this discovery I have made a search on google to find if anyone had the same
problem and I have found the following: &lt;a href="http://odetocode.com/Blogs/scott/archive/2006/04/10/3258.aspx"&gt;artcle&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/07/master-page-contentplaceholder-inside.html"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/07/master-page-contentplaceholder-inside.html" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=c88c1717-cf2c-41a9-b97f-dfb5e6a0d9da" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=d6611a1f-56f6-4d94-a95d-4a418b85c6fe</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,d6611a1f-56f6-4d94-a95d-4a418b85c6fe.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have made some research on how to create a container control - a control which works
in a similar way as the Panel control does. There are three key thing you have to
do in order to make it work:
</p>
        <p>
        </p>
        <ul>
          <li>
Add ParseChildren(false) attribute 
</li>
          <li>
Add PersistChildren(true) attribute 
</li>
          <li>
Add Designer(typeof(PanelDesigner)) attribute where PanelDesigner is a custom class
deriving from ContainerControlDesigner. I haven't tried it, but I suppose that using
the ContainerControlDesigner in with Designer attribute would also do.</li>
        </ul>
        <p>
        </p>
        <p>
It's all. You do not have to add any more code. Since, the custom PanelDesigner does
not require any code, the question arises - why do we need it? Shouldn't it be some
kind of attribute?
</p>
        <p>
A creative usage for such a control could be for example when you need a panel control
which has some kind of static content, or maybe images as borders. You get the picture.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=d6611a1f-56f6-4d94-a95d-4a418b85c6fe" />
      </body>
      <title>A custom container control</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,d6611a1f-56f6-4d94-a95d-4a418b85c6fe.aspx</guid>
      <link>http://VaultOfThoughts.net/ACustomContainerControl.aspx</link>
      <pubDate>Mon, 07 Aug 2006 08:54:06 GMT</pubDate>
      <description>&lt;p&gt;
I have made some research on how to create a container control - a control which works
in a similar way as the Panel control does. There are three key thing you have to
do in order to make it work:
&lt;/p&gt;
&lt;p&gt;
&lt;ul&gt;
&lt;li&gt;
Add ParseChildren(false) attribute 
&lt;li&gt;
Add PersistChildren(true) attribute 
&lt;li&gt;
Add Designer(typeof(PanelDesigner)) attribute where PanelDesigner is a custom class
deriving from ContainerControlDesigner. I haven't tried it, but I suppose that using
the ContainerControlDesigner in with Designer attribute would also do.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
It's all. You do not have to add any more code. Since, the custom PanelDesigner does
not require any code, the question arises - why do we need it? Shouldn't it be some
kind of attribute?
&lt;/p&gt;
&lt;p&gt;
A creative usage for such a control could be for example when you need a panel control
which has some kind of static content, or maybe images as borders. You get the picture.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=d6611a1f-56f6-4d94-a95d-4a418b85c6fe" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=b9130b88-4434-4611-95b1-4937b7cc8fff</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,b9130b88-4434-4611-95b1-4937b7cc8fff.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have used my PathsBuildProvider a while now and I have came across some issues.
I have found two major issues.
</p>
        <p>
The first issue had to do with the fact that from some strange reason the development
web server treats root directory "/" in a different way than IIS. So redirecting to
/aplicationname/page.aspx has different effect those two servers. I have made a change
so that the property is not statically encoded to return a string but rather to use
System.Web.VirtualPathUtility.ToAbsolute("~/Default.aspx"). As far as I know it works
on both IIS and the development server.
</p>
        <p>
The second issue has to do with the application name being encoded in the code as
in Paths.WebTest.Default_aspx. Imagine however that you deploy your application under
a different name say WebTest2. It no longer works and strange things happen. To overcome
this I have changed the code to allways use the AppRoot as a root directory name.
</p>
        <p>
I have also included the changes proposed by Will Gant. The changes are simple yet
brilliant. He proposed that the properties returning file paths to be static. In fact
that was what I have first implemented but then I have run to a problem with doing
something like Paths.AppRoot.Directory1.Directory2.Default_aspx since if all properties
are static than I'm not able to return anything meaningful (an object instance) from
such property. Will's solution uses a fact that when you reference a directory as
in Paths.AppRoot.Directory1 what you really get is not a property Directory1 of AppRoot
object/class, but a namespace qualified Directory1 class name which in turn has static
properties.
</p>
        <p>
As usuall, the code is available here <a href="http://vaultofthoughts.net/content/binary/PathsBuildProvider.zip">PathsBuildProvider.zip
(11,13 KB)</a>.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b9130b88-4434-4611-95b1-4937b7cc8fff" />
      </body>
      <title>PathsBuildProvider update</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,b9130b88-4434-4611-95b1-4937b7cc8fff.aspx</guid>
      <link>http://VaultOfThoughts.net/PathsBuildProviderUpdate.aspx</link>
      <pubDate>Mon, 07 Aug 2006 08:48:46 GMT</pubDate>
      <description>&lt;p&gt;
I have used my PathsBuildProvider a while now and I have came across some issues.
I have found two major issues.
&lt;/p&gt;
&lt;p&gt;
The first issue had to do with the fact that from some strange reason the development
web server treats root directory "/" in a different way than IIS. So redirecting to
/aplicationname/page.aspx has different effect those two servers. I have made a change
so that the property is not statically encoded to return a string but rather to use
System.Web.VirtualPathUtility.ToAbsolute("~/Default.aspx"). As far as I know it works
on both IIS and the development server.
&lt;/p&gt;
&lt;p&gt;
The second issue has to do with the application name being encoded in the code as
in Paths.WebTest.Default_aspx. Imagine however that you deploy your application under
a different name say WebTest2. It no longer works and strange things happen. To overcome
this I have changed the code to allways use the AppRoot as a root directory name.
&lt;/p&gt;
&lt;p&gt;
I have also included the changes proposed by Will Gant. The changes are simple yet
brilliant. He proposed that the properties returning file paths to be static. In fact
that was what I have first implemented but then I have run to a problem with doing
something like Paths.AppRoot.Directory1.Directory2.Default_aspx since if all properties
are static than I'm not able to return anything meaningful (an object instance) from
such property. Will's solution uses a fact that when you reference a directory as
in Paths.AppRoot.Directory1 what you really get is not a property Directory1 of AppRoot
object/class, but a namespace qualified Directory1 class name which in turn has static
properties.
&lt;/p&gt;
&lt;p&gt;
As usuall, the code is available here &lt;a href="http://vaultofthoughts.net/content/binary/PathsBuildProvider.zip"&gt;PathsBuildProvider.zip
(11,13 KB)&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b9130b88-4434-4611-95b1-4937b7cc8fff" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=1824e97e-6e31-4735-9d9b-c108ac2466e1</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,1824e97e-6e31-4735-9d9b-c108ac2466e1.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
After a lot of time spent on working with the Page.ClientScript.GetWebResourceUrl
method made it work and it was good. I had my ImageCheckBox control which worked well
using this method. The problem occured when I have inherited from the control in another
project. It stopped to properly reference the javascript from the resource url. After
short investigation I have found the that the problem was caused by the call Page.ClientScript.GetWebResourceUrl(GetType(),
...); The GetType method of course returns a different type when called inside the
inherited control and so, the web resource url was incorrect. A quick change to typeof(ImageCheckBox)
fixed the issue.
</p>
        <p>
So basically it is worth to take a minute and think when to use the GetType and when
to use the typeof.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=1824e97e-6e31-4735-9d9b-c108ac2466e1" />
      </body>
      <title>GetWebResourceUrl method catch</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,1824e97e-6e31-4735-9d9b-c108ac2466e1.aspx</guid>
      <link>http://VaultOfThoughts.net/GetWebResourceUrlMethodCatch.aspx</link>
      <pubDate>Mon, 07 Aug 2006 08:47:51 GMT</pubDate>
      <description>&lt;p&gt;
After a lot of time spent on working with the Page.ClientScript.GetWebResourceUrl
method made it work and it was good. I had my ImageCheckBox control which worked well
using this method. The problem occured when I have inherited from the control in another
project. It stopped to properly reference the javascript from the resource url. After
short investigation I have found the that the problem was caused by the call Page.ClientScript.GetWebResourceUrl(GetType(),
...); The GetType method of course returns a different type when called inside the
inherited control and so, the web resource url was incorrect. A quick change to typeof(ImageCheckBox)
fixed the issue.
&lt;/p&gt;
&lt;p&gt;
So basically it is worth to take a minute and think when to use the GetType and when
to use the typeof.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=1824e97e-6e31-4735-9d9b-c108ac2466e1" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=e9289221-7658-4576-a059-357019a514b0</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,e9289221-7658-4576-a059-357019a514b0.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I do not often rename the controls that I do not use in the code behind code, so when
I place a Label on a page it most often is named something like Label1. It does not
make a big difference when you are not paying for the bandwidth and you only have
a few controls on a form. Imagine however that you are working with Master Page and
there you are using the defaul ContentPlaceHolder1 as a place for pages to put their
contnet. Now it is only 19 characters long yes? But when you look at the generated
html, you will see that each control put on a page using such a Master Page has its
ID prefixed with the ID of the placeholder. A quick calculation on one of my pages
and It appeared that for 100 controls on a page I was wasting 2000 characters only
for the id of the placeholder!!! Just renaming the placeholder from the default ContentPlaceHolder1
to something like CPH saves 1700 characters. Such a name change does not cause any
major inconvinience on the developer's part since you will rarely reference the placeholder
by its ID.
</p>
        <p>
The same mechanism holds true especially for controls such as GridView and UserControls
which tend to have a long ID.
</p>
        <p>
Currently I'm renaming all my controls that I put inside a GridView or FormView templates
to one letter strings since I'm not using them. I also rename all my controls on pages
to a shortest string that I can come up with without loosing the meaning.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/07/optimizing-rendered-page-size.html">
            <img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/07/optimizing-rendered-page-size.html" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=e9289221-7658-4576-a059-357019a514b0" />
      </body>
      <title>Optimizing rendered page size</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,e9289221-7658-4576-a059-357019a514b0.aspx</guid>
      <link>http://VaultOfThoughts.net/OptimizingRenderedPageSize.aspx</link>
      <pubDate>Mon, 07 Aug 2006 08:47:00 GMT</pubDate>
      <description>&lt;p&gt;
I do not often rename the controls that I do not use in the code behind code, so when
I place a Label on a page it most often is named something like Label1. It does not
make a big difference when you are not paying for the bandwidth and you only have
a few controls on a form. Imagine however that you are working with Master Page and
there you are using the defaul ContentPlaceHolder1 as a place for pages to put their
contnet. Now it is only 19 characters long yes? But when you look at the generated
html, you will see that each control put on a page using such a Master Page has its
ID prefixed with the ID of the placeholder. A quick calculation on one of my pages
and It appeared that for 100 controls on a page I was wasting 2000 characters only
for the id of the placeholder!!! Just renaming the placeholder from the default ContentPlaceHolder1
to something like CPH saves 1700 characters. Such a name change does not cause any
major inconvinience on the developer's part since you will rarely reference the placeholder
by its ID.
&lt;/p&gt;
&lt;p&gt;
The same mechanism holds true especially for controls such as GridView and UserControls
which tend to have a long ID.
&lt;/p&gt;
&lt;p&gt;
Currently I'm renaming all my controls that I put inside a GridView or FormView templates
to one letter strings since I'm not using them. I also rename all my controls on pages
to a shortest string that I can come up with without loosing the meaning.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/07/optimizing-rendered-page-size.html"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/07/optimizing-rendered-page-size.html" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=e9289221-7658-4576-a059-357019a514b0" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=b38a3e8b-7f56-4cfe-a745-f483950833a3</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,b38a3e8b-7f56-4cfe-a745-f483950833a3.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
While working with my <a href="http://vaultofthoughts.net/ValidationControlsAsImagesWorkaround.aspx">validator
controls that support displaying images</a>, I have come to a situation where I needed
the image to be themable. Easy you think? 
</p>
        <p>
Not so easy as it should be. I have added the Themeable(true) attribute to an ImageUrl
property, put a control on a page set everything in the skin file, compilled and...
??? No it didn't work. Url points to the same directory as specified in the skin file
but is not prefixed with the theme folder name. A quick reflector peek at the Image
control and the solution was found. There is a little known UrlProperty attribute
that has to be applied to a property which we want to behave as expected when applying
urls from skin files.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b38a3e8b-7f56-4cfe-a745-f483950833a3" />
      </body>
      <title>UrlProperty and skins</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,b38a3e8b-7f56-4cfe-a745-f483950833a3.aspx</guid>
      <link>http://VaultOfThoughts.net/UrlPropertyAndSkins.aspx</link>
      <pubDate>Mon, 07 Aug 2006 08:45:06 GMT</pubDate>
      <description>&lt;p&gt;
While working with my &lt;a href="http://vaultofthoughts.net/ValidationControlsAsImagesWorkaround.aspx"&gt;validator
controls that support displaying images&lt;/a&gt;, I have come to a situation where I needed
the image to be themable. Easy you think? 
&lt;p&gt;
Not so easy as it should be. I have added the Themeable(true) attribute to an ImageUrl
property, put a control on a page set everything in the skin file, compilled and...
??? No it didn't work. Url points to the same directory as specified in the skin file
but is not prefixed with the theme folder name. A quick reflector peek at the Image
control and the solution was found. There is a little known UrlProperty attribute
that has to be applied to a property which we want to behave as expected when applying
urls from skin files.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=b38a3e8b-7f56-4cfe-a745-f483950833a3" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=0a9521ef-58cc-4ff3-be7d-06cff1458c6a</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,0a9521ef-58cc-4ff3-be7d-06cff1458c6a.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I have been trying to make the Validation controls show an image instead of plain
text in case of an error. In doing so I have used <a href="http://www.aisto.com/roeder/dotnet/">Reflector</a> a
lot.
</p>
        <p>
My investigation had lead me to some conclusions and even more questions. The main
problem here is that for some creepy reason, the BaseValidator inherits directly from
a Label control!!! Now I have always been aware of this fact but it was until not
long age when it struck me how bad decision it was on Microsoft part. Keeping the
parent class in mind it should be obvious that making a Label display as an image
was not going to be an easy task.
</p>
        <p>
There are few possible ways in which you can make the validator control show an image
instead of text. One of them is providing a img tag as a Text property for the control
and it works, but is very unellegant. I thought that it is time to get to know the
new feature of ASP.NET 2.0 - the Control Adapters.
</p>
        <p>
After a rather long time it came out that it is not possible to use the control adapters
for the validation control since some of the important logic that renders the control
is in the Render method of a BaseValidator class and it cannot be easily replaced.
I have left the adapter path but having learned a lot nonetheless.
</p>
        <p>
Afer yet more time I have realized that the only real solutions are to either create
my own classes implementing the IValidator interface - good there is such a thing
or use some third party controls. I had however no spare time for writing my own fully
blown controls and choosing the right toolset of third party controls is also time
consuming I have decided to take a dirty shortcut.
</p>
        <p>
Below is a listing of all the things I have done in my inherited validator control.
Baasicaly I'm changing the TagKey to img, I'm providing a property for users to specify
the ImageUrl. The rest is to trick the BaseValidator control to render image instead
of a label. The important thing is that the Text property has to be something different
than a null, empty or white-space string - otherwise it is possible that ErrorMessage
will be rendered inside the img tags which is invalid.
</p>
        <p>
          <code>
            <br />
protected override System.Web.UI.HtmlTextWriterTag TagKey<br />
{<br />
get<br />
{<br />
return System.Web.UI.HtmlTextWriterTag.Img;<br />
}<br />
}<br />
public string ImageUrl<br />
{<br />
get { 
<br />
return (string)(ViewState["ImageUrl"] ?? String.Empty); 
<br />
}<br />
set { ViewState["ImageUrl"] = value; }<br />
}<br />
protected override void 
<br />
AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)<br />
{<br />
writer.AddAttribute(<br />
System.Web.UI.HtmlTextWriterAttribute.Src, 
<br />
ResolveClientUrl(ImageUrl));<br />
if (!string.IsNullOrEmpty(Text))<br />
{<br />
writer.AddAttribute(<br />
System.Web.UI.HtmlTextWriterAttribute.Title, Text);<br />
writer.AddAttribute(<br />
System.Web.UI.HtmlTextWriterAttribute.Alt, Text);<br />
}<br />
base.AddAttributesToRender(writer);<br />
}<br />
protected override void 
<br />
RenderContents(System.Web.UI.HtmlTextWriter writer)<br />
{<br />
// to prevent rendering<br />
}<br />
[DefaultValue("Error")]<br />
public override string Text<br />
{<br />
get<br />
{<br />
return (string.IsNullOrEmpty(base.Text) ? "Error" : base.Text);<br />
}<br />
set<br />
{<br />
base.Text = value;<br />
}<br />
}<br /></code>
          <br />
        </p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/06/validation-controls-as-images.html">
            <img alt="kick it on dotnetkicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/06/validation-controls-as-images.html" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=0a9521ef-58cc-4ff3-be7d-06cff1458c6a" />
      </body>
      <title>Validation controls as images - workaround</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,0a9521ef-58cc-4ff3-be7d-06cff1458c6a.aspx</guid>
      <link>http://VaultOfThoughts.net/ValidationControlsAsImagesWorkaround.aspx</link>
      <pubDate>Mon, 07 Aug 2006 08:43:07 GMT</pubDate>
      <description>&lt;p&gt;
I have been trying to make the Validation controls show an image instead of plain
text in case of an error. In doing so I have used &lt;a href="http://www.aisto.com/roeder/dotnet/"&gt;Reflector&lt;/a&gt; a
lot.
&lt;/p&gt;
&lt;p&gt;
My investigation had lead me to some conclusions and even more questions. The main
problem here is that for some creepy reason, the BaseValidator inherits directly from
a Label control!!! Now I have always been aware of this fact but it was until not
long age when it struck me how bad decision it was on Microsoft part. Keeping the
parent class in mind it should be obvious that making a Label display as an image
was not going to be an easy task.
&lt;/p&gt;
&lt;p&gt;
There are few possible ways in which you can make the validator control show an image
instead of text. One of them is providing a img tag as a Text property for the control
and it works, but is very unellegant. I thought that it is time to get to know the
new feature of ASP.NET 2.0 - the Control Adapters.
&lt;/p&gt;
&lt;p&gt;
After a rather long time it came out that it is not possible to use the control adapters
for the validation control since some of the important logic that renders the control
is in the Render method of a BaseValidator class and it cannot be easily replaced.
I have left the adapter path but having learned a lot nonetheless.
&lt;/p&gt;
&lt;p&gt;
Afer yet more time I have realized that the only real solutions are to either create
my own classes implementing the IValidator interface - good there is such a thing
or use some third party controls. I had however no spare time for writing my own fully
blown controls and choosing the right toolset of third party controls is also time
consuming I have decided to take a dirty shortcut.
&lt;/p&gt;
&lt;p&gt;
Below is a listing of all the things I have done in my inherited validator control.
Baasicaly I'm changing the TagKey to img, I'm providing a property for users to specify
the ImageUrl. The rest is to trick the BaseValidator control to render image instead
of a label. The important thing is that the Text property has to be something different
than a null, empty or white-space string - otherwise it is possible that ErrorMessage
will be rendered inside the img tags which is invalid.
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
&lt;br&gt;
protected override System.Web.UI.HtmlTextWriterTag TagKey&lt;br&gt;
{&lt;br&gt;
get&lt;br&gt;
{&lt;br&gt;
return System.Web.UI.HtmlTextWriterTag.Img;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
public string ImageUrl&lt;br&gt;
{&lt;br&gt;
get { 
&lt;br&gt;
return (string)(ViewState["ImageUrl"] ?? String.Empty); 
&lt;br&gt;
}&lt;br&gt;
set { ViewState["ImageUrl"] = value; }&lt;br&gt;
}&lt;br&gt;
protected override void 
&lt;br&gt;
AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)&lt;br&gt;
{&lt;br&gt;
writer.AddAttribute(&lt;br&gt;
System.Web.UI.HtmlTextWriterAttribute.Src, 
&lt;br&gt;
ResolveClientUrl(ImageUrl));&lt;br&gt;
if (!string.IsNullOrEmpty(Text))&lt;br&gt;
{&lt;br&gt;
writer.AddAttribute(&lt;br&gt;
System.Web.UI.HtmlTextWriterAttribute.Title, Text);&lt;br&gt;
writer.AddAttribute(&lt;br&gt;
System.Web.UI.HtmlTextWriterAttribute.Alt, Text);&lt;br&gt;
}&lt;br&gt;
base.AddAttributesToRender(writer);&lt;br&gt;
}&lt;br&gt;
protected override void 
&lt;br&gt;
RenderContents(System.Web.UI.HtmlTextWriter writer)&lt;br&gt;
{&lt;br&gt;
// to prevent rendering&lt;br&gt;
}&lt;br&gt;
[DefaultValue("Error")]&lt;br&gt;
public override string Text&lt;br&gt;
{&lt;br&gt;
get&lt;br&gt;
{&lt;br&gt;
return (string.IsNullOrEmpty(base.Text) ? "Error" : base.Text);&lt;br&gt;
}&lt;br&gt;
set&lt;br&gt;
{&lt;br&gt;
base.Text = value;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/06/validation-controls-as-images.html"&gt;&lt;img alt="kick it on dotnetkicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/06/validation-controls-as-images.html" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=0a9521ef-58cc-4ff3-be7d-06cff1458c6a" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=f4cf1ac1-5715-49d4-beb9-7f4847bd0b09</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,f4cf1ac1-5715-49d4-beb9-7f4847bd0b09.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Some time ago I have posted an <a href="http://vaultofthoughts.net/PathsBuildProvider.aspx"><font color="#6699cc">article</font></a> about
using Build Provider feature of ASP.NET 2.0 to build a strongly typed classes for
accessing the files found in the application virtual directory. I myself have been
using this provider ever since for every Response.Redirect(). I hope that it serves
you as well.
</p>
        <p>
I have found yet two more ways of using the Build Providers to make your applications
safer (the second one I will describe in few days). Today I present to you, the ClassPropertiesBuildProvider.
This time we get a strongly typed way to access name of the properties of our objects.
When this could be usefull? Of course everywhere where we have to specify the name
of the properties using a plain string. And where could that be? Mostly in data-binding
expressions such as Eval and Bind.
</p>
        <p>
Take for example a sample data-binding scenario where we bind a GridView control to
an array of Client objects. Each client has Age, FirstName and LastName:
</p>
        <p>
          <img src="http://vaultofthoughts.net/content/binary/ClassPropertiesBuildProvider1.jpg" border="0" />
        </p>
        <p>
          <img src="http://vaultofthoughts.net/content/binary/ClassPropertiesBuildProvider2.jpg" border="0" />
        </p>
        <p>
As you can see we get the full intellsense over available properties. Keep in mind
that those properties actually return a string containing the name of the property
- exact thing needed to use the Eval or Bind. Notice also that in case some property
changes its name, or is deleted, we get an instant compile time error as oposite to
the runtime exception you get when you use a not strongly typed Eval("Age").
</p>
        <p>
Using the ClassPropertiesBuildProvider is similiar to the PathsBuildProvider. Just
add extension=".cpbp" type="ClassPropertiesBuildProvider.ClassPropertiesBuildProvider"
to a web.config file and put one or more file with .cpbp extension in the App_Code
directory. In this file the first line declares a namespace under which the classes
will be generated ("Classes" in this example). On the following line put the name
of the assemblies you would like to have analyzed.
</p>
        <p>
The solution is available as a source and binary here: <a href="http://vaultofthoughts.net/content/binary/ClassPropertiesBuildProvider.zip">ClassPropertiesBuildProvider.zip
(57,43 KB)</a>.
</p>
        <p>
As with PathsBuildProvider, the provided solution is for demonstration purposes and
as such you should be aware that it might not always work as expected. If you find
some bugs, or wish to provide some improvements, please feel free to do it, and contact
me so I too can benefit from it :-)
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/06/classpropertiesbuildprovider.html">
            <img alt="kick it on dotnetkicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/06/classpropertiesbuildprovider.html" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=f4cf1ac1-5715-49d4-beb9-7f4847bd0b09" />
      </body>
      <title>ClassPropertiesBuildProvider</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,f4cf1ac1-5715-49d4-beb9-7f4847bd0b09.aspx</guid>
      <link>http://VaultOfThoughts.net/ClassPropertiesBuildProvider.aspx</link>
      <pubDate>Thu, 03 Aug 2006 15:59:21 GMT</pubDate>
      <description>&lt;p&gt;
Some time ago I have posted an &lt;a href="http://vaultofthoughts.net/PathsBuildProvider.aspx"&gt;&lt;font color=#6699cc&gt;article&lt;/font&gt;&lt;/a&gt; about
using Build Provider feature of ASP.NET 2.0 to build a strongly typed classes for
accessing the files found in the application virtual directory. I myself have been
using this provider ever since for every Response.Redirect(). I hope that it serves
you as well.
&lt;/p&gt;
&lt;p&gt;
I have found yet two more ways of using the Build Providers to make your applications
safer (the second one I will describe in few days). Today I present to you, the ClassPropertiesBuildProvider.
This time we get a strongly typed way to access name of the properties of our objects.
When this could be usefull? Of course everywhere where we have to specify the name
of the properties using a plain string. And where could that be? Mostly in data-binding
expressions such as Eval and Bind.
&lt;/p&gt;
&lt;p&gt;
Take for example a sample data-binding scenario where we bind a GridView control to
an array of Client objects. Each client has Age, FirstName and LastName:
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://vaultofthoughts.net/content/binary/ClassPropertiesBuildProvider1.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://vaultofthoughts.net/content/binary/ClassPropertiesBuildProvider2.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
As you can see we get the full intellsense over available properties. Keep in mind
that those properties actually return a string containing the name of the property
- exact thing needed to use the Eval or Bind. Notice also that in case some property
changes its name, or is deleted, we get an instant compile time error as oposite to
the runtime exception you get when you use a not strongly typed Eval("Age").
&lt;/p&gt;
&lt;p&gt;
Using the ClassPropertiesBuildProvider is similiar to the PathsBuildProvider. Just
add extension=".cpbp" type="ClassPropertiesBuildProvider.ClassPropertiesBuildProvider"
to a web.config file and put one or more file with .cpbp extension in the App_Code
directory. In this file the first line declares a namespace under which the classes
will be generated ("Classes" in this example). On the following line put the name
of the assemblies you would like to have analyzed.
&lt;/p&gt;
&lt;p&gt;
The solution is available as a source and binary here:&amp;nbsp;&lt;a href="http://vaultofthoughts.net/content/binary/ClassPropertiesBuildProvider.zip"&gt;ClassPropertiesBuildProvider.zip
(57,43 KB)&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
As with PathsBuildProvider, the provided solution is for demonstration purposes and
as such you should be aware that it might not always work as expected. If you find
some bugs, or wish to provide some improvements, please feel free to do it, and contact
me so I too can benefit from it :-)
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/06/classpropertiesbuildprovider.html"&gt;&lt;img alt="kick it on dotnetkicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/06/classpropertiesbuildprovider.html" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=f4cf1ac1-5715-49d4-beb9-7f4847bd0b09" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=736d34d6-d424-4d31-a926-aa4472601035</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,736d34d6-d424-4d31-a926-aa4472601035.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
While working on MyDataSourceControl (see my other articles) I have found an interesting
thing. It appears that when properties of a control are set to the values provided
as attributes in the aspx file, the control is not yet very functional. The most important
thing that my be not set is the Parent and other control-tree related properties of
the control! It seams however that it is not always so. I may be mistaken, but from
my observation it is possible that sometimes the Parent property will be set before
the properties defined in the aspx. If somebody can shed some light on this I will
be grateful.
</p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=736d34d6-d424-4d31-a926-aa4472601035" />
      </body>
      <title>When it isn't safe to use the Parent property of a control</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,736d34d6-d424-4d31-a926-aa4472601035.aspx</guid>
      <link>http://VaultOfThoughts.net/WhenItIsntSafeToUseTheParentPropertyOfAControl.aspx</link>
      <pubDate>Thu, 03 Aug 2006 15:53:45 GMT</pubDate>
      <description>&lt;p&gt;
While working on MyDataSourceControl (see my other articles) I have found an interesting
thing. It appears that when properties of a control are set to the values provided
as attributes in the aspx file, the control is not yet very functional. The most important
thing that my be not set is the Parent and other control-tree related properties of
the control! It seams however that it is not always so. I may be mistaken, but from
my observation it is possible that sometimes the Parent property will be set before
the properties defined in the aspx. If somebody can shed some light on this I will
be grateful.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=736d34d6-d424-4d31-a926-aa4472601035" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=54b235bb-d322-4bb8-899f-838aac895734</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,54b235bb-d322-4bb8-899f-838aac895734.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When developing stateless applications sooner or later there comes a time, when you
need to store some information between requests. In technologies such as PHP and ASP.NET
there is a special object to serve this function. It is called the Session object
in ASP.NET. Using it is very easy, just use an indexer in a form of a string to set
and retrieve some information that you want to persist between requests. Now the main
problem with such an approach is the fact that it is hard to control. As an example:
suppose that there are two pages using the Session object to store some state and
make it accessible to each other. The simplies approach is to just use the Session
object on one page to store an object and on the other to retrieve it:
</p>
        <p>
          <strong>Page1.aspx</strong>
          <br />
Session["SomeKey"] = "some value"; 
</p>
        <p>
          <strong>Page2.aspx</strong>
          <br />
object value = Session["SomeKey"]; 
</p>
        <p>
Suppose further that there are many pages that access the value with "SomeKey" key.
Sooner or later (sooner) you will lose a track of where and what is setting and retrieving
this value. More over, it is hard to change since it requires you to look through
multiple files - a very error prone technique.
</p>
        <p>
Another solution would be to use some kind of public constant variable in place of
the "SomeKey". It is better but not perfect.
</p>
        <p>
A more secure solution would be to create a strongly typed class for accessing the
Session object by means of properties. This way you only have to change session related
stuff in one place and every braking change is detected at the compile time. Additionaly,
when working with other developers, they do not have to ask you, what kind of object
is available through the Session[“SomeKey”].
</p>
        <p>
As a side note I would strongly encourage you to avoid Session object whenever you
can. It is a hell to work with a statefull statless application :-(
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/06/strongly-typed-session-object.html">
            <img alt="kick it on dotnetkicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/06/strongly-typed-session-object.html" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=54b235bb-d322-4bb8-899f-838aac895734" />
      </body>
      <title>Strongly typed Session object</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,54b235bb-d322-4bb8-899f-838aac895734.aspx</guid>
      <link>http://VaultOfThoughts.net/StronglyTypedSessionObject.aspx</link>
      <pubDate>Thu, 03 Aug 2006 15:52:46 GMT</pubDate>
      <description>&lt;p&gt;
When developing stateless applications sooner or later there comes a time, when you
need to store some information between requests. In technologies such as PHP and ASP.NET
there is a special object to serve this function. It is called the Session object
in ASP.NET. Using it is very easy, just use an indexer in a form of a string to set
and retrieve some information that you want to persist between requests. Now the main
problem with such an approach is the fact that it is hard to control. As an example:
suppose that there are two pages using the Session object to store some state and
make it accessible to each other. The simplies approach is to just use the Session
object on one page to store an object and on the other to retrieve it:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Page1.aspx&lt;/strong&gt;
&lt;br&gt;
Session["SomeKey"] = "some value"; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Page2.aspx&lt;/strong&gt;
&lt;br&gt;
object value = Session["SomeKey"]; 
&lt;/p&gt;
&lt;p&gt;
Suppose further that there are many pages that access the value with "SomeKey" key.
Sooner or later (sooner) you will lose a track of where and what is setting and retrieving
this value. More over, it is hard to change since it requires you to look through
multiple files - a very error prone technique.
&lt;/p&gt;
&lt;p&gt;
Another solution would be to use some kind of public constant variable in place of
the "SomeKey". It is better but not perfect.
&lt;/p&gt;
&lt;p&gt;
A more secure solution would be to create a strongly typed class for accessing the
Session object by means of properties. This way you only have to change session related
stuff in one place and every braking change is detected at the compile time. Additionaly,
when working with other developers, they do not have to ask you, what kind of object
is available through the Session[“SomeKey”].
&lt;/p&gt;
&lt;p&gt;
As a side note I would strongly encourage you to avoid Session object whenever you
can. It is a hell to work with a statefull statless application :-(
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/06/strongly-typed-session-object.html"&gt;&lt;img alt="kick it on dotnetkicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/06/strongly-typed-session-object.html" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=54b235bb-d322-4bb8-899f-838aac895734" /&gt;</description>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://vaultofthoughts.net/Trackback.aspx?guid=ff4c4ed0-8481-4ecf-b4a6-381d7cec1fdf</trackback:ping>
      <pingback:server>http://vaultofthoughts.net/pingback.aspx</pingback:server>
      <pingback:target>http://vaultofthoughts.net/PermaLink,guid,ff4c4ed0-8481-4ecf-b4a6-381d7cec1fdf.aspx</pingback:target>
      <dc:creator>Michal Talaga</dc:creator>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
There comes a time when you have to develop a page which allows its users to go back
to the page they came from. Keeping in mind the statlessnes of the web applications,
it is hard to tell where our users came from since every page is (or at least should
be) working on its own. Suppose that on one of the pages we want to put a kind of
“Cancel” button which takes users back. One possible way of doing it would be to use
some client-side JavaScript such as history.back(). What if for some reason the use
of JavaScript is inacceptable?
</p>
        <p>
We are left with the server-side code. But there is a problem. On the server, there
is no way of telling the previous page. We have to create our own solution. There
are couple of possibilities here. You can use a Session or Profile object and every
time a page is loaded put some information there so we have a way of checking where
the user was before. You can even use a Stack object to make it possible to drill
down and up as you like. This solution has however an impact on the server resources
(memory occupied by the session object). Another solution uses the query url to pass
the returnUrl address of page from which the users came. In fact this pattern is used
every time you try to access a page which requires authorization and you are redirected
to a login page. This way you save some valuable server resources and make your solution
a lot more scalable.
</p>
        <p>
          <a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/06/returnurl-pattern.html">
            <img alt="kick it on dotnetkicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/06/returnurl-pattern.html" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=ff4c4ed0-8481-4ecf-b4a6-381d7cec1fdf" />
      </body>
      <title>The returnUrl pattern</title>
      <guid isPermaLink="false">http://vaultofthoughts.net/PermaLink,guid,ff4c4ed0-8481-4ecf-b4a6-381d7cec1fdf.aspx</guid>
      <link>http://VaultOfThoughts.net/TheReturnUrlPattern.aspx</link>
      <pubDate>Thu, 03 Aug 2006 15:44:18 GMT</pubDate>
      <description>&lt;p&gt;
There comes a time when you have to develop a page which allows its users to go back
to the page they came from. Keeping in mind the statlessnes of the web applications,
it is hard to tell where our users came from since every page is (or at least should
be) working on its own. Suppose that on one of the pages we want to put a kind of
“Cancel” button which takes users back. One possible way of doing it would be to use
some client-side JavaScript such as history.back(). What if for some reason the use
of JavaScript is inacceptable?
&lt;/p&gt;
&lt;p&gt;
We are left with the server-side code. But there is a problem. On the server, there
is no way of telling the previous page. We have to create our own solution. There
are couple of possibilities here. You can use a Session or Profile object and every
time a page is loaded put some information there so we have a way of checking where
the user was before. You can even use a Stack object to make it possible to drill
down and up as you like. This solution has however an impact on the server resources
(memory occupied by the session object). Another solution uses the query url to pass
the returnUrl address of page from which the users came. In fact this pattern is used
every time you try to access a page which requires authorization and you are redirected
to a login page. This way you save some valuable server resources and make your solution
a lot more scalable.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://mikeoff.blogspot.com/2006/06/returnurl-pattern.html"&gt;&lt;img alt="kick it on dotnetkicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://mikeoff.blogspot.com/2006/06/returnurl-pattern.html" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://vaultofthoughts.net/aggbug.ashx?id=ff4c4ed0-8481-4ecf-b4a6-381d7cec1fdf" /&gt;</description>
      <category>ASP.NET</category>
    </item>
  </channel>
</rss>