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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…

617 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