如何从网格箱中检索更新的数据并在数据库中更新它

时间:2013-07-31 09:40:34

标签: asp.net entity-framework gridview

我的代码实际上返回了之前从数据库获取的数据,但无法从gridcontrol获取更新的数据。

这是我的Default.aspx,其中已经完成了gridview的设计。

 <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
 </asp:Content>
 <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowupdating="GridView1_RowUpdating" onrowediting="GridView1_RowEditing"
    onrowupdated="GridView1_RowUpdated" onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:CommandField ShowEditButton="true" ShowDeleteButton="true" ShowInsertButton="true" />
        <asp:TemplateField HeaderText="Id" SortExpression="Id">
            <ItemTemplate>
                <asp:Label ID="lbl_Id" Text='<%# Bind("id")%>' runat="server">  </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <ItemTemplate>
                <asp:Label ID="lbl_name" Text='<%# Bind("Name")%>' runat="server"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_name" Text='<%# Bind("Name")%>' runat="server"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Address" SortExpression="address">
            <ItemTemplate>
                <asp:Label ID="lbl_address" Text='<%# Bind("address")%>' runat="server"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txt_address" Text='<%# Bind("address")%>'    runat="server"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
   </asp:GridView>
   <asp:Label ID="lbl_msg" runat="server"></asp:Label>
   </asp:Content>



 My.cs File

我的代码是以下类中的行更新方法。 我使用实体框架作为在数据库中进行操作的方法。

 using System; 
 using System.Collections.Generic;
 using System.Linq;
  using System.Web;
  using System.Web.UI;
 using System.Web.UI.WebControls;
     using DatabaseModel;

   public partial class _Default : System.Web.UI.Page
   {
   DatabaseEntities obj;

   protected void Page_Load(object sender, EventArgs e)
  {
   obj = new DatabaseEntities();
   GridView1.DataSource = obj.Records.ToList();
  GridView1.DataBind();
  }
  string data;

  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {

  }

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
   GridViewRow row = GridView1.Rows[e.RowIndex];
   TextBox name = row.FindControl("txt_name") as TextBox;
  TextBox address = row.FindControl("txt_address") as TextBox;
  Label id = row.FindControl("lbl_Id") as Label;
  int no = int.Parse(id.Text);
   Record rec = obj.Records.First(x => x.Id == no);
  rec.Name = name.Text;
  rec.Address = address.Text;
   obj.SaveChanges();
  data = name.Text;
   lbl_msg.Text = name.Text;
  }

  protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
  {
  }

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
  }
 } 

2 个答案:

答案 0 :(得分:1)

你在linq查询中遇到一些问题,使用lambda表达式并使用它:

protected void Page_Load(object sender, EventArgs e)
{
    // do not rebind the data on postback
    if (!IsPostBack)
    {
        obj = new DatabaseEntities();
        GridView1.DataSource = obj.Records.ToList();
        GridView1.DataBind();
    }
}

答案 1 :(得分:0)

这可能是因为数据在触发GridView1_RowUpdating之前反弹。尝试更改Page_Load方法,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    // do not rebind the data on postback
    if (!IsPostBack)
    {
        obj = new DatabaseEntities();
        GridView1.DataSource = obj.Records.ToList();
        GridView1.DataBind();
    }
}