网格视图编辑问题
本文关键字:问题 编辑 视图 网格 | 更新日期: 2023-09-27 18:30:00
我有可编辑的网格视图,如果我点击任何一行的编辑,比如"x",它会在编辑模式下打开,但如果我点击其他任何一行,比如"y","x"应该取消编辑。但在我的代码中,单击"y"后,这两行仍处于编辑模式。
protected void gvViewAdmins_RowEditing(object sender, GridViewEditEventArgs e)
{
gvViewAdmins.EditIndex = e.NewEditIndex;
Label lblEmailId = gvViewAdmins.Rows[e.NewEditIndex].FindControl("gvlblEmail") as Label;
lblEmailId.Visible = false;
ViewState["currentEmailId"] = lblEmailId.Text;
TextBox textboxEmailId = gvViewAdmins.Rows[e.NewEditIndex].FindControl("gvtbEmailId") as TextBox;
textboxEmailId.Text = ViewState["currentEmailId"].ToString();
textboxEmailId.Visible = true;
Label lblRole = gvViewAdmins.Rows[e.NewEditIndex].FindControl("gvlblRole") as Label;
lblRole.Visible = false;
ViewState["currentRole"] = lblRole.Text;
DropDownList dropdownRoles = gvViewAdmins.Rows[e.NewEditIndex].FindControl("gvddlRoles") as DropDownList;
this.PopulateRole(dropdownRoles);
dropdownRoles.Visible = true;
this.SelectRoleDropDownValue(dropdownRoles);
LinkButton lbtnUpdate = gvViewAdmins.Rows[e.NewEditIndex].FindControl("lbtnUpdate") as LinkButton;
LinkButton lbtnCancel = gvViewAdmins.Rows[e.NewEditIndex].FindControl("lbtnCancel") as LinkButton;
LinkButton lbtnEdit = gvViewAdmins.Rows[e.NewEditIndex].FindControl("lbtnEdit") as LinkButton;
lbtnUpdate.Visible = true;
lbtnCancel.Visible = true;
lbtnEdit.Visible = false;
}
我的代码出了什么问题?
查看您的代码后,您似乎没有使用gridview的默认编辑功能。你只是设置控件可见属性打开和关闭。
更好的方法是,分别定义项目模板和编辑项目模板,如
标记:
<asp:GridView ID="objGridView" runat="server" AutoGenerateColumns="false" onRowEditing="objGridView_RowEditing">
<columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="lblEmailID" runat="server" Text='<%#Eval("<<EmailID Field>>")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmailID" runat="server" Text='<%#Eval("<<EmailID Field>>")%>'/>
</EditItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
代码背后:
protected void objGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
objGridView.EditIndex = e.NewEditIndex;
<<BindGrid Again>>
}