home »forums »asp.net topics »getting started / general asp.net »How to call confirm modal from codebehind

How to call confirm modal from codebehind

Topic RSS Feed

Posts under the topic: How to call confirm modal from codebehind

Posted: 3/7/2010 1:15:14 PM

Lurker 105 points Lurker
  • Joined on: 11/1/2009 2:23:31 AM
  • Posts: 21

Hi,

I need to call confirm modal from codebehind. I have a submit button now when user click the button I need to have some processing and then I need to  show confirm nmodal to the user.


Posted: 3/8/2010 6:46:31 AM

Contributor 5349 points Contributor
  • Joined on: 4/15/2009 12:12:23 PM
  • Posts: 236
answered  Answered

what you can do is probably toggle the visibilty of your modal popup control.. You can also create your own custom modal control with panel and within that panel you can add buttons like Yes or No and based on whatever button is click, you can easily toggle the visibilty of the panel based on your conditions.


Posted: 3/9/2010 8:35:40 PM

Professional 9529 points Professional
  • Joined on: 4/19/2009 1:46:52 AM
  • Posts: 219
answered  Answered

Here is the sample:

 

 

using System;
using System.Web.UI;
using System.Text;


/// <summary>
/// 
/// </summary>
public partial class _Default : Page
{
    #region Private Properties

    bool _isConfirmNeeded = true;
    string _confirmMessage = string.Empty;

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets or sets a value indicating whether this instance is confirm needed.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is confirm needed; otherwise, <c>false</c>.
    /// </value>
    public bool IsConfirmNeeded
    {
        get { return _isConfirmNeeded; }
        set { _isConfirmNeeded = value; }
    }

    /// <summary>
    /// Gets or sets the confirm message.
    /// </summary>
    /// <value>The confirm message.</value>
    public string ConfirmMessage
    {
        get { return _confirmMessage; }
        set { _confirmMessage = value; }
    }

    #endregion

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        IsConfirmNeeded = true;
        ConfirmMessage = "Do you want to proceed ?";

        // Insure that the __doPostBack() JavaScript is added to the page...
        ClientScript.GetPostBackEventReference(this, string.Empty);

        if (IsPostBack)
        {
            string eventTarget = Request["__EVENTTARGET"] ?? string.Empty;
            string eventArgument = Request["__EVENTARGUMENT"] ?? string.Empty;

            switch (eventTarget)
            {
                case "UserConfirmationPostBack":
                    if (Convert.ToBoolean(eventArgument))
                    {
                        SaveDataInDB();
                    }
                    else
                    {

                        // User said NOT to do it...
                    }
                    break;
            }
        }
    }

    /// <summary>
    /// Handles the OnClick event of the Button1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Button1_OnClick(object sender, EventArgs e)
    {
        ////Do your stuff here.....


        // All server side execution goes here and, if user confirmation is needed,
        // set isConfirmNeeded to true and create the confirmMessage text.

        if (IsConfirmNeeded)
        {
            StringBuilder javaScript = new StringBuilder();

            string scriptKey = "ConfirmationScript";

            javaScript.AppendFormat("var userConfirmation = window.confirm('{0}');\n", ConfirmMessage);

            // Un-comment to only PostBack if user answers OK...
            //javaScript.Append("if ( userConfirmation == true )\n");
            javaScript.Append("__doPostBack('UserConfirmationPostBack', userConfirmation);\n");

            ClientScript.RegisterStartupScript(GetType(), scriptKey, javaScript.ToString(), true);
        }
    }

    protected void SaveDataInDB()
    {
        //Do your stuff of saving data in db here...
    }

}





 


Page 1 of 1 (3 items)