Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form the form allows user to add additional text boxes dynamically using repeater.
in the new text boxes user to enter numbers only. I figure how to calculate the first part.

I need the code example:

textbox1 = MBE, textbox2 = Yes or No, textbox3 FBE, textbox4 = Yes or No, textbox5 =Local, textbox6 = Yes or No
textbox7 = txtmbetotal,
textbox8 = txtwfbetotal,
textbox9 = txtloctotal.


where ever text equal yes (textbox2 == "Yes")
add up the number in textbox1(txtman) and total in textbox7(txtmantotal)

I have something like this.

if (TotalAmount.Text != "")
                  {
                      if (MBE.Text == "Yes")
                      {
                          foreach (RepeaterItem item0 in Repeater1.Items)
                          {
                              string mbetxt = ((TextBox)item0.FindControl("txtMBE")).Text;
                              if (mbetxt.ToString() == "Yes")
                              {
                                  foreach (RepeaterItem item1 in Repeater1.Items)
                                  {
                                      //string mbe = ((TextBox)item1.FindControl("txtMBE")).Text;
                                      string amount = ((TextBox)item1.FindControl("txtAmount")).Text;
                                      totalamount = int.Parse(Amount.Text);
                                      subamount = subamount + int.Parse(amount.ToString());
                                  }
                              }
                              else
                              {
                                  foreach (RepeaterItem item1 in Repeater1.Items)
                                  {
                                      //string mbe = ((TextBox)item1.FindControl("txtMBE")).Text;
                                      string amount = ((TextBox)item1.FindControl("txtAmount")).Text;
                                      totalamount = int.Parse(Amount.Text);
                                      subamount = subamount + int.Parse(amount.ToString());
                                  }
                              }
                          }

                         // answers = totalamount + subamount;
                          //MBEAmount.Text = answers.ToString();
                      }
                      else
                      {
                          if (MBE.Text == "No")
                          {
                              foreach (RepeaterItem item0 in Repeater1.Items)
                              {
                                  string mbetxt = ((TextBox)item0.FindControl("txtMBE")).Text;
                                  if (mbetxt.ToString() == "Yes")
                                  {
                                      foreach (RepeaterItem item1 in Repeater1.Items)
                                      {
                                          //string mbe = ((TextBox)item1.FindControl("txtMBE")).Text;
                                          string amount = ((TextBox)item1.FindControl("txtAmount")).Text;
                                          totalamount = int.Parse(Amount.Text);
                                          subamount = subamount + int.Parse(amount.ToString());
                                      }
                                  }
                              }

                          }

                      }
                  }
                  answers = totalamount + subamount;
                  MBEAmount.Text = answers.ToString();
                  //addallfbe();
              }


here is my problem
The code is calculating every all numbers I need for it to check the static first which is doing MBE.TEXT then loop-through my repeater for Yes. if Yes is on MBE add and total to mbetotal, if Yes for FBE add total to FBE, and if Yes on Local add total to local total.
Posted
Updated 9-Aug-10 11:01am
v2

I don't really understand what you want to achieve but the only thing I see is extremely redundant code. The inner for each loop is included three times.

And this code:
C#
string mbetxt = ((TextBox)item0.FindControl("txtMBE")).Text;
                              if (mbetxt.ToString() == "Yes")
                              {
                                  foreach (RepeaterItem item1 in Repeater1.Items)
                                  {
                                      //string mbe = ((TextBox)item1.FindControl("txtMBE")).Text;
                                      string amount = ((TextBox)item1.FindControl("txtAmount")).Text;
                                      totalamount = int.Parse(Amount.Text);
                                      subamount = subamount + int.Parse(amount.ToString());
                                  }
                              }
                              else
                              {
                                  foreach (RepeaterItem item1 in Repeater1.Items)
                                  {
                                      //string mbe = ((TextBox)item1.FindControl("txtMBE")).Text;
                                      string amount = ((TextBox)item1.FindControl("txtAmount")).Text;
                                      totalamount = int.Parse(Amount.Text);
                                      subamount = subamount + int.Parse(amount.ToString());
                                  }
                              }

Does it really matter if mbetxt is Yes or something else? I think whatever the idea is, it would be much more easy to first divide it up into chunks and use those function to perform the actual task. In the inner loop you also use amount and Amount, is this really the most clear? Your code is already pretty long, unclear and not a single part can be reused. Further it is prone to errors because it's hard to maintain and almost even harder to test. You could make a function for the inner loop like SumRepeaterItems, this is also more self documenting and can be tested apart from the rest. You could then use it in your code three times, shortening it drastically and get more overview. Well, just some pointers to inspire your coding :)

Good luck!
 
Share this answer
 
Comments
postonoh 9-Aug-10 17:50pm    
Thanks.
postonoh 9-Aug-10 17:52pm    
will correct!
postonoh 16-Aug-10 16:04pm    
Reason for my vote of 5
fix problem
Just to add, what sort of insane UI has people entering Yes as text, instead of a checkbox ? What sort of insanity is it to call ToString on a string ? Surely no-one is going to ever use this code ? I think you need to talk to your teacher, you're obviously a beginner student, and he can help you better than we can.
 
Share this answer
 
Comments
Sean Ewington 10-Aug-10 9:30am    
Reason for my vote of 1
"Let's work to help developers, not make them feel stupid."
Serge V. Sushko 7-Apr-16 15:32pm    
[offtopic] Sean, this is regarding Oxetta Report generator article you've marked for deletion recently. I am Serge Sushko, the author of that article, and I will be pleased if you will contact me somehow. You may, for example, email me at serge at Oxetta dot com
What you want to do, as far as I can see, is to iterate over all the controls in the row of the repeater, and thus find all the textboxes, and add the values.

Is this a web app ? If so, how are you dynamically adding textboxes and maintaining viewstate ? I assume it's not, or you'd have marked your question ASP.NET ?
 
Share this answer
 
This is a web application and and using the repeater.control
 
Share this answer
 
v2
Comments
Christian Graus 9-Aug-10 17:01pm    
Don't press answer to post comments, push comment, or, in this case, edit your post so it's tagged properly
I never claim to be an expert, that why I am asking for help. And I have not a teacher to learn from. However because of your remark it apparent you have not the knowledge to instruct me, so please only comment on question you have answers to. I certainly was not looking for the negative conversation.

Thanks
 
Share this answer
 
Comments
Christian Graus 9-Aug-10 17:41pm    
Well, it seems you can't read. DON'T push 'answer' to make comments. Use the comment button. I gave you several correct answers, if you're teaching yourself, and not being paid for this code or doing it for a class, it's obviously all way over your head, so choose something a LOT simpler, I suggest learning to use C# properly first, THEN learning ASP.NET. That's not negative, it's damn good advice.
Hi,


Why dont you bind the data with the DataTable or your Custom IEnumerable and then use ItemDataBound event to get each Row.

By this, the code will look more simple and easier to handle. :rose:
 
Share this answer
 
Comments
postonoh 9-Aug-10 17:51pm    
will try thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900