在GridView中加入删除提示
GridView控件有自带的、可以通过配置完成的删除功能,但是它没有提示,点击直接删除。而且由于和选择、编辑按钮很近,所以也比较容易点错。因此我喜欢把删除按钮单独拿出来,并加入删除提示。
请看下面的例子:
1
<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
<Columns>
3
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" Visible="false" SortExpression="id" />
4
<asp:TemplateField>
5
<ItemTemplate>
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" OnClientClick='return confirm("此删除操作不可恢复,您确认删除此用户吗?")'>删除</asp:LinkButton>
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
</ItemTemplate>
10
<HeaderTemplate> </HeaderTemplate>
11
</asp:TemplateField>
12
<asp:BoundField DataField="logname" HeaderText="登录名" SortExpression="logname" Visible="true" />
13
<asp:BoundField DataField="username" HeaderText="用户名" SortExpression="username" Visible="true" />
14
<asp:BoundField DataField="password" HeaderText="密码" SortExpression="password" Visible="false" />
15
</Columns>
16
</asp:GridView>
17
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NewlandConnectionString %>"
18
SelectCommand="SELECT * FROM [Manager] "
19
DeleteCommand="DELETE FROM [Manager] WHERE id=@id">
20
<DeleteParameters>
21
<asp:ControlParameter ControlID="GridView2" Name="id" PropertyName="id" />
22
</DeleteParameters>
23
</asp:SqlDataSource>
24![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
请注意:例子中GridView2的DataKeyNames属性和OnRowDeleting属性,以及LinkButton1的CommandName属性是必须的。DataKeyNames属性指定GridView的索引字段,OnRowDeleting属性指定执行删除操作时调用cs文件中对应的方法,CommandName属性指定点击触发GrifView的Delete操作。
另外,还需要在对应cs文件中加入如下方法:
1
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
2
{
3
int categoryID = (int)GridView2.DataKeys[e.RowIndex].Value;
4
SqlDataSource2.DeleteParameters["id"].DefaultValue = categoryID.ToString();
5
}
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这个方法实现取得当前要删除的行的id,并将其赋值给SqlDataSource2中SQL语句的id参数。
到这里就实现了删除提示功能。简单实用的功能。