我想从不同的下拉列表中添加选定的值。我的代码有问题吗?因为当我点击按钮添加它们时没有显示任何内容。继承人的代码
protected void btnSum_Click(object sender, EventArgs e)
{
int a = Int32.Parse(ddlEval1.SelectedValue);
int b = Int32.Parse(ddlEval2.SelectedValue);
int c = Int32.Parse(ddlEval3.SelectedValue);
int d = Int32.Parse(ddlEval4.SelectedValue);
int f = Int32.Parse(ddlEval5.SelectedValue);
txtScore.Text = Convert.ToString(a + b + c + d + f);
}
答案 0 :(得分:0)
请尝试:
int sum = a + b + c + d + f; //or double depending on your values.
txtScore.Text = Convert.ToString(sum);
答案 1 :(得分:0)
我认为这会有效,但请考虑一些错误处理。当所选项目不包含整数表示时,您将获得异常。
protected void btnSum_Click(object sender, EventArgs e)
{
int a = Int32.Parse(ddlEval1.SelectedItem.ToString());
int b = Int32.Parse(ddlEval2.SelectedItem.ToString());
int c = Int32.Parse(ddlEval3.SelectedItem.ToString());
int d = Int32.Parse(ddlEval4.SelectedItem.ToString());
int f = Int32.Parse(ddlEval5.SelectedItem.ToString());
txtScore.Text = Convert.ToString(a + b + c + d + f);
}
答案 2 :(得分:-2)
替换:
txtScore.Text = Convert.ToString(a + b + c + d + f);
使用:
txtScore.Text = "" + (a + b + c + d + f);