3. 使用"编辑" 功能
1 先添加一列 "编辑、更新、取消"
2 GridView 属性的 事件里 有RowEditing 双击进入后台:
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
int index = e.NewEditIndex;
this.GridView1.EditIndex = index;
//这样写是错误的: this.GridView1.DataBind();
this.InitGridView();
}
4 . 使用更新 UpDating
protected
void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{
int id = -1;
string ot_name = string.Empty;
string way = string.Empty;
string fenlei = string.Empty;
try
{
id =
Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
}
catch
{
}
TextBox tb =
(TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]);
TextBox tb1 =
(TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]);
TextBox tb2 =
(TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]);
if (tb != null)
{
ot_name = tb.Text;
}
if (tb1 != null)
{
way = tb1.Text;
}
if (tb2 != null)
{
fenlei = tb2.Text;
}
bool bo = false;
bo = new ChuLi.Class1().Updating(ot_name, way, fenlei,id);
if (bo == true)
{
this.RegisterStartupScript("sss",
"<script>alert('修改成功')</script>");
}
else
{
this.RegisterStartupScript("sss",
"<script>alert('修改失败')</script>");