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!
-
use repeater or any other control that allows you to replicate UIKris Ivanov– Kris Ivanov2011-02-28 15:05:35 +00:00Commented 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.John K.– John K.2011-02-28 15:09:02 +00:00Commented 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.Gerharddc– Gerharddc2011-03-01 13:01:18 +00:00Commented Mar 1, 2011 at 13:01
Add a comment
|
5 Answers
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");
}
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.