We help IT Professionals succeed at work.

Webmatrix and confirmation message

panJames
panJames asked
on
Hello experts.

I am creating new website using webmatrix.

I want to display confirmation message before user decides to really delete something.

How can I do it?

Please have a look at the code attached.

thank you

panJames
*****************************
This is how I fetch data:

var db = Database.Open("StarterSite");
var itemsSelected = db.Query("select * from Items where UserFK = @0", @WebSecurity.CurrentUserId).ToList();


*****************************
This is how I display data:

@foreach (var p in itemsSelected) 
{
	<input type="button" value="Delete!" onclick="itemDelete(@p.id)" />
}


*****************************
itemDelete is actually a javaScript function:

function itemDelete(index)
{
	var r=confirm("Are you sure?");
	
	if (r==true)
	{
		@{
			//this is actually a c# code where I cannot use "index" variable
			var db = Database.Open("StarterSite");
			db.Query("DELETE FROM Items WHERE id = @0", index);
		}
		
	}
	else
	{
	
	}
}

//so I can either:
//learn how to get access to java script variables for my c# code
//or learn how to generate a "are you sure?" form using c#

//additional questions:
//how can I create c# methods to be used within WebMatrix?
//how can I attach them to buttons- like "onclick" in my example?

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
Hi,
you can display confirmation message box before deletion in javascript
here is a sample code you use it before deletion in javascript

var answer = app.alert("Do you want to eat sardine salad?", 2, 2);    

/* Question icon, Yes/No button. */
/* 0 = error, 1 = OK, 2 = Cancel, 3 = No, 4 = Yes */

if (answer == 4)
{
//write deletion code
}


Regards

Dani

Author

Commented:
Dani: Thank you for your answer.

My deletion code needs to know the index of a row to be deleted.

So I need to pass this variable to javaScript (I know how to do it) but use it in c# code (which I struggle with).

panJames

Author

Commented:
Tried such a trick:

 function deletion(a)
            {
                var r=confirm("U sure?");
               
                if (r==true)
                {
                    hdnbox.value = a;
                   
                    @{
                        var db = Database.Open("StarterSite");
                        var index = Request.Form["hdnbox"];
                                               
                        db.Query("DELETE FROM Items WHERE id = @0", index);
                       
                    }
                }    
               
            }

<input type="hidden" id="hdnbox" name="hdnbox" value="0"></input>

but get error message:

System.ArgumentNullException: Parameterized query expects a parameter value which was not supplied.


panJames

CERTIFIED EXPERT

Commented:
Hi,
Fir this scenario you have write all deletion process in C# not in javascript, then you have full control on all the functionality of deletion process and you can do any you want.


Regards

Dani

Author

Commented:
Dani:

Is "function deletion(a)" a correct C# declaration of a method?

It does not compile.

panJames
CERTIFIED EXPERT

Commented:

Author

Commented:
Dani: these links do not solve my problem.

I have really one problem here:

function delete(index)
{
      var r=confirm("Are you sure?");
      
      if (r==true)
      {
            @{
                  var db = Database.Open("StarterSite");
                  
                  //#######################################
                  //HOW CAN I GET VALUE OF "INDEX" HERE???
                  //#######################################
                  
                  db.Query("DELETE FROM table WHERE id = @0", index);
                  
                  
            }
            
      }
}

thank you

panJames
CERTIFIED EXPERT

Commented:
Hi,
If the above function with delete query work then the index variable contains required.
Please checkout.

Dani

Author

Commented:
Dani: can you re- phrase your sentence please?
I do not understand it.

index variable is not being seen because it is inside c# code, outside of JS scope.

panJames
CERTIFIED EXPERT

Commented:
 db.Query("DELETE FROM table WHERE id = @0", index);

in above line i am talking about index if above line works fine then you can use index variable

Author

Commented:
But I cannot use index variable as explained above.

panJames

Explore More ContentExplore courses, solutions, and other research materials related to this topic.