【问题标题】:how to retrieve the updated data from the gridbox and update it in the database如何从网格框中检索更新的数据并在数据库中更新
【发布时间】:2013-07-31 18:31:00
【问题描述】:

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

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

【问题讨论】:

  • asp.netasp.net-mvcasp-classic 之间存在细微差别。请在 StackOverflow 上提问时适当地使用这些标签来识别您的目标平台。

标签: asp.net entity-framework gridview


【解决方案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();
    }
}

【讨论】:

    【解决方案2】:

    这可能是因为在触发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();
        }
    }
    

    【讨论】:

    • 我试过了。它确实有效,但在 LINQ 查询中显示空异常
    • 你说得对,我现在明白了。 obj 变量仅在回发时实例化。只需移动“obj = new DatabaseEntities();” if 语句上方的行。
    猜你喜欢
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2016-04-29
    相关资源
    最近更新 更多