<?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>RANDOM SEED :: richard finn &#187; ASP</title>
	<atom:link href="http://www.random-seed.com/tag/asp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.random-seed.com</link>
	<description>Random thoughts on programming, finance, and art.</description>
	<lastBuildDate>Sat, 28 Jan 2012 00:00:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ASP.Net User Control &#8216;does not exist in the current context&#8217;</title>
		<link>http://www.random-seed.com/2009/07/15/asp-net-user-control-field-does-not-exist-in-the-current-context/</link>
		<comments>http://www.random-seed.com/2009/07/15/asp-net-user-control-field-does-not-exist-in-the-current-context/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 22:27:59 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[error]]></category>

		<guid isPermaLink="false">http://www.random-seed.com/?p=34</guid>
		<description><![CDATA[I may be missing something, but a search around the Internet revealed several people having the same problem and not finding any good answers.  You have an ASP.Net user control with fields on it, but when you compile you get an error.  Intellisence knows the objects are there, but the compiler doesn&#8217;t.  What do you [...]]]></description>
			<content:encoded><![CDATA[<p>I may be missing something, but a search around the Internet revealed several people having the same problem and not finding any good answers.  You have an ASP.Net user control with fields on it, but when you compile you get an error.  Intellisence knows the objects are there, but the compiler doesn&#8217;t.  What do you do?</p>
<p><span id="more-34"></span>Like I said, I might be wrong &#8211; but after a lot of trial and error, and error, and error I settled upon this fix.  The error comes from the fact that the <em>.ascx.cs</em> file is compiled <em><strong>before</strong></em> the <em>.ascx</em> page.  So, if could could compile them separately it will actually work (I know, I managed to accomplish this by accident).</p>
<p>What does not work is to declare the form controls as protected variables in the code behind.  If you set them in place in the <em>.ascx</em> file, then they have already been declared and initialized.  However, if you did not place them in <em>.ascx</em> page, you could add them programatically at run time from the code behind and that would work &#8211; it&#8217;s not pretty, and to be avoided if you can, but it will work (assuming you know when you&#8217;re developing this control that the layout of the form controls is static, if its dynamic then please add them programatically).  I also tried data binding, which is likely the official solution &#8211; but I ran into problems along that route as well.</p>
<p>If you&#8217;re altering an <em>.aspx</em> page, make sure to change to <em>&lt;%@ Page</em> directive to a <em>&lt;%@ Control</em> directive, such as:</p>
<pre>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="myControl.ascx.cs" Inheirits="myControl" %&gt;</pre>
<p>What I then did was to add string fields to hold my values as protected members in the code behind:</p>
<pre>#region form strings
protected String firstName = "";
protected String lastName = "";
protected String phoneNumber = "";
protected String emailAddress = "";
#endregion</pre>
<p>The function that did pull the contents from the form variables, formerly called <em>btnSubmit_Click()</em>,  instead uses the holder variables and is now called <em>processForm()</em>.  The submit event handler is then created in the .ascx file directly:</p>
<pre>&lt;script language="cs" runat="server"&gt;
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        this.firstName = txtfName.Text;
        this.lastName = txtLname.Text;
        this.phoneNumber = txtPhone.Text;
        this.emailAddress = txtEmail.Text;

        this.processForm();
    }
&lt;/script&gt;</pre>
<p>There you have it.  Maybe not the most elegant, but when you&#8217;re hitting your head on the wall, this will save you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.random-seed.com/2009/07/15/asp-net-user-control-field-does-not-exist-in-the-current-context/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

