![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//前台:
<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();
}