<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Moving the Curve &#187; c#</title>
	<atom:link href="http://www.stevideter.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stevideter.com</link>
	<description>Technology, code, and thoughts by Stevi Deter</description>
	<lastBuildDate>Fri, 16 Apr 2010 17:01:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Picking the right .NET Framework Version with Specific Reference</title>
		<link>http://www.stevideter.com/2008/08/14/picking-the-right-net-framework-version-with-specific-reference/</link>
		<comments>http://www.stevideter.com/2008/08/14/picking-the-right-net-framework-version-with-specific-reference/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 17:38:02 +0000</pubDate>
		<dc:creator>stevi</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[csharp]]></category>

		<guid isPermaLink="false">http://www.stevideter.com/?p=26</guid>
		<description><![CDATA[This morning I needed to work on a long-standing ASP.NET web application project. I haven&#8217;t had to run it locally for quite a while. I updated my source from TFS, cleaned and built the solution, and started the project in debug mode. After logging in, I went to a page, and was suddenly staring at [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>This morning I needed to work on a long-standing ASP.NET web application project. I haven&#8217;t had to run it locally for quite a while. I updated my source from TFS, cleaned and built the solution, and started the project in debug mode. After logging in, I went to a page, and was suddenly staring at an unexpected and confusing exception:</p>
<pre>The base class includes the field 'ScriptManager1',
but its type (System.Web.UI.ScriptManager)
is not compatible with the type of control
(System.Web.UI.ScriptManager).</pre>
<p>
I put aside the surprise that a type was not compatible with itself, and checked the source control history. The specific page hadn&#8217;t been changed since the last time I&#8217;d tried it. I know the project is under active development, including a recent deployment of the latest version at the client site, so I had ever reason to believe the build should work. </p>
<p>Before sending a panicked email to my team, I did a quick Google search. I found a couple links of similar people with this problem in VS2008, but I&#8217;m still running VS2005. </p>
<p>I realized, however, that I had installed Framework versions 3.0 and 3.5 since I had last had to work on this project. I went to the project references, and checked that for System.Web.Extensions, and noticed that &#8220;Specific Reference&#8221; was set to false. I set this to true, rebuilt the project, and started the debugger again. </p>
<p>This time, success! The page displayed as expected. </p>
<p>What problems have you encountered from having multiple .NET Frameworks installed? </p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.stevideter.com/2008/08/14/picking-the-right-net-framework-version-with-specific-reference/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Why Are All My Collection Elements the Same as the Last One I Added?</title>
		<link>http://www.stevideter.com/2008/04/29/why-are-all-my-collection-elements-the-same-as-the-last-one-i-added/</link>
		<comments>http://www.stevideter.com/2008/04/29/why-are-all-my-collection-elements-the-same-as-the-last-one-i-added/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 00:08:26 +0000</pubDate>
		<dc:creator>stevi</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[pass by value]]></category>
		<category><![CDATA[set]]></category>

		<guid isPermaLink="false">http://www.stevideter.com/?p=20</guid>
		<description><![CDATA[I&#8217;ve seen several programmers struggle with a similar question. They create a collection of some sort, and add items to it in a loop. When they finish the loop and try to use the collection (or list, or array, or set, or map) it looks like every single object in the collection is the same [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen several programmers struggle with a similar question. They create a collection of some sort, and add items to it in a loop. When they finish the loop and try to use the collection (or list, or array, or set, or map) it looks like every single object in the collection is the same one &#8212; the last one added to the array. Usually they&#8217;ve written code that looks something like this C# example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> Stevideter.<span style="color: #0000FF;">ArrayTest</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Program
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
&nbsp;
            List<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span> people <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Person person <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                person.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Person {0}&quot;</span>, i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                person.<span style="color: #0000FF;">Age</span> <span style="color: #008000;">=</span> i <span style="color: #008000;">*</span> i<span style="color: #008000;">;</span>
                people.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>person<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Person person1 <span style="color: #0600FF;">in</span> people<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Console.<span style="color: #0600FF;">Out</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>person1<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #FF0000;">class</span> Person
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">String</span> name<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Name
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> name<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> name <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> age<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Age
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> age<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> age <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0} is {1}&quot;</span>,name,age<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>What happened? Why is the output:</p>
<pre>Person 2 is 4
Person 2 is 4
Person 2 is 4</pre>
<p>The answer is in the mystery that is parameter passing by value.</p>
<p>In both Java and C#, parameters are passed by value. But what this means for objects is not what you may intuitively expect. When you pass an object reference to a parameter, the value that is passed is, in fact, a copy of the reference, not a copy of the object that is referenced. Both the original reference and the value copy point to the same object. That is why you can update the members of an object in a method that you pass it to &#8211; you have a copy of the address where that object lives on the heap.</p>
<p>In the code above, only one Person is created. Each trip through the loop, the members of<br />
<i>person</i> are updated. When <i>people.Add(person)</i> is called, the reference to the object created by <i>new Person()</i> is copied and added to the List people.</p>
<p>When you change the object&#8217;s values the next time through, every reference to that object sees the changes, because they all still point to the same instance on the heap. This is why at the end, every item in the array has the most recent updates. The code, as usual, is doing exactly what you told it to do. </p>
<p>Instead, you need to create a new object for each object you want in the array, and add that new reference to the array. This time, let&#8217;s see it in Java: </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.stevideter.java</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ArrayTest <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            person.<span style="color: #006633;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Person %d&quot;</span>, i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            person.<span style="color: #006633;">setAge</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">*</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            people.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Person person <span style="color: #339933;">:</span> people<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> age<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getAge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> age<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAge<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> age<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">age</span> <span style="color: #339933;">=</span> age<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%s is %d&quot;</span>, name, age<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
This time we get the results we probably expected:</p>
<pre>
Person 0 is 0
Person 1 is 1
Person 2 is 4
</pre>
<p>
At this point, you might be confused by the issue of scope, and feel you must declare and instantiate the object outside of the loop, afraid of losing the object when the loop ends.</p>
<p>What is actually lost when we leave the for loop is access to the reference <i>person</i>.<br />
But the references to the heap location for the object created by each call to <i>new Person()</i> still exist. So the objects are still in the heap, and can still be used via the array/collection to which they were added.</p>
<p>I know I still get caught now and again by the implications of pass by value in Java and C#. When is the last time it surprised you?</p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.stevideter.com/2008/04/29/why-are-all-my-collection-elements-the-same-as-the-last-one-i-added/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Role aware ASP.NET web controls</title>
		<link>http://www.stevideter.com/2008/03/12/role-aware-aspnet-web-controls/</link>
		<comments>http://www.stevideter.com/2008/03/12/role-aware-aspnet-web-controls/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 01:09:00 +0000</pubDate>
		<dc:creator>stevi</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[role]]></category>
		<category><![CDATA[webcontrol]]></category>

		<guid isPermaLink="false">http://www.stevideter.com/2008/03/12/role-aware-aspnet-web-controls/</guid>
		<description><![CDATA[A common feature I&#8217;ve found necessary for web applications is role-based security at the field level. For example, I may have a form that allows editing a user&#8217;s information. The requirements may include the rule that only certain types (roles) may edit certain fields. This can be accomplished in myriad ways. You can create a [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>A common feature I&#8217;ve found necessary for web applications is role-based security at the field level. For example, I may have a form that allows editing a user&#8217;s information. The requirements may include the rule that only certain types (roles) may edit certain fields.</p>
<p>This can be accomplished in myriad ways. You can create a different form per role, and direct the user to the appropriate form. Or you can put specific logic around each relevant field to show, enable, or hide each field.</p>
<p>Both these routes can lead to a lot of extra coding, and even more maintenance. I&#8217;d like to be able to tell each affected field to display itself correctly based on the user&#8217;s role.</p>
<p>The Microsoft.NET Framework 2.0 introduced the Membership and Roles framework and the Microsoft Provider Model, which gives us an API-based way to  interface with users and roles. If you use a RoleManager (either one of the defaults provided or your own custom implementation), the following code gives an example of how you can extend the existing WebControls to render themselves based on a current user&#8217;s roles.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web.UI.WebControls</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">namespace</span> RoleAwareServerControls
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #008080; font-style: italic;">/// Role Aware Text Box. Will only render as Enabled if user is</span>
    <span style="color: #008080; font-style: italic;">/// in the in the RoleName.</span>
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #000000;">&#91;</span>DefaultProperty<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Text&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ToolboxData<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;{0}:RoleAwareTextBox runat=server RoleName=user&gt;&lt;/{0}:RoleAwareTextBox&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> RoleAwareTextBox <span style="color: #008000;">:</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span></span>.<span style="color: #0000FF;">TextBox</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> visibleIfUnauthorized<span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #008080; font-style: italic;">/// if user is not in role, should the field be displayed at all?</span>
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> VisibleIfUnauthorized
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> visibleIfUnauthorized<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> visibleIfUnauthorized <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">String</span> roleName<span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #008080; font-style: italic;">/// if user is in this role, enable this field, else not</span>
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> RoleName
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> roleName<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> roleName <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #008080; font-style: italic;">/// determine if TextBox should be Enabled or Visible</span>
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #008080; font-style: italic;">///An EventArgs</span>
       <span style="color: #008080; font-style: italic;">/// that contains the event data.</span>
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnPreRender<span style="color: #000000;">&#40;</span>EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnPreRender</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// assume normal display if RoleName not set</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>roleName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Enabled <span style="color: #008000;">=</span>
                    <span style="color: #000000;">&#40;</span>Page.<span style="color: #0000FF;">User</span>.<span style="color: #0000FF;">Identity</span>.<span style="color: #0000FF;">IsAuthenticated</span> <span style="color: #008000;">&amp;&amp;</span> Page.<span style="color: #0000FF;">User</span>.<span style="color: #0000FF;">IsInRole</span><span style="color: #000000;">&#40;</span>roleName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Visible <span style="color: #008000;">=</span> Enabled <span style="color: #008000;">?</span> <span style="color: #0600FF;">true</span> <span style="color: #008000;">:</span> visibleIfUnauthorized<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>To use the new web control, register it with your page (or for the whole application in your web.config) and add it to the page:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Site.master&quot; 
    AutoEventWireup=&quot;true&quot; CodeFile=&quot;Default.aspx.cs&quot; 
    Inherits=&quot;_Default&quot; Theme=&quot;Default&quot; %&gt;
&lt;%@ Register TagPrefix=&quot;rasc&quot; Namespace=&quot;RoleAwareServerControls&quot; %&gt;
&lt;body&gt;
    &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
        &lt;rasc:RoleAwareTextBox runat=&quot;server&quot; ID=&quot;textBox&quot;
            RoleName=&quot;administrator&quot; VisibleIfUnauthorized=&quot;true&quot; &gt;
            Hi, administrator&lt;/rasc:RoleAwareTextBox&gt;
    &lt;/form&gt;
&lt;/body&gt;</pre></div></div>



<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.stevideter.com/2008/03/12/role-aware-aspnet-web-controls/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
