We continue the development of a simple web application using WebMatrix, the new instrument publicly released by Microsoft in early January 2011. WebMatrix is a platform for Windows, which is distributed at no charge, which allows you to create web applications “from scratch” using a database including SQL Server Compact, fully compatible with the engine of the “big brother” SQL Server.
Web applications are being developed can be tested locally thanks to the web server (IIS Express 7.5) that WebMatrix will install automatically. The development tool from Microsoft is also designed to “test” and customize as you see fit operation of all the open source web application built in ASP.NET or PHP.
To begin, you must open the project so far developed WebMatrix then add (click on the New button on the toolbar), a new file named this dbelimina.cshtml:
At this point, you should delete the content added by default in WebMatrix dbelimina.cshtml the page and insert instead the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
@{ var id=0; if (Request[ "id" ].IsInt()) { id = Request[ "id" ].AsInt(); } else { Response.Redirect(@Href( "~/catalogo.cshtml" )); } if (id < 1) { Response.Redirect(@Href( "~/catalogo.cshtml" )); } var db = Database.Open( "Catalogo" ); var NomeProdotto= "" ; var NomeScientifico= "" ; var Descrizione= "" ; if (IsPost){ } else { var sqlquery = "SELECT * FROM Piante WHERE id=@0" ; var record = db.QuerySingle(sqlquery,id); if (record == null ) { Response.Redirect(@Href( "~/catalogo.cshtml" )); } NomeProdotto=record.Nome; NomeScientifico=record.Nome_scientifico; Descrizione=record.Descrizione; } } <h1>Eliminazione di una scheda prodotto</h1> <p>Sei sicuro di voler eliminare il prodotto <strong>@NomeProdotto</strong>?</p> <form action= "" method= "post" > <input type= "submit" value= "Sì" /> <input type= "button" value= "No" onclick= "window.location = 'catalogo.cshtml'" /> </form> |
As with the dynamic files for editing the contents of the database (dbmodifica.cshtml), saw in the previous episode (see this article), the file for the removal of information from the database is to be invoked indicating the input, the ‘ indetificativo corresponding to the record you want to delete (eg dbelimina.cshtml? id = 10 will remove the record with identifier “10″).
The first lines (2 to 10) are employed to control which input is inserted a numerical value and that this is not negative. Otherwise, it is simply made a redirect to the home page of the web application (catalogo.cshtml) without any further (Response.Redirect (@ Href (“~ / catalogo.cshtml”)) .
When the page is called directly, without clicking any button next HTML form, to be executed is the code enclosed in the else block (lines 20-27). This code is the same as the page dbmodifica.cshtml and is used to retrieve the contents of the various database fields (columns), corresponding to the record as having the specified ID (the identifier value passed in from the page dbelimina.cshtml, is saved in the variable id with the assignment at line 4).
The name of the corresponding product identifier is stored in the variable ProductName (line 25) can then be retrieved for display in the HTML code of the page (line 33):
Into Are you sure you want to delete the product <strong> @ ProductName </ strong> </ p>
It remains to define how the page should react when the user confirms the deletion of this product. The form (lines 35-38) shows two buttons: in the case in which the removal of the records not be confirmed (pressing the button No), as shown, is made a simple redirect using JavaScript code (onclick = “window.location = ‘catalogo.cshtml’ “). Otherwise (by pressing the Yes button), a transaction will be made to POST to the same page dbelimina.cshtml (action=”" <form method=”post”>).
To complete the script, then you need to specify – within the block if (isPost) {} – the following
1
2
3
|
var sqldelete = "DELETE FROM Piante WHERE ID=@0" ; db.Execute(sqldelete,id); Response.Redirect(@Href( "~/catalogo.cshtml" )); |
In this way, when you press the Yes button will delete the item with the specified ID as input (it will be executed on the database, the SQL query DELETE).
The final code of dbelimina.cshtml then becomes the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
@{ var id=0; if (Request[ "id" ].IsInt()) { id = Request[ "id" ].AsInt(); } else { Response.Redirect(@Href( "~/catalogo.cshtml" )); } if (id < 1) { Response.Redirect(@Href( "~/catalogo.cshtml" )); } var db = Database.Open( "Catalogo" ); var NomeProdotto= "" ; var NomeScientifico= "" ; var Descrizione= "" ; if (IsPost){ var sqldelete = "DELETE FROM Piante WHERE ID=@0" ; db.Execute(sqldelete,id); Response.Redirect(@Href( "~/catalogo.cshtml" )); } else { var sqlquery = "SELECT * FROM Piante WHERE id=@0" ; var record = db.QuerySingle(sqlquery,id); if (record == null ) { Response.Redirect(@Href( "~/catalogo.cshtml" )); } NomeProdotto=record.Nome; NomeScientifico=record.Nome_scientifico; Descrizione=record.Descrizione; } } <h1>Eliminazione di una scheda prodotto</h1> <p>Sei sicuro di voler eliminare il prodotto <strong>@NomeProdotto</strong>?</p> <form action= "" method= "post" > <input type= "submit" value= "Sì" /> <input type= "button" value= "No" onclick= "window.location = 'catalogo.cshtml'" /> </form> |
Finally, you need to edit the page catalogo.cshtml so that there is a link to the eventual elimination of each record. After opening the file, you must replace the line <li> <a href=”dbmodifica.cshtml?id=@row.id”> @ row.Nome (<i> @ row.Nome_scientifico </ i>) </ a> </ li> with the following:
1
2
3
|
<li><a href= "dbmodifica.cshtml?id=@row.id" >@row.Nome (<i>@row.Nome_scientifico</i>)</a> <a href= "dbelimina.cshtml?id=@row.id" ><img src= "ico_elimina.gif" alt= "Elimina" border= "0" /></a> </li> |
Ico_elimina.gif The icon can be downloaded from here. This image must be saved with the same name, in the project folder WebMatrix on which you are working.
Here’s how it will appear in the list of products (catalogo.cshtml page in your web browser):
Clicking on various red crosses the page is fetched for the removal of each product:
The test application can be realized since here now, for example, transported online leaning on Internet providers that provide hosting services ASP.NET.