Solved

Cancel User control action based on user input on page

Posted on 2011-09-02
5
341 Views
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.
0
Comment
Question by:silent_waters
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:jagrut_patel
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.
0
 

Author Comment

by:silent_waters
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.
0
 
LVL 8

Accepted Solution

by:
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.

InputFormUserControl.ascx

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


InputFormUserControl.ascx.cs

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

        private string onClientClickCancel;

        public string OnClientClickCancel
        {
            get
            {
                return onClientClickCancel;
            }
            set
            {
                this.onClientClickCancel = value;
            }
        }
        
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!string.IsNullOrEmpty(this.onClientClickCancel))
                btnCancel.OnClientClick = this.onClientClickCancel;
            else
                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
            OnAfterCancel(EventArgs.Empty);
        }

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

Open in new window


TestInputFormUserControl.aspx

<%@ 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>
<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.
        }
    </script>
    <h3>Test App</h3>
    <local:InputFormUserControl ID="myInputFormUserControl" runat="server" OnClientClickCancel="return OnClientClickCancel();" />
</asp:Content>

Open in new window


TestInputFormUserControl.aspx.cs

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.

HTH!
0
 

Author Closing Comment

by:silent_waters
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.
0
 
LVL 8

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

22 Experts available now in Live!

Get 1:1 Help Now