GridView中的下拉列表不尊重选择
本文关键字:选择 下拉列表 GridView | 更新日期: 2023-09-27 18:17:36
. NET页面,我有一个gridview,其中包含一个下拉列表在其列之一。而gridview中的其他列是绑定的,下拉列表是NOT,只包含3个预设值"Frank","Yes"answers"No"。(以"Frank"为例,这样我就不会从我喜欢的空白选项中得到错误的读数)
<asp:GridView ID="testGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Code1" HeaderText="Code1" />
<asp:BoundField DataField="Code2" HeaderText="Code2" />
<asp:TemplateField HeaderText="Like Frank?">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlLikeFrank">
<asp:ListItem>Frank</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:HyperLinkField ... HeaderText="File" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="cmdUpdate" runat="server" Text="Update" OnClick="cmdUpdate_Click" /></div>
我不需要这些下拉列表上的AutoPostBack,因为我只想在单击按钮cmduupdate时考虑它们的选定值。
protected void cmdUpdate_Click(object sender, EventArgs e)
{
bool likesFrank = false;
string selected = "";
DropDownList ddl = null;
GridViewRow current = null;
// Go through each row and check the dropdown list.
for (int i = 0; i < testGrid.Rows.Count; i++)
{
current = testGrid.Rows[i];
...
ddl = (DropDownList)(current.FindControl("ddlLikeFrank"));
/* THIS FOR LOOP IS WAS USED FOR INVESTIGATING THIS ISSUE*/
for (int j = 0; j < ddl.Items.Count; j++)
{
if (ddl.Items[j].Selected)
{
continue;
}
}
selected = ddl.SelectedItem.Value;
switch (selected)
{
case LIKE: // "Yes"
likesFrank = true;
break;
case DONT_LIKE: // "No"
likesFrank = false;
break;
default: // If nothing is selected in the drop-down list, move on.
continue;
} // end switch block
/* USE THE DERIVED BOOLS HERE */
} // end for loop on grid rows
} // end method cmdUpdateClick
问题是:无论下拉列表中选择了什么项目,我的页面都认为它们中的每一个都被设置为"Frank",即第一个项目。此外,如果我在for循环的顶部放置一个断点,表示为"调查代码",然后与页面进行如下交互:
- 为不同的行选择不同的下拉列表选项
- 点击更新按钮。
…我的调试代码告诉我没有选择任何项!没有一个!!我可以在页面上看到2个是和3个否,我在VS2008中的调试工具告诉我每个下拉列表都没有选择。
这怎么可能?(注:我已经尝试过这个与EnableViewState设置为true,并与EnableViewState甚至没有在页面标题中提到。)
谢谢。
受此打击。将基于索引的for替换为
foreach(GridViewRow row in grid.Rows)
{
var ddlVal = ((DropDownList)row.FindControl("yourId")).SelectedItem.Value;
}
<< use SelectedItem.Value - I think this will solve your problem >>
更新:
要更新ddl的选定索引,需要进行回发。支持将autopostback = true。但是在这里,您已经提到您不想要自动回发,因为这是唯一的解决方案,所以将gridview放在updatepanel中以抑制回发
服务器端
protected void btnSaveRankChanges_Click(object sender, EventArgs e)
{
foreach (GridViewRow grv in GridViewRankChanges.Rows)
{
DropDownList dropDownChangeRank = (DropDownList)grv.FindControl("DropDownListRank");
StudentInfoObject.RankID = Convert.ToInt32(dropDownChangeRank.SelectedValue);
}
}
在网格<asp:TemplateField HeaderText="Select New Rank">
<ItemTemplate>
<asp:DropDownList ID="DropDownListRank" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
打开按钮点击
<asp:Button ID="btnSaveRankChanges" runat="server" Text="Submit" ValidationGroup="RankChanges"
class="accordionHeader" Height="27px" OnClick="btnSaveRankChanges_Click" OnClientClick="LoadImage()" />
下拉菜单以这种方式绑定
/// <summary>
/// bind dropdown with rank in grid
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridViewRankChanges_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList drdList;
// Nested DropDownList Control reference is passed to the DrdList object. This will allow you access the properties of dropdownlist placed inside the GridView Template column.
if (e.Row.RowType == DataControlRowType.DataRow)
{
//bind dropdown rank
drdList = (DropDownList)e.Row.FindControl("DropDownListRank");
drdList.DataSource = RankList.GetRankList(false);
drdList.DataTextField = "Rank";
drdList.DataValueField = "RankID";
drdList.DataBind();
drdList.Items.Insert(0, new ListItem("Select", "0"));
}
}
对我有用,希望对你也有用。
我认为你在每个帖子上重新绑定网格,这将重新加载整个数据以及下拉框。例如,请检查是否(!page.IsPostBack)之前做gridview的数据定位。如果这不是问题,请告诉我。