[RESOLVED]Which one is better or both are same?

I have a form which has 2 buttons "Delete" and "Edit".. on Post to check which button submitted the request… which of the 2 approaches is better and is there is any difference or both are same? For example for delete button:

@if(isPost){
   if(!Request["buttonDelete"].IsEmpty()){
          //Do something
   }
  if(Request["btn"].Equals("buttonDelete")){
    //do something
  }
}

 

You are testing a condition in each approach. The condition is different but apart from that, the approaches are identical. 

Using just Request[] is normally a bad idea, you’re better tying it down, so if your form is a POST

if (!string.IsNullOrWhiteSpace(Request.Form["buttonDelete"]))
{
// buttonDelete was clicked
}

Hello,

I usually use the first one with try-catch block:

@if(isPost){
   if(!Request["buttonDelete"].IsEmpty()){
        try {
            //do something
        }
        catch(Exception ex){
            //error message
        }
   }
}

Thanks Mike,

can u please comment on my other question too. I borrowed the processing image on form submit from your contact form…

AidyF

Using just Request[] is normally a bad idea,

Normally it’s not a problem. It’s only an issue if you have query string values with the same name as a form field.

Leave a Reply