希望有人可以帮我解决这个问题。我有一个带有x行数的GridView,每行有4列,包含3个DropDownLists和一个Label,用于保存3个DropDownLists的选定值之和。我的问题是如何实现这一目标?我已经尝试了DropDownLists的SelectedIndexChanged路由,但我没有得到我想要的结果。它们不是被添加,而是被下一个选择覆盖。我究竟做错了什么?这是我到目前为止所做的:
protected void ddlSelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvStuRoster.Rows)
{
double scores = 0;
DropDownList ddlFocus = (DropDownList)row.FindControl("ddlFocus");
DropDownList ddlEvidence = (DropDownList)row.FindControl("ddlEvidence");
DropDownList ddlConventions = (DropDownList)row.FindControl("ddlConventions");
//DropDownList ddlScores = (DropDownList)row.FindControl("ddlScores");
Label lblScores = (Label)row.FindControl("lblScores");
if (ddlFocus.SelectedIndex > 0 && ddlFocus.SelectedValue != "-1")
{
scores += Convert.ToDouble(ddlFocus.SelectedValue);
}
else if (ddlFocus.SelectedValue == "-1")
{
scores = -1;
}
if (ddlEvidence.SelectedIndex > 0 && ddlEvidence.SelectedValue != "-1")
{
scores += Convert.ToDouble(ddlEvidence.SelectedValue);
}
else if (ddlEvidence.SelectedValue == "-1")
{
scores = -1;
}
if (ddlConventions.SelectedIndex > 0 && ddlConventions.SelectedValue != "-1")
{
scores += Convert.ToDouble(ddlConventions.SelectedValue);
}
else if (ddlConventions.SelectedValue == "-1")
{
scores = -1;
}
lblScores.Text = scores == -1 ? "" : Convert.ToString(scores);
}
}
更新
在内部移动scores
和lblScores
标签之后,所有内容都按预期工作,除了在任何DropDownList的第一个选择上,除了同一行中的那个,所有其他GridView中的标签值设置为0.为什么会发生这种情况?
先谢谢你的帮助!
答案 0 :(得分:1)
每个请求都会将scores
变量重置为0(每次更改下拉列表)。然后,您只需将scores
设置为当前更改下拉列表的值。如果下拉列表中有选择,您可以做的是获取每个请求的每个下拉列表的值。假设你的下拉每个都将他们的第一个项目设置为"选择一个值"或者其他什么,您可以检查SelectedIndex
是否大于0,如果是,则意味着选择。
foreach (GridViewRow row in gvStuRoster.Rows)
{
double scores = 0;
DropDownList ddlFocus = (DropDownList)row.FindControl("ddlFocus");
DropDownList ddlEvidence = (DropDownList)row.FindControl("ddlEvidence");
DropDownList ddlConventions = (DropDownList)row.FindControl("ddlConventions");
Label lblScores = (Label)row.FindControl("lblScores");
if (ddlFocus.SelectedIndex > 0)
{
scores += Convert.ToDouble(ddlFocus.SelectedValue);
}
if (ddlEvidence.SelectedIndex > 0)
{
scores += Convert.ToDouble(ddlEvidence.SelectedValue);
}
if (ddlConventions.SelectedIndex > 0)
{
scores += Convert.ToDouble(ddlConventions.SelectedValue);
}
lblScores.Text = scores == 0 ? "" : Convert.ToString(scores);
}
答案 1 :(得分:0)
在戴夫的帮助下,我得到了我想要的结果。这是我的最终代码:
protected void ddlSelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvStuRoster.Rows)
{
double scores = 0;
DropDownList ddlFocus = (DropDownList)row.FindControl("ddlFocus");
DropDownList ddlEvidence = (DropDownList)row.FindControl("ddlEvidence");
DropDownList ddlConventions = (DropDownList)row.FindControl("ddlConventions");
DropDownList ddlScores = (DropDownList)row.FindControl("ddlScores");
Label lblScores = (Label)row.FindControl("lblScores");
if (ddlFocus.SelectedIndex > 0 && ddlFocus.SelectedValue != "-1")
{
scores += Convert.ToDouble(ddlFocus.SelectedValue);
}
if (ddlEvidence.SelectedIndex > 0 && ddlEvidence.SelectedValue != "-1")
{
scores += Convert.ToDouble(ddlEvidence.SelectedValue);
}
if (ddlConventions.SelectedIndex > 0 && ddlConventions.SelectedValue != "-1")
{
scores += Convert.ToDouble(ddlConventions.SelectedValue);
}
ddlScores.SelectedValue = scores == 0 ? "" : Convert.ToString(scores);
}
}
再次感谢,戴夫!