当前位置: 代码迷 >> ASP.NET >> 怎样动态为GridView添加Select、Edit、Delete按钮,该如何解决
  详细解决方案

怎样动态为GridView添加Select、Edit、Delete按钮,该如何解决

热度:10440   发布时间:2013-02-25 00:00:00.0
怎样动态为GridView添加Select、Edit、Delete按钮
我把购物车的信息以DataTable的形式放在Session里,然后与GridView绑定就可以查看购物车内容了。但是如果我想在GridView里增加Select、Edit、Delete等按钮(以及相应处理程序)应该怎么办?

在正常情况下,我用一个ObjectDataSource同GridView绑定,可以在设计状态增加各种按钮,但是现在的GridView在开发环境里空荡荡的怎么设置呢?

谢谢各位的帮助!


------解决方案--------------------------------------------------------
GridView自带了编辑功能
如<asp:TemplateField HeaderText="操作" ItemStyle-HorizontalAlign="Center" ShowHeader="False">
<HeaderStyle Width="20%"></HeaderStyle>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton5" runat="server" CausesValidation="True" CommandName="update">保存</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" CommandName="cancel">取消</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit" CausesValidation="false" OnClientClick="return confirm('确认要编辑吗?');">编辑</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete" CausesValidation="False" OnClientClick="return confirm('确认要删除吗?');">删除</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Select" CausesValidation="False" Visible="false" >选取</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
 protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
BindData();
}

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
this.gv.EditIndex = e.NewEditIndex;
BindData();
}

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
this.gv.EditIndex = e.RowIndex;
int id = int.Parse(this.gv.DataKeys[e.RowIndex].Value.ToString());

this.gv.EditIndex = -1;
BindData();
}

protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
BindData();
}

------解决方案--------------------------------------------------------
帮楼上顶。。。
------解决方案--------------------------------------------------------
帮定~
  相关解决方案