Asp.Net-informations.com   Advertisement
     SiteMap

ASP.NET GridView Delete

The GridView control provides many built-in capabilities that allow the user to sort, update, delete, select, and page through items in the control. The following tutorial shows how to delete a row from Gridview and display a confirmation message before deleting the specified row.

gridview-delete

To enable delete , set the AutoGenerateDeleteButton to true and specify the delete command in the SqlDataSource.

  DeleteCommand="DELETE From [stores] WHERE [stor_id] = @stor_id"

Here we are trying to display a confirmation message before deleting the specified row. In order to doing this we have to write a small Javascript code for display confirmation message.

  function isDelete()
  {
	return confirm("Do you want to delete this row ?");

}

We have to call this Javascript function on OnClientClick event of the delete LinkButton.

  

<asp:LinkButton ID="DeleteBtn" runat="server" CommandName="Delete"
OnClientClick="return isDelete();">Delete</asp:LinkButton>

The follwoing ASP.NET program shows how to delete a row from Gridview and display a confirmation message before deleting the specified row.

Default.aspx

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title>Untitled Page</title>
	<script type="text/javascript">
		function isDelete()
		{
			return confirm("Do you want to delete this row ?");
		}
	</script>
</head>
<body>
	<form id="form1" runat="server">
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
	AllowSorting="True" DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="stor_id" >
		<Columns>
			<asp:BoundField ReadOnly="True" HeaderText="stor_id"
			DataField="stor_id" SortExpression="stor_id"></asp:BoundField>
			<asp:BoundField HeaderText="stor_name" DataField="stor_name"
			SortExpression="stor_name"></asp:BoundField>
			<asp:BoundField HeaderText="stor_address" DataField="stor_address"
			SortExpression="stor_address"></asp:BoundField>
			<asp:BoundField HeaderText="city" DataField="city"
			SortExpression="city"></asp:BoundField>
			<asp:BoundField HeaderText="state" DataField="state"
			SortExpression="state"></asp:BoundField>
			<asp:BoundField HeaderText="zip" DataField="zip"
			SortExpression="zip"></asp:BoundField>
			<asp:TemplateField>
				<ItemTemplate>
					<asp:LinkButton ID="DeleteBtn" runat="server" CommandName="Delete"
					OnClientClick="return isDelete();">Delete
					</asp:LinkButton>
				</ItemTemplate>
			</asp:TemplateField>
		</Columns>
	</asp:GridView>
	<div>
	<asp:SqlDataSource ID="SqlDataSource1" runat="server"
	ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
	SelectCommand="select * from stores"
	DeleteCommand="DELETE From [stores] WHERE [stor_id] = @stor_id" >
	<DeleteParameters>
		<asp:Parameter Name="stor_id" Type="String" />
	</DeleteParameters>
	</asp:SqlDataSource>
	</div>
	</form>
</body>
</html>


Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.cs
default.aspx.vb



ASP.NET GridView - Related Contents


More Source Code :
|  Home  |   VB.NET  |   C#  |   ASP.NET  |   SiteMap  |   Terms of Use  |   About  |