Need help in asp.net cancel button

Asked By Sumit Kumar
07-Nov-09 02:48 AM
Earn up to 0 extra points for answering this tough question.

Hi all,

Could somebody pls explain what exactly happen when user press cancel button in the form. I have form with two button Save and Cancel. How to handle cancel button pls somebody help me with simple coding.

 

Thanks,

Sumit

 

  Cancel Button

F Cali replied to Sumit Kumar
07-Nov-09 10:19 AM

A Cancel button in a form will have the CommandName set to "Cancel" and the CausesValidation set to false.  It is just like any other button with its own CommandName.  On the server side, to know that it is clicked, you can check for the CommandName if it is "Cancel" and you can do whatever you want.

protected void onItemCommand(object sender, FormViewCommandEventArgs fce)
    {
        if (fce.CommandName == "Cancel")
        {
            // do work here
        }
    }

Regards,
SQL Server Helper

  re

Web Star replied to Sumit Kumar
07-Nov-09 11:12 AM

it completely depends upon where u use cancel button and in which content....

eg. as u told in a Form use Save and Cancel button , there are cancel button use for reset all control on The form like when u submit a form fill some data in textbox and other control and when u click save button then save data on same page if u fill data in textbox and other control and click on Cancel button then it resent all control value as previous menas all textbox will be blank.

public void Cancel_Click(object sender, System.EventArgs e)
        {

             txtTitle.Text = "";
                    txtAuthor.Text = "";
                    txtISBN.Text = "";
                    txtPublication.Text = "";
                  

         }

or u can also redirect to another page without saving any data then u just redirect to anothher page on click of cancel buttton as follows

public void Cancel_Click(object sender, System.EventArgs e)
        {

            Response.Redirect("parentpage.aspx");      

         }

so it matter where u use Cancel Button and the purpose of that

hope u understand

Create New Account