UserAccounts_RowEditing / Updating无法正常工作

时间:2014-10-17 13:17:31

标签: asp.net gridview

好吧,所以我有一个Gridview并添加了RowEditing和RowUpdating,但它不会真正编辑某些内容..这是我的代码:

protected void UserAccounts_RowEditing(object sender, GridViewEditEventArgs e)
{
     UserAccounts.EditIndex = e.NewEditIndex;
     BindUserAccounts();
}
protected void UserAccounts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     int index = UserAccounts.EditIndex;
     GridViewRow row = UserAccounts.Rows[e.RowIndex];
     username = UserAccounts.Rows[e.RowIndex].Cells[1].Text;
     email = ((TextBox)row.Cells[2].Controls[0]).Text;
     MembershipUser user = Membership.GetUser(username);
     if (user != null)
     {
         user.Email = email;
         Membership.UpdateUser(user);
         ActionStatus.Text = string.Format("User {0} details have been successfully updated!", username);
     }
     UserAccounts.EditIndex = -1;
     BindUserAccounts();
}

我在这里做错了什么?

编辑:这是我的BindUserAccounts:

private void BindUserAccounts()
{
     int totalRecords;
     UserAccounts.DataSource = Membership.FindUsersByName(this.UsernameToMatch + "%", this.PageIndex, this.PageSize, out totalRecords);
     UserAccounts.DataBind();

     bool visitingFirstPage = (this.PageIndex == 0);
     lnkFirst.Enabled = !visitingFirstPage;
     lnkPrev.Enabled = !visitingFirstPage;

     int lastPageIndex = (totalRecords - 1) / this.PageSize;
     bool visitingLastPage = (this.PageIndex >= lastPageIndex);
     lnkNext.Enabled = !visitingLastPage;
     lnkLast.Enabled = !visitingLastPage;
}

2 个答案:

答案 0 :(得分:0)

我认为应该是这样的

 protected void update_click_foredu(object sender, GridViewUpdateEventArgs e)
{

    Label edui = (Label)edugrid.Rows[e.RowIndex].FindControl("label");
    TextBox unitxt = (TextBox)edugrid.Rows[e.RowIndex].FindControl("txtuni");





    if (unitxt != null && costxt != null && startdatetxt != null && enddatetxt != null)
    {
        using (Entities1 context = new Entities1())
        {
            string eduID =  edui.Text;
            model obj = context.entitytabel.First(x =>  x.ID == eduID);
            obj.Univ_Name = unitxt.Text;

            context.SaveChanges();
            lblMessage.Text = "Saved successfully.";
            edugrid.EditIndex = -1;

            bindgrid();

        }
    }
}

这里我使用EF这样你可以找到文本框的控件并将其保存在gridview

希望这可以帮助你

答案 1 :(得分:0)

在编辑GridView并将“UserName”,“IsApproved”,“IsLockedOut”和“IsOnline”设置为ReadOnly =“true”后,它现在可以正常工作了