asp.net 不用GridView自带删除功能,删除一行数据
作者:
数据表一定要有个ID的主键值,你的gridview要设定一下DataKeyNames="ID"这个属性值,接下的事件就好多了,写个OnRowDeleting事件就可以了。
前台代码:
<asp:GridView ID="GridLog" runat="server"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
BorderColor="#333" BorderStyle="solid" BorderWidth="1"
OnRowDeleting="PublicGridRowDeleting"
GridLines="None" Width="98%" ForeColor="#333333">
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="false"
ReadOnly="True"
SortExpression="ID" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:BoundField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('您确认删
除?');" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"
/>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center"
/>
<HeaderStyle BackColor="#5A799C" ForeColor="White" Height="22px" />
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<EmptyDataTemplate>
日志库暂时为空!
</EmptyDataTemplate>
</asp:GridView>
CS代码
protected void PublicGridRowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();//strID就是该行的ID
string strSQL = "Delete from table " +
" WHERE id = " + strID;
//执行删除
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT
LANGUAGE='javascript'>alert('删除成功!');</script>");
GridBind();
}
关键是设定好DataKeyNames后,可以靠 string strID = GridLog.DataKeys
[e.RowIndex].Value.ToString();获得选择列的ID值 然后用这个ID执行删除就可以了 。
复制代码 代码如下:
<asp:GridView ID="GridLog" runat="server"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
BorderColor="#333" BorderStyle="solid" BorderWidth="1"
OnRowDeleting="PublicGridRowDeleting"
GridLines="None" Width="98%" ForeColor="#333333">
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="false"
ReadOnly="True"
SortExpression="ID" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:BoundField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Delete" OnClientClick="return confirm('您确认删
除?');" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"
/>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center"
/>
<HeaderStyle BackColor="#5A799C" ForeColor="White" Height="22px" />
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<EmptyDataTemplate>
日志库暂时为空!
</EmptyDataTemplate>
</asp:GridView>
CS代码
复制代码 代码如下:
protected void PublicGridRowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strID = GridLog.DataKeys[e.RowIndex].Value.ToString();//strID就是该行的ID
string strSQL = "Delete from table " +
" WHERE id = " + strID;
//执行删除
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT
LANGUAGE='javascript'>alert('删除成功!');</script>");
GridBind();
}
关键是设定好DataKeyNames后,可以靠 string strID = GridLog.DataKeys
[e.RowIndex].Value.ToString();获得选择列的ID值 然后用这个ID执行删除就可以了 。
您可能感兴趣的文章:
- asp.net GridView控件中模板列CheckBox全选、反选、取消
- Asp.net GridView使用大全(分页实现)
- asp.net gridview代码绑定
- asp.net GridView 删除时弹出确认对话框(包括内容提示)
- asp.net Gridview里添加汇总行
- asp.net GridView排序简单实现
- asp.net DataGridView导出到Excel的三个方法[亲测]
- asp.net中让Repeater和GridView支持DataPager分页
- asp.net 设置GridView的选中行
- asp.net gridview的Rowcommand命令中获取行索引的方法总结
- asp.net实现固定GridView标题栏的方法(冻结列功能)