Cancel User control action based on user input on page

Posted on 2011-09-02
Last Modified: 2012-05-12
I have an ASP.NET page containing a fairly complex user control (comprised of a lot of other controls) and a panel that starts invisible and acts as a pop-up confirmation window (I set the visible property to true, and then when a button on the panel is clicked set it back to false.)

There is a button in the user control that acts as a 'save' button, committing any changes made in the control to the database, and another that cancels changes and reverts the control to the original state. I want to have the popup on the page appear when the cancel button is clicked, and then only revert the control to the original state if the users confirms that this is what they want.

I can achieve this by putting the popup panel within the user control rather than on the page, but this will mean duplicating the panel a LOT of times in other controls, and I also want the page to perform other actions when the user confirms the cancellation on the popup.

I can also achieve this by firing a 'cancelling' event in the control, and handling it with the page, and then the page can call a 'revert' method in the control if the user confirms the cancel with the popup, but this will tie the page and the control together so that any other page using the control would have to implement the popup. I want to make sure that the control can be used without a confirmation box if I don't want one.

To summarise: I want to have a control that fires an event when the cancel button in the control is clicked, and then only continues and reverts the control to default values if the event is not handled or if it is handled but the page allows the operation to continue (based on user input or whatever else)

I think this can be achieved using a custom Eventargs class containing a 'continue' field that can be set by the page, but the problem is that there has to be another postback in between.

Hope that isn't too confusing. I'm afraid i can't post the code because there is too much of it and some of it isn't mine.
Question by:silent_waters
  • 3
  • 2

Expert Comment

ID: 36473506
I think you are thinking on the right path. Event is I too believe an ideal solution here as it can keep you control and page on which it is used loosely coupled.

In the control,

1. Implement a Cancel event that is raised when Cancel button is clicked. Just before the usercontrol code that reverts user's changes.
2. This event can use built-in CancelEventArgs. (No need for custom EventArgs with "continue"). CancelEventArgs has a Cancel property for same reason.
3. Before the event is raised but e.Cancel = false;. To allow other pages where you do not want all this support to function as is.
4. After event is raised put condition like if(!e.Cancel) { //code to save control changes }

In the page,

1. Subscribe Cancel event
2. In the event-handler, show the dialog
3. If user decides to Cancel set e.Cancel = true

Hope I haven't missed anything from your question!!! In a little hurry. Would be glad to know the outcome.

Author Comment

ID: 36473637
Thanks for that, that is where i was heading, but the problem arises in that there is a postback between the event being raised and the user input being returned. The value of cancel needs to be set based on whether the user clicks on a 'Continue' or 'Cancel' button on a panel, but I don't have access to e.Cancel by the time I am handling the click event of the panel button on the page.

Accepted Solution

jagrut_patel earned 500 total points
ID: 36483204
Understood your issue. Can you try this.

My User control name is "InputFormUserControl" and the aspx page name is "TestInputFormUserControl". Here is the code.


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="InputFormUserControl.ascx.cs" Inherits="WebApplication1.InputFormUserControl" %>
<asp:Label runat="server" Text="Enter data:"></asp:Label>
<asp:TextBox runat="server" ID="txtData"></asp:TextBox>
<br />
<asp:Button runat="server" ID="btnSave" OnClick="btnSave_Click" Text="Save" />
<asp:Button runat="server" ID="btnCancel" OnClick="btnCancel_Click" Text="Cancel" />

Open in new window


namespace WebApplication1
    public partial class InputFormUserControl : System.Web.UI.UserControl
        public event EventHandler<EventArgs> AfterCancel;

        private string onClientClickCancel;

        public string OnClientClickCancel
                return onClientClickCancel;
                this.onClientClickCancel = value;
        protected override void OnPreRender(EventArgs e)

            if (!string.IsNullOrEmpty(this.onClientClickCancel))
                btnCancel.OnClientClick = this.onClientClickCancel;
                btnCancel.OnClientClick = string.Empty;
        protected void btnSave_Click(object sender, EventArgs e)

        protected void btnCancel_Click(object sender, EventArgs e)
            //Perform cancellation operation like clearing the child controls
            txtData.Text = string.Empty;
            //Raise the AfterCancel event

        protected virtual void OnAfterCancel(EventArgs e)
            if(AfterCancel != null)
                AfterCancel(this, e);

Open in new window


<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="TestInputFormUserControl.aspx.cs" Inherits="WebApplication1.TestInputFormUserControl" %>
<%@ Register Src="~/InputFormUserControl.ascx" TagName="InputFormUserControl" TagPrefix="local" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script language="javascript" type="text/javascript">
        function OnClientClickCancel()
            alert("Show confirmation dialog.");
            //return false; //To cancel post back. i.e. User do not want to revert the changes.
            return true; //To continue post back. i.e. User wants to revert the changes. So let UserControl do what it does on Cancellation.
    <h3>Test App</h3>
    <local:InputFormUserControl ID="myInputFormUserControl" runat="server" OnClientClickCancel="return OnClientClickCancel();" />

Open in new window


namespace WebApplication1
    public partial class TestInputFormUserControl : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            myInputFormUserControl.AfterCancel += new EventHandler<EventArgs>(myInputFormUserControl_AfterCancel);

        void myInputFormUserControl_AfterCancel(object sender, EventArgs e)

Open in new window

- When you run the page you would see a user control with a Textbox and 2 buttons, Save and Cancel.
- Enter some text in the textbox and click Cancel.
- You will see an alert "Show confirmation dialog." (This is a JS function OnClientClickCancel written in TestInputFormUserControl.aspx.cs. See my comments in it.)
- If you put break points in btnCancel_Click event-handler of UserControl and myInputFormUserControl_AfterCancel event-handler of ASPX then you can see that on clicking Cancel both will be hit. Thus, first your UserControl's code will be executed and then the page specific code can be executed in After Cancel.


Author Closing Comment

ID: 36483394
Genius! I don't use Javascript much and my knowledge of it is limited, so I would never have come up with that.

Thanks very much indeed.

Expert Comment

ID: 36483489
My pleasure! Happy to know that it worked.
Thank you for the feedback.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity mvc return json 2 54
How do ASP.NET and MVC work together? 4 27
Angular JS Route 3 47
Turn on intranet settings 1 37
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now