您的位置:首页 > 其它

GridView 编辑、更新、取消

2008-12-16 09:03 204 查看
前台:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="GridView1_RowCancelingEdit" DataKeyNames="RollMessageID"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="RollMessageID" HeaderText="编号" Visible="False" />
<asp:BoundField DataField="Heading" HeaderText="姓名" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>

后台:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataInfo();
}

}

//绑定数据
protected void BindDataInfo()
{
RollMessageBusiness rm = new RollMessageBusiness();
GridView1.DataSource = rm.GetAllRollMessage();
GridView1.DataBind();
}

//编辑按钮

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindDataInfo();
}

//更新按钮

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
string strhead = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;

RollMessageBusiness rmb = new RollMessageBusiness();
rmb.UpdateRollMessage(id, strhead);
GridView1.EditIndex = -1;
BindDataInfo();
}

//取消按钮

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindDataInfo();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: