Link to home
Create AccountLog in
Avatar of panJames
panJames

asked on

Webmatrix and confirmation message

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

ASKER CERTIFIED SOLUTION
Avatar of mcs0506
mcs0506
Flag of Pakistan image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of panJames
panJames

ASKER

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
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

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
Dani:

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

It does not compile.

panJames
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
Hi,
If the above function with delete query work then the index variable contains required.
Please checkout.

Dani
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
 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
But I cannot use index variable as explained above.

panJames