1

I have a form with various inputs, some of which use the 'title' property to put hinted text inside the input. When the form is submitted, an email is sent with the values of each input. However, if the field has not been filled out, it will use the title as the value. I can check for an empty field and remove the value manually like so:

if (a_eventSelect.Attributes["title"] == a_eventSelect.Value)
     {
          a_eventSelect.Value = "";
     }

The problem with that is if the form has many inputs checking for each one could become unnecessarily cumbersome. I started making a function to check each control and clear if it was empty.

protected void Page_Load(object sender, EventArgs e)
{
     //initialize
     base.Initialize();

     //on reload of page
     if (IsPostBack)
     {
          //clear blank values
          clear(mainform);

          //send email
          SendEmail();

          //display thank you
          thankyou.Visible = true;

          //hide main
          main.Visible = false;
      }
}

public void clear(Control location)
{
     //for each control in location
     foreach (Control c in location.Controls)
     {
          //if the control has child controls
          if (c.HasControls())
          {
               //call function with new location
               clear(c);
          }
          //some code to check value and title
     }
}

What I can't seem to figure out is how to then take each control and actually compare it title and value or even change its value within the function. Does anyone have any ideas of what may help? Thanks in advance.

2 Answers 2

1

iterate on the main form controls, casting to a "root" class (i think Control will do it) and then check them

1
  • I'm not sure I understand what you're saying to do.
    – Nick Amor
    Jun 28, 2012 at 19:28
0

The answer is based two points i noticed

  1. You want clear all Input controls
  2. The line of code (a_eventSelect.Attributes["title"] == a_eventSelect.Value) says you are using HtmlInputControl in your webform

Example that will set the value to empty

    private void SetControlValueToEmpty()
    {
        IEnumerable<HtmlInputControl> htmlInputControls = form1.Controls.OfType<System.Web.UI.HtmlControls.HtmlInputControl>();
        foreach (var htmlInputControl in htmlInputControls)
        {
            if (htmlInputControl.Attributes["title"] == htmlInputControl.Value)
            {
                htmlInputControl.Value = "";
            }
        }
    }
1
  • Whenever I use a message box to check which controls this is affecting, the only control that appears is 'postbacks'. Do you have any idea why this is?
    – Nick Amor
    Jul 2, 2012 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.