title: Problems reading form variables in asp.net C#
body: <p><strong>Background</strong>: I've been asked to work on a legacy asp.net application for which there is no documentation and the original programmers are not available for consultation.  I'm primarily a Perl programmer, primarily on *nix systems, so while I've no major problems with the actual logic of the system, I'm getting tied in knots by some of the "bare bones" stuff underneath.</p>
  
  <p><strong>Outline</strong>: I'm trying to extend an existing aspx form to increase its functionality, the form is part of a system for purchasing telephone numbers.  At the moment, the form when first called shows a list of locations and telephone area codes in an HTML drop-down list, and expects the user to select one.  When a value is selected (or selected and the select button clicked, if JS is disabled) and the form submitted, the page in postback mode then provides an appropriate redirect to a purchase page, with various values set in the query string.  I want to extend the functionality by adding a second row of controls, on the same form, comprising a text box for users to enter an area code, and a "Search" button. The idea is that when a user does this, the system will then do a DB search for that area code, and if a match is found, generate the appropriate redirect to the page selling that area.  All the code is (and will be) in C#.</p>
  
  <p><strong>The problem</strong>: Having found a lot of useful info here on Stack Overflow on extracting form variables from asp.net forms, I tried to retrieve my new variables using Request.form["variable_name"].  But it soon became clear the values weren't coming through. </p>
  
  <p>Here's the aspx page code for the new bit of the form:</p>
  
  <pre><code>&lt;tr&gt;
      &lt;td align='left'&gt;&lt;b&gt;STD code:&lt;/b&gt;&lt;/td&gt;
      &lt;td align='left'&gt;
          &lt;asp:TextBox id="STD_Code_Search"
               AutoPostBack="True"
               Columns="7"
               Text=""
               TextMode="SingleLine"
               Wrap="False"
               runat="server"/&gt;
      &lt;/td&gt;
          &lt;td&gt;&lt;asp:Button ID="Button2" runat="server" Text="Search" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>I expected to be able to pull those variables back via</p>
  
  <pre><code>Button2_Clicked = Request.Form["Button2"]
  STD_Code_Search = Request.Form["STD_Code_Search"]
  </code></pre>
  
  <p>However nothing ever came through.  Based on more searches, I found this code to display all the variables coming into the form:</p>
  
  <pre><code>    foreach(string key in Request.Form.Keys)
      {
          LOG.WriteLine(key + ": " + Request.Form[key] + "&lt;br/&gt;");
      }
  </code></pre>
  
  <p>The LOG object is a StreamWriter going to a text file, which seems to work fine.</p>
  
  <p>However the data in the file looks like this:</p>
  
  <pre><code>ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$STD_Code_Search: 01952&lt;br/&gt;
  ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$Button2: Search&lt;br/&gt;
  </code></pre>
  
  <p>Fair enough, I then tried to use the following to retreive the data in C#</p>
  
  <pre><code>STD_Search = Request.Form["ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$STD_Code_Search"];
  </code></pre>
  
  <p>That does actually work and if I send STD_Search to the StreamWriter LOG I can see the entered value in my text file.</p>
  
  <p>However if I then try and read a second value (in this case I'm trying to catch the value of Button2, so I can trigger specific behaviour if that button, rather than the original one, is clicked), using this code:</p>
  
  <pre><code>STD_Search = Request.Form["ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$STD_Code_Search"];
  Button2_Clicked = Request.Form["ctl00$ctl00$ctl00$ContentPlaceHolder1$Main$Range_Data$Button2"];
  </code></pre>
  
  <p>Then while the asp.net processes all appear to complete correctly (no errors or page crashes), the writing of the text file stops dead and no data at all is written.</p>
  
  <p><strong>Questions</strong>:</p>
  
  <ol>
  <li><p>I'm sure I shouldn't have to include all the placeholder stuff
  (reading on here I gather that's part of the underlying framework
  and asp should in fact just present me with the named variables, the
  same as a Perl or PHP system would do with a plain HTML form), any
  pointers as to why that might be happening and if there's anything
  else I should be doing to process / extract the data?</p></li>
  <li><p>Why does it all drop dead when I try and read in a second variable,
  despite working (for some large values of working) when I only read
  in one?</p></li>
  <li><p>Are there any recommended books on asp.net and C#?  I have
  Sitepoint's "Build your own asp.net 4 website using C# &amp; VB", which
  has helped a bit but isn't so useful for trying to take someone
  else's code apart and re-build it.</p></li>
  </ol>
  
  <p><strong>Prior research</strong>: I've done extensive searching on this, however most of the answers I've seen so far have been related to people using asp.net to process data submitted from external sites, etc, or by scripts using post directly.  All the sites I've looked at for tutorials on asp.net appear to say I should just use Request.Form["variable_name"], so there appears to be something else going on here.</p>