Prevent Autopostback with a javascript confirm (cs file)

2 posts, 0 answers
  1. Kevin
    Kevin avatar
    1 posts
    Member since:
    Sep 2011

    Posted 02 Sep 2011 Link to this post

    Hi, when I click on the cancel button on the javascript confirm dialog, it does do a postback. 

    The problem is that the file is just a (.cs), so I can't use the "return args.set_cancel(true);" argument in de aspx file.
    .
    Creating Remove button and javascript confirm:
    // Remove
    rtb = new RadToolBarButton();
    rtb.CommandName = "Remove";
    rtb.Text = "";
    rtb.ImageUrl = "~/Images/actiontoolbar/actiontoolbar_remove.png";
    rtb.DisabledImageUrl = "~/Images/actiontoolbar/actiontoolbar_remove_grayed.png";
    rtb.CausesValidation = false;
    rtb.Attributes.Add("onclick", "javascript: if (!confirmDelete()) return false;");
    radtoolbar.Items.Add(rtb);

    Creating message:
    /// <summary>
    /// Shows a conformation dialog when deleting.  On by default.
    /// </summary>
    [DefaultValue(true)]
    public bool ConfirmDelete
    {
        get
        {
            return (bool)(this.ViewState["ConfirmDelete"] ?? true);
        }
        set
        {
            this.ViewState["ConfirmDelete"] = value;
        }
    }
     
    /// <summary>
    /// Shows this message in a conformation dialog when deleting ans ConfirmDelete is on.  On by default.
    /// </summary>
    [DefaultValue("Are you sure you want to delete this record?")]
    public string ConfirmDeleteMessage
    {
        get
        {
            return (string)(this.ViewState["ConfirmDeleteMessage"] ?? "Are you sure you want to delete this record?");
        }
        set
        {
            this.ViewState["ConfirmDeleteMessage"] = value;
        }
    }
     
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        RadScriptManager.RegisterClientScriptBlock(Page, GetType(), "confirmdelete", @"function confirmDelete(){return confirm('" + ConfirmDeleteMessage + "');}",true);
    }

    I hope somebody can help me.

    Thanks.
  2. Kevin
    Kevin avatar
    360 posts
    Member since:
    Jul 2012

    Posted 21 Oct 2011 Link to this post

    Hello Kevin,

    Why don't you handle the RadToollBar's OnClientButtonClicking event? You could do something like this:

    function OnClientButtonClicking(sender args) {
        var toolbarButton = args.get_item();
          
        if(toolbarButton.get_commandName() == "Remove"){
            args.set_cancel(!confirmDelete());
        }
    }

    I hope that helps.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top