GridView 编辑、更新、取消
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.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();
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步