<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>AaronHardy.com &#187; ASP.NET</title>
	<atom:link href="http://aaronhardy.com/category/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://aaronhardy.com</link>
	<description>For all your Aaron Hardy needs.</description>
	<lastBuildDate>Fri, 20 Apr 2012 14:53:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Maintaining scroll position in ASP.NET 2.0</title>
		<link>http://aaronhardy.com/aspnet/maintaining-scroll-position-in-aspnet-20/</link>
		<comments>http://aaronhardy.com/aspnet/maintaining-scroll-position-in-aspnet-20/#comments</comments>
		<pubDate>Mon, 20 Aug 2007 20:12:37 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[scrolling]]></category>

		<guid isPermaLink="false">http://intimateconversations.aaronhardy.com/aspnet/maintaining-scroll-position-in-aspnet-20/</guid>
		<description><![CDATA[While I have often vented my frustrations with how difficult it can be working with DHTML in ASP.NET, Microsoft has taken a great step in helping developers make the transition to their primarily postback-based framework. To explain, one reason why I love DHTML so much is that I hate having to post back to the [...]]]></description>
			<content:encoded><![CDATA[<p>While I have often vented my frustrations with how difficult it can be working with DHTML in ASP.NET, Microsoft has taken a great step in helping developers make the transition to their primarily postback-based framework.  To explain, one reason why I <i>love</i> DHTML so much is that I hate having to post back to the server just to dynamically add/remove a text box or show/hide a particular div.  What makes posting back worse is that when you get the regenerated page, the user&#8217;s browser jumps back to the top of the page.<span id="more-22"></span>  In other words, if my page has a bunch of content, the user has scrolled down and is looking at the bottom of the page, and then the user clicks some link or button that has to post back to the server to regenerate the same page for just a slight user interface modification, the user&#8217;s browser jumps back to the top of the page.  It&#8217;s really pretty annoying.  Well if you don&#8217;t want to jump through all the hoops of using DHTML in .NET but you don&#8217;t want to drive your users crazy, you should quickly become acquainted to a property new to .NET 2.0: <i>MaintainScrollPositionOnPostback</i></p>
<p>Here&#8217;s how it&#8217;s implemented:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;">&lt;%@ Page MaintainScrollPositionOnPostback<span style="color: #008000;">=</span><span style="color: #808080;">&quot;true&quot;</span> ... %&gt;</pre></td></tr></table></div>

<p>That&#8217;s it.  Just add the property to your page directive and every time there&#8217;s a postback the user&#8217;s scroll position will stay put.  This makes for a much more seamless interaction for your users.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/aspnet/maintaining-scroll-position-in-aspnet-20/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DBNull can&#8217;t be converted to Null?  .NET 2.0 drops the ball on Nullable Types</title>
		<link>http://aaronhardy.com/aspnet/dbnull-cant-be-converted-to-null-net-20-drops-the-ball-on-nullable-types/</link>
		<comments>http://aaronhardy.com/aspnet/dbnull-cant-be-converted-to-null-net-20-drops-the-ball-on-nullable-types/#comments</comments>
		<pubDate>Thu, 28 Jun 2007 00:25:41 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[dbnull]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[null-checking]]></category>

		<guid isPermaLink="false">http://intimateconversations.aaronhardy.com/uncategorized/dbnull-cant-be-converted-to-null-net-20-drops-the-ball-on-nullable-types/</guid>
		<description><![CDATA[Recommended Reading: Nullable Generic Structure One giant leap for mankind that Microsoft proposed for .NET 2.0 (specifically VB) was the concept of Nullable types. Unfortunately, reportedly due to time constraints, Microsoft was unable to implement full support for them. To understand the concept, you can just google &#8220;Nullable Types&#8221; or check out the link above. [...]]]></description>
			<content:encoded><![CDATA[<p><b>Recommended Reading:</b><br />
<a href="http://msdn2.microsoft.com/en-us/library/b3h38hb0.aspx" target="_blank">Nullable Generic Structure</a></p>
<p>One giant leap for mankind that Microsoft proposed for .NET 2.0 (specifically VB) was the concept of Nullable types.  Unfortunately, reportedly due to time constraints, Microsoft was unable to implement full support for them. To understand the concept, you can just google &#8220;Nullable Types&#8221; or check out the link above.  I&#8217;ll give you the quick, trimmed-down version.<span id="more-17"></span></p>
<p>In .NET, there are two types of variables: value types and reference types.  Integers, booleans, dates, and others fall into the value type category, while strings, objects, and others fall into the reference type category.  Reference types can be null (or &#8220;nothing&#8221; in VB), while value types cannot.  In other words, if you have an integer, it has to be <i>something</i>, so some people initially set it to a &#8220;magic number&#8221; like -1 so they know it&#8217;s not a value with meaning&#8211;it&#8217;s more of a default value.  This becomes problematic because, when you go to store the values of a business object into a database, you have two options, neither of which are pretty:</p>
<ol>
<li>Have a bunch of -1&#8242;s sitting in the database, which is ugly and takes up space
<li>Check every variable before it goes into the database and see if it&#8217;s set to your magic number.  If it is, you then substitute the -1 or whatever it is with DBNull.
</ol>
<p>So what do we do?  Behold, the power of nullables.  Basically, if I declare my variable like so&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Dim</span> foofarginbar <span style="color: #FF8000;">As</span> Nullable<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Of</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>&#8230;I can then set my variable to null (nothing) without any problems.  That way, when I go to save my business object into the database, I don&#8217;t have to worry about checking any &#8220;magic number&#8221; because, hey, it&#8217;s null, and it&#8217;ll be null in the database.</p>
<p>Oh but wait!  How did Microsoft <i>ever</i> drop the ball on reading values <i>from</i> a database? Check this out.  With what I have explained in mind, you would think this would work just fine:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Dim</span> foofarginbar <span style="color: #FF8000;">As</span> Nullable<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Of</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
foofarginbar <span style="color: #008000;">=</span> myTableRow.<span style="color: #0000FF;">SomeIntegerColumn</span></pre></td></tr></table></div>

<p>If the value from myTableRow.SomeIntegerColumn is null (in this case it would actually be of type DBNull), you would think .NET would have no problem setting it to a <i>nullable</i> variable.  I mean, a nullable variable is getting set to null&#8230;not a big deal right?  Wrong.  .NET 2.0 drops the ball.  Instead, we have to <i>still</i> check for a null value and act accordingly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Dim</span> foofarginbar <span style="color: #FF8000;">As</span> Nullable<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Of</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">If</span> IsDBNull<span style="color: #000000;">&#40;</span>myTableRow<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SomeIntegerColumn&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span>
    foofarginbar <span style="color: #008000;">=</span> <span style="color: #FF8000;">Nothing</span>
<span style="color: #FF8000;">Else</span>
    foofarginbar <span style="color: #008000;">=</span> myTableRow.<span style="color: #0000FF;">SomeIntegerColumn</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></pre></td></tr></table></div>

<p>I&#8217;m amazed this was left out of .NET 2.0 as I think it&#8217;s a <i>huge</i> component of nullable types.  Without it, some developers still may be hesitant to adopt the whole idea quite yet.</p>
<p>So, what do you think?  Please&#8230;join the intimate conversation.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/aspnet/dbnull-cant-be-converted-to-null-net-20-drops-the-ball-on-nullable-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing Columns in the Visual Studio DataSet Designer</title>
		<link>http://aaronhardy.com/aspnet/changing-columns-in-the-visual-studio-dataset-designer/</link>
		<comments>http://aaronhardy.com/aspnet/changing-columns-in-the-visual-studio-dataset-designer/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 01:03:23 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[dataset]]></category>
		<category><![CDATA[dataset designer]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://intimateconversations.aaronhardy.com/aspnet/changing-columns-in-the-visual-studio-dataset-designer/</guid>
		<description><![CDATA[Recommended Reading: New DataSet Features in Visual Studio 2005 New to Visual Studio 2005, Microsoft has included the new DataSet Designer that is supposed to automate many of our data access tasks and to replace most, if not all, of the data layer in N-tier applications. To be honest though, I think they missed the [...]]]></description>
			<content:encoded><![CDATA[<p><b>Recommended Reading:</b><br />
<a href="http://msdn2.microsoft.com/en-us/library/ms379590(VS.80).aspx" target="_blank">New DataSet Features in Visual Studio 2005</a></p>
<p>New to Visual Studio 2005, Microsoft has included the new DataSet Designer that is supposed to automate many of our data access tasks and to replace most, if not all, of the data layer in N-tier applications.  To be honest though, I think they missed the boat on convenience.<span id="more-15"></span></p>
<p>As a quick intro, here&#8217;s a sample screenshot of DataSet Designer:<br />
<a href='/wp-content/uploads/2007/06/dataset_designer_screenshot.gif' title='DataSet Designer Screenshot'><img src='/wp-content/uploads/2007/06/dataset_designer_screenshot.thumbnail.gif' alt='DataSet Designer Screenshot' /></a></p>
<p>Today, for example, it turned out that I needed to modify the name and datatype of a column in one of my database tables.  It can&#8217;t be <i>that</i> hard, right? Wrong.  Maybe there&#8217;s a better way, but just to change my one, single, measly column name  and datatype, here&#8217;s what I had to do as far as the DataSet Designer goes (in other words, excluding code changes in other application layers as well as the initial change in the database)</p>
<ol>
<li>With the column name selected on the dataset, change the &#8220;Name&#8221; property to match the new column name.
<li>With the column name selected on the dataset, change the &#8220;Source&#8221; property to match the new column name.
<li>With the column name selected on the dataset, change the &#8220;DataType&#8221; property to match the new column name.
<li>With the column name selected on the dataset, update the &#8220;MaxLength&#8221; property to match the new column datatype if needed
<li>With the column name selected on the dataset, update the &#8220;MaxLength&#8221; property to match the new column datatype if needed
<li>With the tableadapter selected, in the properties window, hit the plus sign next to &#8220;DeleteCommand,&#8221; highlight the &#8220;Parameters&#8221; field, click the ellipses next to where it says &#8220;(Collection),&#8221; select the column name, then change the DbType, ColumnName, Size, SourceColumn, ParameterName, and ProviderType columns.
<li>With the tableadapter selected, in the properties window, hit the plus sign next to &#8220;InsertCommand,&#8221; highlight the &#8220;Parameters&#8221; field, click the ellipses next to where it says &#8220;(Collection),&#8221; select the column name, then change the DbType, ColumnName, Size, SourceColumn, ParameterName, and ProviderType columns.
<li>With the tableadapter selected, in the properties window, hit the plus sign next to &#8220;UpdateCommand,&#8221; highlight the &#8220;Parameters&#8221; field, click the ellipses next to where it says &#8220;(Collection),&#8221; select the column name, then change the DbType, ColumnName, Size, SourceColumn, ParameterName, and ProviderType columns.
<li>With the tableadapter selected, in the properties window, hit the plus sign next to &#8220;DeleteCommand,&#8221; highlight the &#8220;CommandText&#8221; field, click the ellipses, and modify the statement to match the new column name.
<li>With the tableadapter selected, in the properties window, hit the plus sign next to &#8220;InsertCommand,&#8221; highlight the &#8220;CommandText&#8221; field, click the ellipses, and modify the statement to match the new column name.
<li>With the tableadapter selected, in the properties window, hit the plus sign next to &#8220;UpdateCommand,&#8221; highlight the &#8220;CommandText&#8221; field, click the ellipses, and modify the statement to match the new column name.
<li>If you want to keep things consistent, repeat <b>all previous steps</b> for <b>every</b> table that has a foreign key pointing to our modified column.  In my case, that was two more tables.
</ol>
<p>Crazy?  Well, considering it would still be a lot of work if we <i>didn&#8217;t</i> have the DataSet Designer, not too crazy.  Actually&#8230;yeah, it is.  I&#8217;d rather not require Carpal Tunnel therapy every time I change a column name.  Going to the underlying XML code (right-click in the DataSet Designer and click View Code) and doing a quick find and replace helps in changing the name where needed, but it can be a risky move and you&#8217;re still left with manually changing the datatype.  Am I the only one that thinks the designer is crazy ridiculous when it comes to modifying what&#8217;s already on the canvas?</p>
<p>So, <i>is</i> there a better way?  As you as frustrated as I am?  Please&#8230;. join my intimate conversation.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/aspnet/changing-columns-in-the-visual-studio-dataset-designer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dynamic Controls and Managing ViewState</title>
		<link>http://aaronhardy.com/aspnet/dynamic-controls-and-managing-viewstate/</link>
		<comments>http://aaronhardy.com/aspnet/dynamic-controls-and-managing-viewstate/#comments</comments>
		<pubDate>Sun, 24 Jun 2007 16:07:25 +0000</pubDate>
		<dc:creator>Aaron Hardy</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[viewstate]]></category>

		<guid isPermaLink="false">http://www.aaronhardy.com/intimateconversations/?p=7</guid>
		<description><![CDATA[Suggested Reading: Understanding ASP.NET View State Recently I&#8217;ve started learning ASP.NET (VB) in order to develop an application assigned to me at ExxonMobil. I have previously worked with ASP Classic, but the .NET framework is fairly new to me, so I&#8217;m just picking up on the architecture and how Microsoft is trying to take over [...]]]></description>
			<content:encoded><![CDATA[<p><b>Suggested Reading:</b><br />
<a href="http://msdn2.microsoft.com/en-us/library/ms972976.aspx" target="_blank">Understanding ASP.NET View State</a></p>
<p>Recently I&#8217;ve started learning ASP.NET (VB) in order to develop an application assigned to me at ExxonMobil.  I have previously worked with ASP Classic, but the .NET framework is fairly new to me, so I&#8217;m just picking up on the architecture and how Microsoft is trying to take over my code and make things easier for me.  Don&#8217;t get me wrong, I think .NET is a step in the right direction.  It absolutely does a better job of following the model-view-controller design pattern by almost forcing you to keep your design code split from your logic.  With that in mind, there are certainly accompanying downfalls, which I&#8217;ll talk about in this post. <span id="more-9"></span></p>
<p>For my project, I&#8217;m developing an application to track exception in the corporate Internet filter.  That means when someone finds out that their vendor&#8217;s website has been blocked by WebSense (the corporate Internet filter) and they feel they are justified in having it unblocked, they must go through a process.  This process will be managed by the new system I&#8217;m developing.  Now, what if the vendor&#8217;s website is made up of three different, but related, domain names (e.g., http://imyourvendor.com, http://imyourvendorslegaldepartment.com).  Instead of having the fill out a big form for each related websites, on the Website Address field I just wanted to provide a link that says &#8220;add another,&#8221; which would then create an additional textbox for the user to enter another related website address.   Here&#8217;s a snapshot of the concept:</p>
<p><a href="/wp-content/uploads/2007/06/address-fields.gif" title="Address Form Fields"><img src="/wp-content/uploads/2007/06/address-fields.thumbnail.gif" alt="Address Form Fields" /></a></p>
<p>Now, if you&#8217;re acquainted with JavaScript you know it can&#8217;t be too hard to just plug another textbox in there without refreshing the page.   On the other hand, customized JavaScript that adds DOM elements and ASP.NET are practically enemies, the reasons of which could fill an entirely separate article(s).  After a few hours of battling with that idea, I decided for the sake of programmers that came later and for my own sanity, I would stick to postbacks.</p>
<p>One more thing to understand before we delve into code: ViewState manages changes in control values, not the controls themselves.  So, if we dynamically create a new textbox control on every click of the Add Another link, we must recreate all the dynamic textboxes that we had created up until that time.  <strong>On every postback, you must recreate all the dynamic controls you had created previously. </strong>That&#8217;s one thing Microsoft won&#8217;t do for you.</p>
<p>So, in order to track how many dynamic textboxes I had created previously, I&#8217;m going to create a property for that purpose:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">' This keeps track of how many URL text fields we have on the form. It is used</span>
<span style="color: #008080; font-style: italic;">' to recreate our dynamic text fields on each post-back</span>
<span style="color: #FF8000;">Private</span> <span style="color: #FF8000;">Property</span> URLControlsCount<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>
    <span style="color: #FF8000;">Get</span>
        <span style="color: #0600FF;">If</span> <span style="color: #000000;">&#40;</span>ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;_urlControlsCount&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span>
            <span style="color: #FF8000;">Return</span> <span style="color: #0600FF;">CType</span><span style="color: #000000;">&#40;</span>ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;_urlControlsCount&quot;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Else</span>
            <span style="color: #FF8000;">Return</span> <span style="color: #FF0000;">1</span> <span style="color: #008080; font-style: italic;">' We always want at least one URL text field on the form</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Get</span>
    <span style="color: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
        ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;_urlControlsCount&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> value
    <span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Set</span>
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Property</span></pre></td></tr></table></div>

<p>Notice that the property value is stored within the ViewState and, as such, will be tracked across future postbacks.  Also notice that if the value hasn&#8217;t been set, it will return 1.  That comes into play later because I always want at least one textbox displaying on the page.</p>
<p>Now, let&#8217;s create the Click event for the &#8220;Add Another&#8221; button:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;">Protected <span style="color: #0600FF;">Sub</span> AddURLLink_Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> AddURLLink.<span style="color: #0000FF;">Click</span>
    <span style="color: #0600FF;">Dim</span> newTextBox <span style="color: #FF8000;">As</span> TextBox <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> TextBox<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
    URLControlsCount <span style="color: #008000;">+=</span> <span style="color: #FF0000;">1</span>
    newTextBox.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;url&quot;</span> <span style="color: #008000;">+</span> URLControlsCount.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    newTextBox.<span style="color: #0000FF;">CssClass</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;GridFormTextBox&quot;</span>
    URLFieldsPlaceHolder.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>newTextBox<span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></td></tr></table></div>

<p>This increments the property we created in the last step so that on a future postback we will know how many controls to recreate.  It then uses that control value to make an ID (convenient since URLControlsCount is already incrementing) and then adds the control to a placeholder.</p>
<p>Now we need to make a function to recreate the controls that we had created on previous postbacks.  Here&#8217;s mine:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">' We must re-create our dynamic URL controls on each postback</span>
<span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> CreateURLControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span> <span style="color: #FF8000;">To</span> URLControlsCount
        <span style="color: #0600FF;">Dim</span> newTextBox <span style="color: #FF8000;">As</span> TextBox <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> TextBox<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        newTextBox.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;url&quot;</span> <span style="color: #008000;">+</span> i.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        newTextBox.<span style="color: #0000FF;">CssClass</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;GridFormTextBox&quot;</span>
        URLFieldsPlaceHolder.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>newTextBox<span style="color: #000000;">&#41;</span>
    <span style="color: #FF8000;">Next</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></td></tr></table></div>

<p>Now here comes the tricky part.  Where are we going to call that function?  If we call it from Page_Load, we won&#8217;t be able to get the values from the controls when we need to (when the user is done filling out the form and we&#8217;re ready for processing).  The reason is that the values are applied to the controls at the <strong>beginning</strong>  of (before?) Page_Load.  If we don&#8217;t have our controls in the control tree until <strong>after the beginning</strong> of Page_Load, their values won&#8217;t be applied, so when we call&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">CType</span><span style="color: #000000;">&#40;</span>URLFieldsPlaceHolder.<span style="color: #0000FF;">FindControl</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;url&quot;</span> <span style="color: #008000;">&amp;</span> i<span style="color: #000000;">&#41;</span>, TextBox<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Text</span></pre></td></tr></table></div>

<p>&#8230;we won&#8217;t get an error (the controls do indeed exist), but we&#8217;ll get an empty string rather than the true value.</p>
<p>So how about we call the function from Page_Init?  While this would essentially allow us to access the values of the controls from within Page_Load (because they would have been added to the control tree before the beginning of Page_Load), we&#8217;ve run into a different problem: the ViewState for our URLControlsCount property has not been loaded yet.  In other words, when our CreateURLControls() function tries to get the value of URLControlsCount, it&#8217;s not going to find the right value.  In the end, we end up with the wrong number of controls (but hey, at least we would be able to access them now, right?).</p>
<p>So what&#8217;s the answer? 42.  No, not really.  We need to call the CreateURLControls() function <strong>after</strong> the ViewState has been loaded, but <strong>before </strong>Page_Load.  In order to do this, let&#8217;s override LoadViewState() like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;">Protected <span style="color: #FF8000;">Overrides</span> <span style="color: #0600FF;">Sub</span> LoadViewState<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> savedState <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span><span style="color: #000000;">&#41;</span>
    <span style="color: #FF8000;">MyBase</span>.<span style="color: #0000FF;">LoadViewState</span><span style="color: #000000;">&#40;</span>DirectCast<span style="color: #000000;">&#40;</span>savedState, Pair<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#41;</span>
    CreateURLControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></td></tr></table></div>

<p>This allows us to load the ViewState before creating our controls, but before Page_Load.  We must also override SaveViewState():</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;">Protected <span style="color: #FF8000;">Overrides</span> <span style="color: #0600FF;">Function</span> SaveViewState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>
    <span style="color: #FF8000;">Return</span> <span style="color: #FF8000;">New</span> Pair<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">MyBase</span>.<span style="color: #0000FF;">SaveViewState</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF8000;">Nothing</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></pre></td></tr></table></div>

<p>LoadViewState is invoked <em>only</em> if ViewState was saved for the given control.  I don&#8217;t really understand this part as much as I should, so I won&#8217;t pretend like i do&#8230;I&#8217;ll leave it at that.</p>
<p>One last thing.  LoadViewState() is only called on postbacks.  This means that CreateURLControls() won&#8217;t get called the first time we load the page, which means that we won&#8217;t have <em>any</em> textbox on our page.   I want to start out with one textbox on the page, so i will call CreateURLControls() from Page_Init <em>only</em> if it&#8217;s not a postback.  Remember how in the first step I set up the property to return 1 if the ViewState value didn&#8217;t exist?  That&#8217;s what we need on this occasion, and this occasion only.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;">Protected <span style="color: #0600FF;">Sub</span> Page_Init<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">Init</span>
    <span style="color: #0600FF;">If</span> <span style="color: #804040;">Not</span> IsPostBack <span style="color: #FF8000;">Then</span>
        CreateURLControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></td></tr></table></div>

<p>Wow&#8230;.how about that. Kind of a whirlwind and if you weren&#8217;t already struggling to find the answer to this problem, very little of this might make sense to you.  On the other hand, if you were trying to figure this out and pouring over websites and books trying to find the answer, hopefully this revealed the piece of the puzzle you were searching for.  Here&#8217;s my final code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Imports</span> ePACS.<span style="color: #0000FF;">ISUBData</span>.<span style="color: #0000FF;">BO</span>
<span style="color: #0600FF;">Imports</span> ePACS.<span style="color: #0000FF;">ISUBData</span>.<span style="color: #0000FF;">BLL</span>
&nbsp;
Partial <span style="color: #0600FF;">Class</span> TestWithMaster
    <span style="color: #0600FF;">Inherits</span> System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">Page</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">' This keeps track of how many URL text fields we have on the form. It is used</span>
    <span style="color: #008080; font-style: italic;">' to recreate our dynamic text fields on each post-back</span>
    <span style="color: #FF8000;">Private</span> <span style="color: #FF8000;">Property</span> URLControlsCount<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>
        <span style="color: #FF8000;">Get</span>
            <span style="color: #0600FF;">If</span> <span style="color: #000000;">&#40;</span>ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;_urlControlsCount&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">IsNot</span> <span style="color: #FF8000;">Nothing</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span>
                <span style="color: #FF8000;">Return</span> <span style="color: #0600FF;">CType</span><span style="color: #000000;">&#40;</span>ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;_urlControlsCount&quot;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
            <span style="color: #FF8000;">Else</span>
                <span style="color: #FF8000;">Return</span> <span style="color: #FF0000;">1</span> <span style="color: #008080; font-style: italic;">' We always want at least one URL text field on the form</span>
            <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
        <span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Get</span>
        <span style="color: #FF8000;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> value <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span><span style="color: #000000;">&#41;</span>
            ViewState<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;_urlControlsCount&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> value
        <span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Set</span>
    <span style="color: #0600FF;">End</span> <span style="color: #FF8000;">Property</span>
&nbsp;
    Protected <span style="color: #0600FF;">Sub</span> Page_Init<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">Init</span>
        <span style="color: #0600FF;">If</span> <span style="color: #804040;">Not</span> IsPostBack <span style="color: #FF8000;">Then</span>
            CreateURLControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
    Protected <span style="color: #FF8000;">Overrides</span> <span style="color: #0600FF;">Sub</span> LoadViewState<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> savedState <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span><span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">MyBase</span>.<span style="color: #0000FF;">LoadViewState</span><span style="color: #000000;">&#40;</span>DirectCast<span style="color: #000000;">&#40;</span>savedState, Pair<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#41;</span>
        CreateURLControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
    Protected <span style="color: #0600FF;">Sub</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">Load</span>
        <span style="color: #0600FF;">Dim</span> masterPage <span style="color: #FF8000;">As</span> MasterPage <span style="color: #008000;">=</span> <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">Master</span>
        masterPage.<span style="color: #0000FF;">Title</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;Submit a Request&quot;</span>
        masterPage.<span style="color: #0000FF;">Subtitle</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;Submit a request to block or unblock a website from the corporate filtering system&quot;</span>
        masterPage.<span style="color: #0000FF;">RightPanelVisibility</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
    Protected <span style="color: #FF8000;">Overrides</span> <span style="color: #0600FF;">Function</span> SaveViewState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>
        <span style="color: #FF8000;">Return</span> <span style="color: #FF8000;">New</span> Pair<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">MyBase</span>.<span style="color: #0000FF;">SaveViewState</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF8000;">Nothing</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span>
&nbsp;
    Protected <span style="color: #0600FF;">Sub</span> AddURLLink_Click<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> AddURLLink.<span style="color: #0000FF;">Click</span>
        <span style="color: #0600FF;">Dim</span> newTextBox <span style="color: #FF8000;">As</span> TextBox <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> TextBox<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
        URLControlsCount <span style="color: #008000;">+=</span> <span style="color: #FF0000;">1</span>
        newTextBox.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;url&quot;</span> <span style="color: #008000;">+</span> URLControlsCount.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        newTextBox.<span style="color: #0000FF;">CssClass</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;GridFormTextBox&quot;</span>
        URLFieldsPlaceHolder.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>newTextBox<span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">' We must re-create our dynamic URL controls on each postback</span>
    <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> CreateURLControls<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">For</span> i <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span> <span style="color: #FF8000;">To</span> URLControlsCount
            <span style="color: #0600FF;">Dim</span> newTextBox <span style="color: #FF8000;">As</span> TextBox <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> TextBox<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
            newTextBox.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;url&quot;</span> <span style="color: #008000;">+</span> i.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
            newTextBox.<span style="color: #0000FF;">CssClass</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;GridFormTextBox&quot;</span>
            URLFieldsPlaceHolder.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>newTextBox<span style="color: #000000;">&#41;</span>
        <span style="color: #FF8000;">Next</span>
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&nbsp;
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Class</span></pre></td></tr></table></div>

<p>If I made a mistake in my explanation or you just want to have a nerd party, please&#8230;<strong>join the intimate conversation</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaronhardy.com/aspnet/dynamic-controls-and-managing-viewstate/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

