gpt4 book ai didi

asp.net - 如何从 gridbox 中检索更新的数据并在数据库中更新它

转载 作者:行者123 更新时间:2023-12-01 15:58:47 26 4
gpt4 key购买 nike

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

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

我的代码是下面类中的行更新方法。我使用 Entity Framework 作为在数据库中进行操作的方法。

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

最佳答案

你在 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();
}
}

关于asp.net - 如何从 gridbox 中检索更新的数据并在数据库中更新它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17966873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com