<?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>WhyPad &#187; ASP.NET/C#</title>
	<atom:link href="http://www.whypad.com/tag/aspnet-c-sharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.whypad.com</link>
	<description>Tips, tricks, and hacks for life and tech...</description>
	<lastBuildDate>Tue, 06 Jul 2010 15:21:22 +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>C#: Break vs. Continue in Foreach Loops</title>
		<link>http://www.whypad.com/posts/c-break-vs-continue-in-foreach-loops/776/</link>
		<comments>http://www.whypad.com/posts/c-break-vs-continue-in-foreach-loops/776/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 14:17:40 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=776</guid>
		<description><![CDATA[A C# Quick Tip: You&#8217;ve probably seen &#8216;break&#8217; before in Switch Case statements, but it also plays an important role in Foreach Loops. To break completely out of a foreach loop (foregoing all the remaining loops), use: break; To go to the next iteration in the loop, use: continue; When is &#8216;break&#8217; useful? It is [...]]]></description>
			<content:encoded><![CDATA[<p><strong>A C# Quick Tip: </strong> You&#8217;ve probably seen &#8216;break&#8217; before in Switch Case statements, but it also plays an important role in Foreach Loops.</p>
<p>To break completely out of a foreach loop (foregoing all the remaining loops), use:  <strong>break;</strong></p>
<p>To go to the next iteration in the loop, use:  <strong>continue;</strong></p>
<p><span id="more-776"></span></p>
<p>When is &#8216;break&#8217; useful?  It is particularly useful if you&#8217;re looping through an a collection of Objects (like Rows in a Datatable) and you&#8217;re searching for a particular match.  When you find that match, there&#8217;s no need to continue through the remaining rows, so you want to Break out.  There are plenty of other cases as well.</p>
<p>&#8216;Continue&#8217; is useful when you&#8217;ve accomplished what you need to in side a loop iteration and you don&#8217;t want to process the remaining code for that particular iteration.  You&#8217;ll normally have &#8216;continue&#8217; after an &#8216;if&#8217;.</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=776&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/c-break-vs-continue-in-foreach-loops/776/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: That Pesky DBNull Error in Datatables</title>
		<link>http://www.whypad.com/posts/c-that-pesky-dbnull-error-in-datatables/767/</link>
		<comments>http://www.whypad.com/posts/c-that-pesky-dbnull-error-in-datatables/767/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 21:42:02 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=767</guid>
		<description><![CDATA[Datatables and Tableadapters are one of the most beautiful things about C# and Visual Studio, until you&#8217;re processing rows in your datatable and you get something that reads like &#8220;The value for column &#8216;your_column&#8217; in table &#8216;your_table&#8217; is DBNull.&#8221;  If you go a little further, you may get:  &#8220;Exception Details: System.InvalidCastException: Specified cast is not [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.whypad.com/wp-content/uploads/datatable.jpg"><img class="alignleft size-thumbnail wp-image-769" title="datatable" src="http://www.whypad.com/wp-content/uploads/datatable-150x150.jpg" alt="" width="150" height="150" /></a>Datatables and Tableadapters are one of the most beautiful things about C# and Visual Studio, until you&#8217;re processing rows in your datatable and you get something that reads like &#8220;The value for column &#8216;your_column&#8217; in table &#8216;your_table&#8217; is DBNull.&#8221;  If you go a little further, you may get:  &#8220;<strong>Exception Details: </strong>System.InvalidCastException: Specified cast is not  valid.&#8221;  You may go to your Datatable in your Dataset and start trying to change the NullValue for the offending column or trying to change the DefaultValue in the column properties, but chances are that the data type will not let you change that NullValue.  It will insist on &#8220;(Throw Exception)&#8221;.<span id="more-767"></span></p>
<h3>Help for the DBNull Problem has Arrived!</h3>
<p>The answer is so simple (at least in VS 2008 and greater, maybe before that too).  It is rather sad how much time I&#8217;ve wasted trying to deal with this problem.  Without further ado, the answer!</p>
<p>You&#8217;re probably using a Datarow object to get at the column values, probably buried in a foreach statement.  Instead of messing around with the specific column like  myRow.MyColumn, the Row object has an isNull method for each column in the row.  The method name takes the column&#8217;s name and puts &#8220;is&#8221; in front, and &#8220;null&#8221; behind.  So for a column called MyColumn, the method would be:   myRow.IsMyColumnNull();</p>
<p>Here&#8217;s a full example of dealing with nulls in a DateTime field.  The logic is the same for Int or other numerics as well.</p>
<pre class="brush: csharp;">

int iReq = 6196;
certdbTableAdapters.integration_procsTableAdapter taProcs = new certdbTableAdapters.integration_procsTableAdapter();
certdb.integration_procsDataTable dtProcs = taProcs.GetData(iReq);

certdb.integration_procsRow rowProc = dtProcs.Rows[0];

if(!rowProc.IsStartTimeNull())
{
    sProcDate = rowProc.StartTime.ToShortDateString();
}
else
{
    sProcDate = &quot;&quot;;
}
</pre>
<h3>The Craziness I Tried Before</h3>
<p>Before I finally found the answer to this, I was using all sorts of crazy workarounds, including using multiple queries to be sure I never got a null situation, and using the fabulous Case statement within SQL statements.  Here&#8217;s a little tast of a Case statement:</p>
<pre class="brush: sql;">
SELECT DISTINCT Process.ProcessID, Process.Name, Process.Description, Process.ResultLogStatusID,
Process.StatusTimeStamp, ResultLogStatus.Name AS ProcResult,
CASE WHEN LogProcess.EndTime IS NULL THEN '1/1/1900' ELSE LogProcess.EndTime END as rortime,
CASE WHEN LogProcess.Status IS NULL THEN 'missing' ELSE LogProcess.Status END as rorstatus
FROM         RequirementTestLink
LEFT JOIN Process ON Process.ProcessID = RequirementTestLink.ProcessID
LEFT JOIN ResultLogStatus ON Process.ResultLogStatusID = ResultLogStatus.ResultLogStatusID
LEFT JOIN LogProcess ON Process.ProcessID = LogProcess.ProcessID AND LogProcess.ResultOfRecord = 1 WHERE     (RequirementTestLink.RequirementID IN (&quot; + sReqs + &quot;)) ORDER BY Process.ProcessID
</pre>
<p>I tried lots of things&#8230;here are a couple others that didn&#8217;t work on my strongly typed datatables:</p>
<p>myRow.MyColumn != null<br />
or<br />
Convert.IsDBNull(myRow.MyColumn)</p>
<p>Neither worked! It&#8217;s a shame the answer was much simpler!</p>
<p>I hope this post will help someone else out there.  Hopefully I&#8217;ll remember this post the next time I&#8217;m dealing with DBNulls.<br />
Cheers,<br />
Byron</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=767&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/c-that-pesky-dbnull-error-in-datatables/767/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# &#8211; SQL Server Foreign Key, Unique constraints</title>
		<link>http://www.whypad.com/posts/c-sql-server-foreign-key-unique-constraints/682/</link>
		<comments>http://www.whypad.com/posts/c-sql-server-foreign-key-unique-constraints/682/#comments</comments>
		<pubDate>Wed, 27 May 2009 21:40:24 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=682</guid>
		<description><![CDATA[I got tripped up today with a particularly nasty Join statement that worked perfectly form 90% of the queries, but was blowing up at times.  The error message was:  Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. I looked and looked at this SQL code: SELECT Process.ProcessID, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.whypad.com/wp-content/uploads/unique_key_constraint.gif"><img class="alignleft size-thumbnail wp-image-684" title="unique_key_constraint" src="http://www.whypad.com/wp-content/uploads/unique_key_constraint-150x150.gif" alt="unique_key_constraint" width="150" height="150" /></a>I got tripped up today with a particularly nasty Join statement that worked perfectly form 90% of the queries, but was blowing up at times.  The error message was: </p>
<p><span id="more-682"></span></p>
<p>Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.</p>
<p>I looked and looked at this SQL code:</p>
<pre class="brush: sql;">
SELECT Process.ProcessID, Process.Name, Process.Description, Process.ResultLogStatusID, Process.StatusTimeStamp, ResultLogStatus.Name AS ProcResult, RequirementTestLink.RequirementID

FROM RequirementTestLink

LEFT JOIN Process ON Process.ProcessID = RequirementTestLink.ProcessID

LEFT JOIN ResultLogStatus ON Process.ResultLogStatusID = ResultLogStatus.ResultLogStatusID

WHERE (RequirementTestLink.RequirementID IN (&quot; + sReqs + &quot;))

ORDER BY RequirementTestLink.RequirementID, Process.ProcessID
</pre>
<p>It looked perfectly legit to me. I figured out that there would be multiple instances of Process.ProcessID in the RequirementTestLink table. But this shouldn&#8217;t have been a problem since it would just create multiple rows for the multiple instances.</p>
<h2>Relax or turn off constraints in your DataSet.</h2>
<p>The problem was that I was using a SQL Adapter to fill a DataTable. When I made the DataTable, it got junked up with Primary Key and Foreign Key constraints on the ProcessID field. The answer had been right there in the message (see image below).  They spelled it out in the first line:  &#8220;Relax or turn off constraints in your DataSet&#8221;. It took me 30 minutes to work it out on my own&#8230;if I&#8217;d just actually read those pop ups (but so often they are so cryptic I can&#8217;t comprehend them <img src='http://www.whypad.com/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' />  ).<br />
<a href="http://www.whypad.com/wp-content/uploads/fk-constraint.gif"></a><br />
<a href="http://www.whypad.com/wp-content/uploads/relax_constraints.gif"><img class="alignleft size-full wp-image-689" title="relax_constraints" src="http://www.whypad.com/wp-content/uploads/relax_constraints.gif" alt="relax_constraints" width="450" height="228" /></a></p>
<p><a href="http://www.whypad.com/wp-content/uploads/delete_key_button.gif"><img class="alignright size-full wp-image-688" title="delete_key_button" src="http://www.whypad.com/wp-content/uploads/delete_key_button.gif" alt="delete_key_button" width="180" height="236" /></a>So, to get rid of that Primary Key:</p>
<ol>
<li>Left click on the key icon (you can see it in the image at right beside ProcessID)</li>
<li>Right click on the same key icon and select &#8216;Delete key&#8217;. </li>
</ol>
<p><a href="http://www.whypad.com/wp-content/uploads/fk-constraint.gif"><img class="alignleft size-thumbnail wp-image-683" title="fk-constraint" src="http://www.whypad.com/wp-content/uploads/fk-constraint-150x150.gif" alt="fk-constraint" width="150" height="150" /></a>If that doesn&#8217;t do it for you, then zap the Foreign Keys as well.  You can see one coming in at the top right (left picture).  Click on the line and delete it.</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=682&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/c-sql-server-foreign-key-unique-constraints/682/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Get ASP.NET DropDownList Selected Value with jQuery</title>
		<link>http://www.whypad.com/posts/get-aspnet-server-dropdownlist-selected-value-with-jquery/418/</link>
		<comments>http://www.whypad.com/posts/get-aspnet-server-dropdownlist-selected-value-with-jquery/418/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 22:19:00 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[Javascript Frameworks]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=418</guid>
		<description><![CDATA[With Microsoft&#8217;s recent announcement that they will begin shipping jQuery with Visual Studio, the little Javascript framework is about to hit the big time (it was already sort of big time, but this is BIG TIME).  And rightfully so. I have been a Prototype and Scriptaculous user for over a year now.  But I am [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-419" title="aspnetjquery" src="http://www.whypad.com/wp-content/uploads/aspnetjquery.gif" alt="" width="138" height="66" />With Microsoft&#8217;s <a href="http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-and-microsoft.aspx">recent announcement </a>that they will begin shipping jQuery with Visual Studio, the little Javascript framework is about to hit the big time (it was already sort of big time, but this is BIG TIME).  And rightfully so.</p>
<p><span id="more-418"></span></p>
<p>I have been a Prototype and Scriptaculous user for over a year now.  But I am starting the transition over to jQuery, and am loving what I see.  The learning curve is easy if you&#8217;re coming in from one of the other frameworks, so don&#8217;t sweat it.</p>
<p>If you&#8217;re an ASP.net programmer and you use server controls, then you know that ASP changes the control ID into something that the server will be able to understand on postbacks.  This makes it a little tricky to reference these controls with Javascript on the client.</p>
<h2>The ASP Trick</h2>
<p><img class="alignleft size-full wp-image-420" title="ddlcycles" src="http://www.whypad.com/wp-content/uploads/ddlcycles.gif" alt="" width="142" height="78" />Well, here&#8217;s a neat little trick that I picked up on one of the forums that will allow you to get the value of an ASP.net dropdownlist using jQuery.  Place the following Javascript function at the bottom of your .aspx file.  I think you&#8217;ll need to have it after your DropDownList to insure that it works properly.</p>
<pre class="brush: jscript;">&lt;script type=&quot;text/javascript&quot;&gt;
        function getDropDownList1Value()
        {
            var SelectedVal = $('#&lt; %=&lt;strong&gt;DropDownList1.ClientID %&gt;').val();
            return SelectedVal;
        }
&lt;/script&gt;</pre>
<p>The jQuery part of this function is shown in blue here:  <span style="color: #0000ff;"><strong>$(&#8216;#</strong></span><span style="color: #ff0000;">&lt;%=DropDownList1.ClientID %&gt;</span><strong><span style="color: #0000ff;">&#8216;).val()</span> </strong></p>
<p>The real magic is the red part.  This is ASP code that will be processed on the server to write the control&#8217;s Client ID into the function.  Given such a small amount of jQuery, you could easily pull out the jQuery code and use it with other frameworks or plain old JScript as well.  The key after all, is getting your Javascript to know what the control&#8217;s DOM ID is.</p>
<p>Using the function is as simple as:</p>
<p>var myValue = getDropDownList1Value();</p>
<p>This could, of course, be used with any ASP.net server control for other purposes as well.</p>
<h2>A jQuery Alternative: The CSS Class Selector</h2>
<p>Another alternative with jQuery, would be to assign a css class to the control and use that class as your selector.  Here&#8217;s a jQuery example to select on a css class called mydropdown:</p>
<p>$(&#8216;.mydropdown&#8217;).val();</p>
<p>Just assign the mydropdown class to your ASP control in Visual Studio and jQuery will find your control.  But be careful&#8230;jQuery will find every control that has that class, so give a unique class to each control you want to try this with.</p>
<p>Hope that helps.</p>
<p>Cheers!<br />
Byron</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=418&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/get-aspnet-server-dropdownlist-selected-value-with-jquery/418/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ASP.NET &#8211; C# &#8211; Emulating the VB Control Array &#8211; Pt. 1</title>
		<link>http://www.whypad.com/posts/aspnet-emulating-control-array/388/</link>
		<comments>http://www.whypad.com/posts/aspnet-emulating-control-array/388/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 18:25:05 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=388</guid>
		<description><![CDATA[For you veterans of VB6, the control array probably became an old friend, or at least an annoying neighbor if you didn&#8217;t like them.  You couldn&#8217;t help run across the concept in VB6 because every time you copy and pasted a control for the first time, you got asked if you wanted to create a control array. If [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-389" href="http://www.whypad.com/posts/aspnet-emulating-control-array/388/vb6controlarray/"><img class="alignleft size-full wp-image-389" title="vb6controlarray" src="http://www.whypad.com/wp-content/uploads/vb6controlarray.gif" alt="" width="222" height="187" /></a>For you veterans of VB6, the control array probably became an old friend, or at least an annoying neighbor if you didn&#8217;t like them.  You couldn&#8217;t help run across the concept in VB6 because every time you copy and pasted a control for the first time, you got asked if you wanted to create a control array.</p>
<p><span id="more-388"></span></p>
<p>If you say yes, VB creates an array for you with the same name and type as the control you copied and begins assigning values to the index property of the original and all new copies.  Keep pasting and the array just grows.  You reference individual instances of the control like:  myControl[0].Text for first instnace, myControl[1].text for second instance, and so on.</p>
<p>But now you&#8217;ve made the switch to C#, or if you just couldn&#8217;t let it go, hopefully to VB.Net, and you&#8217;re wondering what the heck happened to the control arrays.  Well, the short answer is, they&#8217;re gone!  The control array of yore was great if you needed a bunch of textboxes, radio buttons, or checkboxes that you would run through a For Each or For Next loop.  That need hasn&#8217;t magically gone away, so what&#8217;s a developer to do?</p>
<p>This article will discuss how to emulate a control array or an array of html form elements in <strong>ASP.Net</strong> using C# code.  Translating to VB.Net should be relatively painless once you catch the drift.</p>
<h2>Server Controls vs. HTML Form Elements</h2>
<p>First, it bears remembering that in ASP.NET, you get to choose between Server Controls like the TextBox (aka <span style="font-family: Courier New;">System.Web.UI.WebControls.TextBox) and straight up HTML tags like &lt;INPUT type=&#8221;text&#8221; name=&#8221;element_name&#8221; value=&#8221;" /&gt;</span>.  Both of these can be used like a control array, but the methods are little different.</p>
<p>Lets look at the Server Control first&#8230;</p>
<h2>Emulating Control Arrays with Server Controls</h2>
<p>Before I jump into an example, here&#8217;s a rundown of the basic steps for emulating a Control Array with Server Controls:</p>
<ol>
<li>Create your controls that you want to target&#8230;give them IDs that start with a similar prefix</li>
<li>Add your controls to a PlaceHolder control or some other object that has a controls collection.  This allows you to utilize the Controls Collection for easy looping</li>
<li>Set up your function ForEach loop utilizing said Controls Collection of the parent</li>
</ol>
<h3>Jumping Right In</h3>
<p>The first thing you need to do is get your controls created on your Web Form.  You can manually create your controls at design time, dragging and dropping from the Visual Studio toolbox into your form.  Or, you can create the controls at runtime.</p>
<p>The design time creation is blindingly straight forward, so we won&#8217;t go into that, and will do a quick example of runtime creation instead.  In this example, we&#8217;ll create 12 textboxes (1 for each month):</p>
<h3>Start with a PlaceHolder</h3>
<p><img class="alignright size-full wp-image-392" title="placeholder" src="http://www.whypad.com/wp-content/uploads/placeholder.gif" alt="" width="133" height="191" />You need some means of placing your controls with some level of precision in your form.  ASP.Net provides the PlaceHolder control to which you can .Add controls.  So drag a PlaceHolder from the toolbox to the place in your document where you want your textboxes to go.  It gets a name like PlaceHolder1.</p>
<h3>Function for Creating/Adding Controls</h3>
<p>If you&#8217;re creating a whole bunch of similar controls, hopefully you&#8217;ve got a list, a datatable, or an array to loop through, else your code will be slightly more lengthy.  Here&#8217;s a simple For loop that will create the 12 textboxes for the months.  Note that it uses a little string formatting to pop the month name in the newly created textboxes.  SteveX has a super helpful <a href="http://blog.stevex.net/index.php/string-formatting-in-csharp/">reference </a> and <a href="http://blog.stevex.net/index.php/2007/09/28/string-formatting-faq/">FAQ </a>for string formatting.  Thanks, Steve!<br />
protected void addMonthTextBoxes()<br />
{<br />
//Set up the for loop<br />
for (int i = 1; i < 13; i++)<br />
{<br />
TextBox tt = new TextBox();<br />
//   Note that we are prefixing our ID with something that is smiliar<br />
tt.ID = "month" + i;<br />
tt.Text = string.Format("{0:MMMM}", DateTime.Parse( i + "/1/2008"));<br />
PlaceHolder1.Controls.Add(tt);<br />
}<br />
}<br />
Call your function from the Page_Load function, and upon execution, you should have 12 textboxes with the Month names in them.<br />
<quot></p>
<h3>Setting up a Button and a Handler Function</h3>
<p>Now that you&#8217;ve got your textboxes going, drag a button onto your Web Form and double click it to get to the codebehind function that will handle it.  Inside the button&#8217;s click function, use the following code to loop through the Controls Collection of the PlaceHolder you created.  Note that most, if not all, server controls will have a Controls Collection that you can access and do a ForEach loop on.  Here&#8217;s the code:</p>
<p>protected void btnRefresh_Click(object sender, EventArgs e)<br />
{<br />
foreach (Control c in PlaceHolder1.Controls){<br />
//In this case, we&#8217;re only looking for TextBox controls&#8230;use GetType() to test it<br />
if (c.GetType().ToString().Equals(&#8220;System.Web.UI.WebControls.TextBox&#8221;)){// We look at the Control&#8217;s ID to see if it starts with the our unique prefix<br />
if (c.ID.StartsWith(&#8220;month&#8221;)){</p>
<p>/*<br />
Note that we don&#8217;t have an index or even the good old Tag<br />
VB6.  One way to emulate those properties is to use the &#8220;suffix&#8221; of your ID.<br />
In this case, we used the Month #.  So, parsing the ID string by stripping<br />
off &#8216;month&#8217; would leave us with the month #.  And we can use as needed.<br />
*/  property from</p>
<p>//We have to cast the control as a textbox to use it<br />
TextBox tb = (TextBox)c;<br />
//Do something with &#8216;tb&#8217;, the text box we found.. e.g.  tb.Text = &#8220;Hello &#8221; + tb.Text<br />
}<br />
}<br />
}<br />
}</p>
<p>Notice that the key is being able to loop through the PlaceHolder&#8217;s Controls Collection.  You could loop through the Controls Collection of any control on your form.  Even the Form itself like so:</p>
<p>foreach ( Control c in Form.Controls){ &#8230;. }</p>
<p>This foreach only gets the immediate child controls of the Form object.  If you do not know the particular parent or parents of your target controls, you will need to use a recursive function that walks through the Children of each control on the form.  Not the most efficient means of processing, but it works.</p>
<h3>See Part 2 for Using HTML Form Elements like Control Arrays</h3>
<p>That&#8217;s it for emulating control arrays with ASP.Net Server Controls.  I&#8217;m sure there&#8217;s much more that could be written, but that&#8217;s all I&#8217;ve got in me today.  Demonstrating the recursive function that walks all the children of the Form object would be a good start&#8230;maybe later.</p>
<p>Part 2 of this post will show how to use a straight up html form element, ie INPUT tags that don&#8217;t runat server, as arrays.  For those of you with PHP background, you know how nice PHP is to place those for you in an Array through $_REQUEST, $_GET, and $_POST.  We&#8217;ll see how we can do that in ASP.Net.  Hopefully I&#8217;ll get that done in the near, near future.</p>
<p>Cheers!</p>
<p>Byron</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=388&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/aspnet-emulating-control-array/388/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ASP.Net C# &#8211; PHP  Equivalents</title>
		<link>http://www.whypad.com/posts/aspnet-c-sharp-php-equivalents/263/</link>
		<comments>http://www.whypad.com/posts/aspnet-c-sharp-php-equivalents/263/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 21:05:36 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=263</guid>
		<description><![CDATA[On this post, I will be collecting ASP.NET C# commands that I use frequently in PHP but I have to continually look up in the little bit of ASP.NET that I do.  PHP can be frustrating for its inconsistencies in syntax, but what is even more frustrating is when you&#8217;re trying to do something in [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-451" title="ASP.net" src="http://www.whypad.com/wp-content/uploads/logo.png" alt="ASP.net" width="108" height="44" /><img class="alignnone size-full wp-image-450" title="php_snow_2008" src="http://www.whypad.com/wp-content/uploads/php_snow_2008.gif" alt="php_snow_2008" width="120" height="64" /></p>
<p><span id="more-263"></span></p>
<p>On this post, I will be collecting ASP.NET C# commands that I use frequently in PHP but I have to continually look up in the little bit of ASP.NET that I do.  PHP can be frustrating for its inconsistencies in syntax, but what is even more frustrating is when you&#8217;re trying to do something in C# and you&#8217;re trying to remember whether the common sense syntax is in PHP or C#&#8230;This tries to be a cheat sheet for those for me:</p>
<table border="2" cellspacing="2" cellpadding="2">
<tbody>
<tr>
<td width="50%"><strong>PHP</strong></td>
<td width="50%"><strong>ASP.net</strong></td>
</tr>
<tr>
<td>echo &#8216;string&#8217;;</td>
<td>Response.Write(&#8220;the_text_goes_here&#8221;);</td>
</tr>
<tr>
<td>$_REQUEST['parameter_name'];</td>
<td>Request.QueryString["parameter_name"];</td>
</tr>
<tr>
<td>urlencode( <em>string</em> );</td>
<td>Server.UrlEncode( <em>string</em> );</td>
</tr>
<tr>
<td><strong>Convert unix timestamp:</strong>date(&#8220;m.d.y&#8221;, $timestamp);</td>
<td>public static System.DateTime UnixToDotNet(int unixTimestamp)<br />
{<br />
System.DateTime date = System.DateTime.Parse(&#8220;1/1/1970&#8243;);<br />
return date.AddSeconds(unixTimestamp);<br />
}</td>
</tr>
<tr>
<td><strong>Connection string:</strong><br />
mysql_connect([string $server [,string $username [, string $password [,bool $new_link [, int $client_flags ]]]]] )</td>
<td><strong>Code for using conn string in Web.config:</strong></p>
<pre class="csharpcode"><span class="kwrd">string</span> dbConn = System.Configuration.ConfigurationManager.ConnectionStrings[<span class="str">"QueryStringName"</span>].ToString();</pre>
</td>
</tr>
</tbody>
</table>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=263&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/aspnet-c-sharp-php-equivalents/263/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET/C#: Convert HTML Colors to System.Drawing.Color</title>
		<link>http://www.whypad.com/posts/aspnetc-convert-html-colors-to-systemdrawingcolor/171/</link>
		<comments>http://www.whypad.com/posts/aspnetc-convert-html-colors-to-systemdrawingcolor/171/#comments</comments>
		<pubDate>Wed, 14 May 2008 16:34:58 +0000</pubDate>
		<dc:creator>Byron Bennett</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>

		<guid isPermaLink="false">http://www.whypad.com/?p=171</guid>
		<description><![CDATA[HTML colors come in all sorts of flavors: hexadecimal (#0066ff), named colors (e.g. LightSkyBlue&#8230;here&#8217;s the link). Many people may be very comfortable with working with HTML colors, so if you&#8217;re in C# and needing to get the .Net equivalent, you can translate directly from HTML colors with System.Drawing.ColorTranslator.FromHtml(). Here&#8217;s the usage: System.Drawing.Color myColor = System.Drawing.ColorTranslator.FromHtml("Red"); [...]]]></description>
			<content:encoded><![CDATA[<p>HTML colors come in all sorts of flavors: hexadecimal (#0066ff), named colors (e.g. LightSkyBlue&#8230;here&#8217;s the <a title="HTML Color names" href="http://www.w3schools.com/HTML/html_colornames.asp">link</a>).</p>
<p>Many people may be very comfortable with working with HTML colors, so if you&#8217;re in C# and needing to get the .Net equivalent, you can translate directly from HTML colors with System.Drawing.ColorTranslator.FromHtml().  Here&#8217;s the usage:<span id="more-171"></span></p>
<pre>
System.Drawing.Color myColor = System.Drawing.ColorTranslator.FromHtml("Red");
or
System.Drawing.ColorTranslator.FromHtml("#ff0000");</pre>
<p>Controls require the System.Drawing.Color type:</p>
<pre>
TextBox t = new TextBox();
t.BackColor = System.Drawing.Color.Red;
</pre>
<p>Also useful is System.Drawing.ColorConverter. Here&#8217;s the <a href="http://msdn.microsoft.com/en-us/library/system.drawing.colorconverter(VS.80).aspx">MSDN link </a>for that. But working from the HTML colors may be more familiar to you.</p>
<img src="http://www.whypad.com/?ak_action=api_record_view&id=171&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.whypad.com/posts/aspnetc-convert-html-colors-to-systemdrawingcolor/171/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
