网格视图编辑问题

时间:2012-04-03 04:51:00

标签: c# asp.net

我有可编辑的网格视图,如果我点击任何行中的编辑说“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;

}

我的代码有什么问题?

1 个答案:

答案 0 :(得分:2)

查看代码后,您似乎没有使用gridview的默认编辑功能。您只是打开和关闭控件可见属性。

更好的方法是,单独定义ItemTemplate和编辑项目模板,

标记:

<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>>
}