我有一个网格,我尝试将下拉列表绑定到FooterTemplate和EdiItemTemplate。
<asp:TemplateField HeaderText="Role" SortExpression="Role">
<HeaderStyle Width="100px" HorizontalAlign="Center" Wrap="False"></HeaderStyle>
<EditItemTemplate>
<asp:DropDownList ID="ddlRole" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlFRRole" runat="server">
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblRole" runat="server" Text='<%# Bind("[Role_Cd]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
我正在使用此代码,但没有运气。我无法将值绑定到下拉列表。如果我错了,任何人都可以纠正我吗?
protected void UPGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlRole = (DropDownList)e.Row.FindControl("ddlRole");
DropDownList ddlFRRole = (DropDownList)e.Row.FindControl("ddlFRRole");
ddlRole.DataSource = UPRepository.GetRoles();
ddlRole.DataTextField = "Role_Cd";
ddlRole.DataValueField = "Role_Cd";
ddlRole.DataBind();
ddlFRRole.DataSource = UPRepository.GetRoles();
ddlFRRole.DataTextField = "Role_Cd";
ddlFRRole.DataValueField = "Role_Cd";
ddlFRRole.DataBind();
}
}
答案 0 :(得分:5)
您应首先检查并比较RowIndex
与要编辑的GridView的EditIndex
,并将整个代码绑定到下拉列表中。
此外,要查找页脚中的下拉列表,您需要使用条件:
if (e.Row.RowType == DataControlRowType.Footer)
并且,要连续查找下拉列表,请使用
if (e.Row.RowType == DataControlRowType.DataRow)
在RowDataBound
GridView
事件中绑定您的下拉列表。
现在你的代码应该是::
protected void MyGridView_RowDataBound( object sender, GridViewRowEventArgs e)
{
if(MyGridView.EditIndex == e.Row.RowIndex ) //GET THE ROW TO BE EDITED
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlRole = (DropDownList)e.Row.FindControl("ddlRole");
// Rest of Code
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlFRRole = (DropDownList)e.Row.FindControl("ddlFRRole");
// Rest of code
}
}
另一种选择是使用以下条件来检查正在编辑的行::
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
或等同于:: if (e.Row.RowState == DataControlRowState.Edit)