带中继器的数学2 [英] math with repeaters2

查看:94
本文介绍了带中继器的数学2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,该表单允许用户使用转发器动态添加其他文本框.
在新的文本框中,用户仅输入数字.我知道如何计算第一部分.

我需要代码示例:

textbox1 = MBE,textbox2 =是或否,textbox3 FBE,textbox4 =是或否,textbox5 =本地,textbox6 =是或否
textbox7 = txtmbetotal,
textbox8 = txtwfbetotal,
textbox9 = txtloctotal.


文字等于是的地方(textbox2 ==是")
将textbox1(txtman)中的数字相加,并将textbox7(txtmantotal)中的总数相加

我有这样的东西.

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();
              }



这是我的问题
该代码正在计算我需要的所有所有数字,以便首先检查做MBE.TEXT的静态对象,然后遍历我的转发器以选择是".如果在MBE上为是",则将总计添加到mbetotal中;如果在FBE中为是",则将总计添加到FBE中;如果在本地"中是,则将总计添加到本地总计中.

解决方案

"不是很了解您想要实现什么,但是我唯一看到的是极度冗余的代码.每个循环的内部包含三遍.

这段代码:

 字符串 mbetxt =((TextBox)item0.FindControl("  txtMBE")).文本;
                              如果(mbetxt.ToString()== " )
                              {
                                   foreach (RepeaterItem item1  in  Repeater1.Items)
                                  {
                                      // 字符串mbe =((TextBox)item1.FindControl("txtMBE")).Text; 
                                      字符串 amount =((TextBox)item1.FindControl(" )).Text;
                                      totalamount =  int  .Parse(Amount.Text);
                                      subamount = subamount +  int  .Parse(amount.ToString());
                                  }
                              }
                              其他
                              {
                                   foreach (RepeaterItem item1  in  Repeater1.Items)
                                  {
                                      // 字符串mbe =((TextBox)item1.FindControl("txtMBE")).Text; 
                                      字符串 amount =((TextBox)item1.FindControl(" )).Text;
                                      totalamount =  int  .Parse(Amount.Text);
                                      subamount = subamount +  int  .Parse(amount.ToString());
                                  }
                              } 


mbetxt是Yes还是其他东西真的重要吗?我认为无论是什么主意,将其首先分成多个块并使用这些功能执行实际任务都将更加容易.在内部循环中,您还使用了数量和金额,这真的是最清楚的吗?您的代码已经很长,不清楚,并且不能重用单个部分.此外,它容易出错,因为它很难维护,甚至几乎很难测试.您可以为内部循环创建一个函数,例如SumRepeaterItems,这也更易于记录,可以与其余部分分开进行测试.然后,您可以在代码中使用它三次,大幅缩短它并获得更多概述.好吧,只是一些启发您编码的指针:)

祝您好运!


只需添加一下,人们在哪种疯狂的UI中输入"Yes"作为文本,而不是输入复选框?在字符串上调用ToString是什么疯狂?当然没有人会使用此代码吗?我认为您需要与您的老师交谈,您显然是个初学者,他可以为您提供比我们更好的帮助.可以看到,是对转发器行中的所有控件进行迭代,从而找到所有文本框,并添加值.

这是一个网络应用程序吗?如果是这样,您如何动态添加文本框并维护viewstate?我认为不是,或者您已标记您的问题ASP.NET?


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.

解决方案

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:

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!


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.


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 ?


这篇关于带中继器的数学2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆