3

我的代码实际上返回了以前从数据库中获取的数据,但无法从网格控件中获取更新的数据。

这是我的 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)
 {
  }
 } 
4

2 回答 2

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();
    }
}
于 2013-07-31T12:06:41.597 回答
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();
    }
}
于 2013-07-31T09:58:58.817 回答