ASP.NET Web Pages – Delete Page

[ This is a 4 part tutorial, previous tutorial updating database entries ]

Compared to the other pages, this page is quite simple. It does not require any validation, just a confirmation if the user wants to delete the page.

@{
 
var Id = Request["id"];
if (!Id.IsInt())
{
    Response.Redirect("~/");
}

var db = Database.Open("StarterSite");
var SQLDELETE = "DELETE FROM Games Where Id=@0";
 
if (IsPost)
{
try
{
    db.Execute(SQLDELETE, Id);
    Response.Redirect("~/");
}catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
}
    
}

Similar to the other pages, when the ID is requested and the user confirms they want to delete the page, the page will be removed and the user will be redirected back.

HTML

<script type="text/javascript">
 
    function goBack() {
        window.location = "@Request["returnurl"]";
    }
 
</script>
 
 
<form method="post">
<fieldset>
 
<legend>Delete Data</legend>
 
<p>Are you sure you wish to delete this page?</p>
<input type="submit" value="Yes"/>
<input type="button" value="No" onclick="goBack();"/>
 
</fieldset>
</form>

For this we only require two buttons: one to confirm if the user wants to delete the page and the other to reject it. The JavaScript will redirect the user back if they reject the confirmation. @Request[“returnurl”] will request the return URL parameter from the URL and the value of window.location will be set to that.

ASP.NET WebPages - Delete Data Page

Summary

This 4-part tutorial covered how to Update, Delete, Create and Read database content as well as validate the content. If you already know C# and SQL this should not be very difficult for you. Using databases you can create many kinds of websites, including small to large corporate websites, and you’re not limited to SQL Server Compact. If you prefer you can use SQL Server , which is fairly easy to do in WebMatrix.