GridView控件有自带的、可以通过配置完成的删除功能,但是它没有提示,点击直接删除。而且由于和选择、编辑按钮很近,所以也比较容易点错。因此我喜欢把删除按钮单独拿出来,并加入删除提示。

请看下面的例子:

 

 1在GridView中加入删除提示<asp:GridView ID="GridView2" runat="server" DataKeyNames="id" OnRowDeleting="GridView2_RowDeleting" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" Width="100%" AutoGenerateSelectButton="true" AutoGenerateEditButton="false" AutoGenerateDeleteButton="false" RowStyle-VerticalAlign="NotSet">
 2在GridView中加入删除提示<Columns>
 3在GridView中加入删除提示<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" Visible="false" SortExpression="id" />
 4在GridView中加入删除提示<asp:TemplateField>
 5在GridView中加入删除提示<ItemTemplate>
 6在GridView中加入删除提示
 7在GridView中加入删除提示<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" OnClientClick='return confirm("此删除操作不可恢复,您确认删除此用户吗?")'>删除</asp:LinkButton>
 8在GridView中加入删除提示
 9在GridView中加入删除提示</ItemTemplate>
10在GridView中加入删除提示<HeaderTemplate>&nbsp;</HeaderTemplate>
11在GridView中加入删除提示</asp:TemplateField>
12在GridView中加入删除提示<asp:BoundField DataField="logname" HeaderText="登录名" SortExpression="logname" Visible="true" />
13在GridView中加入删除提示<asp:BoundField DataField="username" HeaderText="用户名" SortExpression="username" Visible="true" />
14在GridView中加入删除提示<asp:BoundField DataField="password" HeaderText="密码" SortExpression="password" Visible="false" />
15在GridView中加入删除提示</Columns>
16在GridView中加入删除提示</asp:GridView>
17在GridView中加入删除提示<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NewlandConnectionString %>"
18在GridView中加入删除提示SelectCommand="SELECT * FROM [Manager] " 
19在GridView中加入删除提示DeleteCommand="DELETE FROM [Manager] WHERE id=@id">
20在GridView中加入删除提示<DeleteParameters>
21在GridView中加入删除提示<asp:ControlParameter ControlID="GridView2" Name="id" PropertyName="id" />
22在GridView中加入删除提示</DeleteParameters>
23在GridView中加入删除提示</asp:SqlDataSource>
24在GridView中加入删除提示

 

请注意:例子中GridView2DataKeyNames属性和OnRowDeleting属性,以及LinkButton1CommandName属性是必须的。DataKeyNames属性指定GridView的索引字段,OnRowDeleting属性指定执行删除操作时调用cs文件中对应的方法,CommandName属性指定点击触发GrifViewDelete操作。

另外,还需要在对应cs文件中加入如下方法:

 

 

1在GridView中加入删除提示protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
2

 

这个方法实现取得当前要删除的行的id,并将其赋值给SqlDataSource2SQL语句的id参数。

到这里就实现了删除提示功能。简单实用的功能。

相关文章:

  • 2021-12-24
  • 2022-12-23
  • 2022-01-23
  • 2021-11-02
  • 2021-06-09
  • 2022-12-23
  • 2021-06-08
  • 2021-12-09
猜你喜欢
  • 2021-10-18
  • 2022-02-06
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2021-12-31
相关资源
相似解决方案