title: On ASP.NET textbox losing property after PostBack occurs
body: <p>On my .aspx page I have certain fields that I want disabled so I flag these with a <code>carvedinstone</code> property and dynamically apply <code>disabled="disabled"</code> in the codebehind so the pre-populated value can't be changed by a user. <strong>Note:</strong> <em>This is legacy code 10+ years old so the "Why's?" can only be answered with "It was 2004, someone else did it this way, shrug".</em></p>
  
  <p>When a PostBack occurs during validation the <code>disabled="disabled"</code> property is removed from the fields that had it. I want to prevent that from happening.</p>
  
  <p><strong>.aspx page</strong></p>
  
  <pre><code>&lt;asp:TextBox
      ID="fEE_SSN"
      runat="server"
      alias="fSSN"
      prefix="[0]"
      strip="-"
      stripfor="both"
      width="160px"
      maxlength="11"
      carvedinstone="thirdparty" /&gt;
  </code></pre>
  
  <p>The .aspx page also has required field and regular expression validators:</p>
  
  <pre><code>&lt;asp:RequiredFieldValidator
      ID="rfv_fSSN"
      runat="server"
      display="Dynamic"
      errormessage="Please enter the Employee's SSN"
      controltovalidate="fEE_SSN"
      cssclass="error"
      enabled="false"&gt;[*]&lt;/asp:RequiredFieldValidator&gt;
  
  &lt;asp:RegularExpressionValidator
      ID="rev_fSSN"
      runat="server" 
      display="Dynamic"
      errormessage="Please enter a valild SSN"
      controltovalidate="fEE_SSN"
      validationexpression="\d{3}-\d{2}-\d{4}"
      cssclass="error"
      enabled="false"&gt;[#]&lt;/asp:RegularExpressionValidator&gt;
  </code></pre>
  
  <p>The function for applying <code>disabled="disabled"</code> is being called at <code>Page_Load</code>. <code>Page_Load</code> calls the <code>validateValidators()</code> method which calls the validation check where the disabling of the field happens:</p>
  
  <p><strong>Page_Load() method</strong>:</p>
  
  <pre><code>protected virtual void Page_Load(object sender, System.EventArgs e)
  {
      SimpleTracer.Write(MethodBase.GetCurrentMethod().Name + " Begin");
      EnsureChildControls();
      validateValidators();
  }
  </code></pre>
  
  <p><strong>validateValidators() method</strong>:</p>
  
  <pre><code>protected virtual void validateValidators()
  {
      foreach (BaseValidator validator in Validators)
      {
          if (!validator.Enabled)
              continue; // validator has already been disabled
  
          bool enable = validateValidator(validator, form);
          validator.Enabled = enable;
      }
  }
  </code></pre>
  
  <p><strong>validateValidator() method</strong>:</p>
  
  <pre><code>protected virtual bool validateValidator(BaseValidator validator, Control parent)
  {
      WebControl control = null;
  
      if (validator is CustomValidator)
      {
          string controlId = validator.Attributes["checkboxgroupprefix"];
          if (!String.IsNullOrEmpty(controlId))
              control = parent.FindControl(controlId + "1") as WebControl;
          else
              control = parent.FindControl(validator.ControlToValidate) as WebControl;
  
          if (control != null)
          {
              if (control.GetType().Name == "TextBox")
              {
                  if (CheckCarvedInStoneControl(control))
                      return false;
              }
              if (control.GetType().Name == "RequiredFieldValidator")
              {
                  if (CheckCarvedInStoneValidator(control))
                      return false;
              }
              if (IsReadOnlyForSubmissionType(control, m_SubmissionType))
                  return false;
          }
  
          if (control == null &amp;&amp; parent.HasControls())
          {
              foreach (Control child in parent.Controls)
                  if (!validateValidator(validator, child))
                      return false;
          }
          return true;
      }
  </code></pre>
  
  <p>If anyone has any ideas how to preserve that property after PostBack I'd love to hear from you. Thanks, all!</p>