您好我想知道是否有任何简化以下IF声明的方法。我有47个问题要经历,我真的不想让这个重复。我不熟悉c#而且想要一只手。
protected void GA_Total(object sender, EventArgs e)
{
// Generalized Anxiety Total
int GATotal = 0;
Label1.Text = Convert.ToString(GATotal);
//-----------Question 1----------------
if (RadioButtonList.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 13----------------
if (RadioButtonList1.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList1.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList1.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList1.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 22----------------
if (RadioButtonList2.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList2.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList2.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList2.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 27----------------
if (RadioButtonList3.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList3.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList3.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList3.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 35----------------
if (RadioButtonList4.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList4.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList4.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList4.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//-----------Question 37----------------
if (RadioButtonList5.Items[0].Selected)
{
GATotal = GATotal + 0;
}
if (RadioButtonList5.Items[1].Selected)
{
GATotal = GATotal + 1;
}
if (RadioButtonList5.Items[2].Selected)
{
GATotal = GATotal + 2;
}
if (RadioButtonList5.Items[3].Selected)
{
GATotal = GATotal + 3;
}
//------------Results-------------------
Label1.Text = Convert.ToString(GATotal);
}
答案 0 :(得分:1)
您基本上是将项目的索引添加到GATOTAL,为什么不直接使用RadioButtonList的SelectedIndex
e.g。
GATotal += RadioButtonList.SelectedIndex +
RadioButtonList1.SelectedIndex +
RadioButtonList2.SelectedIndex +
RadioButtonList3.SelectedIndex +
RadioButtonList4.SelectedIndex +
RadioButtonList5.SelectedIndex;
答案 1 :(得分:0)
GATotal += RadioButtonList.SelectedIndex + RadioButtonList1.SelectedIndex ...
答案 2 :(得分:0)
如果您想为所选项目分配不同的值,这种方法将变得简单:
protected void GA_Total(object sender, EventArgs e)
{
// Generalized Anxiety Total
int GATotal = 0;
GATotal += sumSelectedItems(radioButtonList);
GATotal += sumSelectedItems(radioButtonList1);
GATotal += sumSelectedItems(radioButtonList2);
GATotal += sumSelectedItems(radioButtonList3);
GATotal += sumSelectedItems(radioButtonList4);
GATotal += sumSelectedItems(radioButtonList5);
Label1.Text = GATotal.toString();
}
private int sumSelectedItems(RadioButtonList list)
{
int sum = 0;
for (int i = 0; i < list.Items.Count; i++)
{
if (list.Items[i].Selected)
{
sum += i;
}
}
return sum;
}
答案 3 :(得分:0)
假设您拥有表
中的所有单选按钮<asp:Table ID="Table1" runat="server"><asp:RadioButtonList runat="server"></asp:RadioButtonList></asp:Table>
以下代码适用于任意数量的单选按钮。
foreach (Control ctrl in Table1.Controls)
{
if (ctrl is RadioButtonList)
{
RadioButtonList rbl = (RadioButtonList)ctrl;
for (int i = 0; i < rbl.Items.Count; i++)
{
if (rbl.Items[i].Selected)
{
GATotal+= RadioButtonList.SelectedIndex;
}
}
}
}