在ASP.NET页面上,我有一个gridview,其中包含一个列中的下拉列表。虽然gridview中的其他列是数据绑定,但下拉列表不是,并且只包含3个预设值“Frank”,“Yes”和“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,因为我只想在单击按钮cmdUpdate时考虑他们选择的值。
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。)
感谢。
答案 0 :(得分:0)
受此打击。用此
替换索引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。但是在这里,你提到你不想要autopostback,因为这是唯一的解决方法,所以将gridview放在updatepanel中以抑制回发
答案 1 :(得分:0)
在服务器端
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()" />
Dropdown以这种方式绑定
/// <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"));
}
}
它对我有用,希望它对你也有帮助。
答案 2 :(得分:0)
我认为你在每个帖子上重新绑定网格,这将重新加载整个数据以及下拉列表。对于前者在进行gridview的数据绑定之前,请检查是否(!page.IsPostBack)。 如果这不是问题,请告诉我。