user1729807
user1729807

Reputation:

Implement messagebox for warn to user about delete data

I want to delete a some data in a page and want to warn to user (show a messagebox with YES/NO) and if user click on YES delete data

is it possible to implememt MessageBox in ASP?if yes how?

Upvotes: 0

Views: 1143

Answers (3)

MilkyWayJoe
MilkyWayJoe

Reputation: 9092

with confirm it's as simple as:

<input type="submit" value="delete" onclick="javascript:confirm('Are you sure?')"/>

As for ASP.NET, you can do this on server-side:

btnDelete.Attributes.Add("onclick", "return confirm('Are you sure?')");

Or, on markup:

<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure?');" />

Edit: Using a GridView, you can do something like this on the server-side code:

public partial class _Default : System.Web.UI.Page {

    Dictionary<string, string> collection = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            collection = new Dictionary<string, string>();
            collection.Add("Microwave", "$299");
            collection.Add("Coffee maker", "$59");
            collection.Add("Arm chair", "$89");
        }
        GridView1.DataSource = collection;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (DataControlRowType.DataRow == e.Row.RowType)
        {
            ((LinkButton)e.Row.FindControl("lnkDelete"))
                .Attributes.Add("onclick", "return confirm('are you sure?')");

        }
    }

    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) {
        if (e.CommandName.ToLower() == "delete")
        {
            // this code should be executed only when the user clicks "ok"
            // in the confirm message that appears on the browser

            // your implementation goes here
        }
    }
}

And your markup could be done similarly to the following. As for the template column, it's easy to create a command column through visual studio and then convert it into a template column, so you can actually have an ID for the delete link button (or a button) and find it through e.Row.FindControl as shown above.

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"
    OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Key" HeaderText="Product" />
        <asp:BoundField DataField="Value" HeaderText="$$$" />
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" 
                    CommandName="Delete" Text="Remove"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Upvotes: 1

christiangobo
christiangobo

Reputation: 530

You can use jQuery to do that!

<!doctype html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>jQuery UI Dialog - Modal confirmation</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script>

    <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
        <script>
    $(function() {
        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: {
                "Delete all items": function() {
                  alert('deleted');
                  $( this ).dialog( "close" );
                },
                Cancel: function() {
                  alert('cancel')
                    $( this ).dialog( "close" );
                }
            }
        });
    });
    </script>
</head>
<body>

<div id="dialog-confirm" title="Empty the recycle bin?">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>


</body>
</html>

Reference: http://jqueryui.com/dialog/#modal-confirmation

Upvotes: 0

Thousand
Thousand

Reputation: 6638

<asp:Button ID="Button1" runat="server" Text="Button"
 OnClientClick="return confirm('Are you sure you want delete this?');" />

Upvotes: 1

Related Questions