【发布时间】:2013-04-22 05:50:43
【问题描述】:
我想用这段代码更新GridView 中的行,但在编辑GridView 后不会改变:
protected void res_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = (DataTable)Session["dt"];
GridViewRow row =res.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["name"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["dewey"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
*dt.Rows[row.DataItemIndex]["subject"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
Session["dt"] = dt;
res.EditIndex = -1;
res.DataSource = dt;
res.DataBind();
}
我的Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt;
if (!IsPostBack)
{
dt= Converter.ListBooks(new classes.Book().GetAll());
Session["dt"] = dt;
res.DataSource = dt;
res.DataBind();
}
else
{
dt=(DataTable)Session["dt"];
res.DataSource = dt;
res.DataBind();
}
}
例如,我将带有 * 的行更改为:
dt.Rows[row.DataItemIndex]["subject"] = "tx";
并且编辑后的“主题”栏变成了“tx”,所以不知道为什么((TextBox)(row.Cells[4].Controls[0])).Text在编辑前返回TextBox的文字?
【问题讨论】:
-
去掉page_load中的else部分,你真的不应该把它绑定在那里。请在引发回发的事件中绑定网格视图
标签: c# asp.net list gridview datatable