2

I am developing a program wth 30 text boxes and 30 check boxes next to them. I want people to check names and then press the send button. The program then saves the names in a txt file with a true or false statement next to them, it then uploads the file to a ftp server for me analize. The problem I am facing is that I don't want to write code for every text and check box to load and save it's value on the txt file. If I name the text boxes something like tbox1;tbox2;tbox3 etc. How would use a loop to say write the value of tbox i + ; + cbox i on line i of thing.txt or vice versa? Please any help would be grately apreciated because this will save me a lot of unnesacery code writing!

3
  • use repeater or any other control that allows you to replicate UI Commented Feb 28, 2011 at 15:05
  • If you can use jquery - check out sushantp.wordpress.com/2009/02/23/…. This allows you to easily get the list of checked (or unchecked) checkboxes from a page. Commented Feb 28, 2011 at 15:09
  • Thanks this looks great but have not lerned very far in this language so this is still a bit above me. Commented Mar 1, 2011 at 13:01

5 Answers 5

4

You should create a List<TextBox>, and populate it with your textboxes in the constructor.

You can then loop through the list and process the textboxes.

Sign up to request clarification or add additional context in comments.

Comments

2
        for (int i = 0; i <= count; i++)
        {
            TextBox textbox = (TextBox)Controls.Find(string.Format("tbox{0}", i),false).FirstOrDefault();
            CheckBox checkbox = (CheckBox)Controls.Find(string.Format("cbox{0}", i),false).FirstOrDefault();

            string s = textbox.Text + (checkbox.Checked ? "true" : "false");
        }

2 Comments

sorry, there was no IDE in that moment.
Thanks this is just what I need but thanks to all of you who gave me responses!
1

You can loop over all the controls on your form and retrieve the values from them based on their type/name.

Comments

0

Loop through controls in your form/control and investigate name:

        foreach (Control control in f.Controls)
        {
            if (control is TextBox)
            {
               //Investigate and do your thing
            }
        }

Comments

0

Assuming this is ASP.NET, you could use something like this:

StringBuilder sb = new StringBuilder();
for(int i = 1; i < 30; i++){
    TextBox tb = FindControl("TextBox" + i);
    Checkbox cb = FindControl("CheckBox" + i);
    sb.AppendFormat("TextBox{0}={1}; {2}", i, tb.Text, cb.Checked);
}
string result = sb.ToString();
// Now write 'result' to your text file.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.