This is a migrated thread and some comments may be shown as answers.

Prevent Autopostback with a javascript confirm (cs file)

1 Answer 232 Views
ToolBar
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 02 Sep 2011, 07:21 PM
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.

1 Answer, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 21 Oct 2011, 02:02 PM
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.
Tags
ToolBar
Asked by
Kevin
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
Share this question
or